🌱
Dev Compendium
  • Ethereum
    • Solidity
      • EVM
      • Architecture
      • Execution Context
      • Transactions
      • Gas
      • Calldata, Memory & Storage
      • Gas Optimisation
      • Function Declarations
      • receive() & fallback()
      • CALL vs. DELEGATE CALL
    • Yul
      • Introduction
      • Types
      • Basic Operations
      • Storage
      • Memory
        • Arrays
        • Structs
        • Tuples, revert, keccak256
        • Logs and Events
        • Gotchas
        • abi.encode
      • Calldata
        • External Calls
        • Dynamic Length Inputs
        • Transferring Value
        • Receiving Contract Calls
      • Contracts in Yul
      • Other Yul Functions
    • Foundry
    • Security
      • Common Vulnerabilities
      • Best Practices
      • Development Workflow
      • Contract Migration
    • Auditing Tools
      • Slither
      • Mythril
      • Fuzzing
    • Upgradable Contracts
      • Upgrade Patterns
      • ERC-1967 Implementation
      • Deployment
    • MEV
    • Tooling
      • Chainlink
      • IPFS
      • Radicle
    • Frontend
      • Contract Hooks
      • Wallet Connection
        • wagmi.sh
        • Rainbow Kit
      • thirdweb
    • Protocol Research
      • Uniswap v2
      • Uniswap v3
      • Curve
      • GMX
  • Starkware
    • Fundamentals
    • Account Abstraction
    • Universal Deployer
    • Cairo 1.0
    • starknet.js
    • Security Model
  • Zero Knowledge
    • Group Theory
    • ECDSA
  • Rust
    • Basic Operations
    • Set up
    • Primitives
    • Control Flow
    • Mutability & Shadowing
    • Adding Behavior
    • Lifetimes
    • Std Library
  • SUI
    • Architecture
    • Consensus Mechanism
    • Local Node Setup
    • Sui Client CLI
    • Move Contracts
      • Move
      • Move.toml
      • Move.lock
      • Accessing Time in Sui Move
      • Set up Development Framework
      • Debug & Publish
      • Package Upgrades
      • Sui Move Library
      • Difference from Core Move
    • Object Programming
      • Object Basics
      • Using Objects
      • Immutable Objects
      • Object Wrapping
      • Dynamic Fields
      • Collections
      • Unit Testing
      • Deployment with CLI
  • NEAR
    • Architecture
    • Contract Standards
      • Fungible Token (NEP-141)
      • Non-Fungible Token (NEP-171)
      • Storage Management (NEP-145)
      • Events (NEP-297)
      • Meta-Transactions
    • Rust Contracts
      • Development Workflow
      • Smart Contract Layout
      • Storage Management
      • Events & Meta-transactions
      • Method Types
      • Upgrading Contracts
      • Unit Testing
    • NEAR Libraries
    • Environment Variables
    • Serialisation
    • Security Concepts
    • Collections
    • JS SDK
Powered by GitBook
On this page
  1. Ethereum
  2. Yul

Basic Operations

Math Operations

function add(uint x, uint y) external pure returns (uint256) {
        uint ret; 
        assembly {
            ret := add(x,y)
        }
        return ret; 
    }

    function subtract(uint x, uint y) external pure returns (uint256) {
        uint ret; 
        assembly {
            ret := sub(x,y)
        }
        return ret; 
    }

    function multiply(uint x, uint y) external pure returns (uint256) {
        uint ret; 
        assembly {
            ret := mul(x,y)
        }
        return ret; 
    }

		// Note: division rounds down 
    function divide(uint x, uint y) external pure returns (uint256) {
        uint ret; 
        assembly {
            ret := div(x,y)
        }
        return ret; 
   } 

For Loops

  • There are no else statements in Yul

function isPrime(uint256 x) public pure returns (bool p) {
        p = true;
        assembly {
            let halfX := add(div(x, 2), 1)
            for { let i := 2} lt(i, halfX) { i := add(i,1) } {
                if iszero(mod(x, i)) { // if the modulus of x with is zero, the number is not prime 
                    p := 0 // setting p = 0 equates to false 
                    break
                }

                i := add(i, 1)
            }
        }
    }

Comparisons

function max(uint256 x, uint256 y) external pure returns (uint256 maximum) {
        assembly {
            if lt(x, y) {
                maximum := y
            }
            // there are no else statements in solidity 
            if iszero(lt(x, y)) { // if lt(x, y) is false, then iszero will return true because false is zero in bytecode  
                maximum := x
            }
        }
    } 

// alternatively: 
function max2(uint256 x, uint256 y) external pure returns (uint256 maximum) {
        assembly  {
            if lt(x, y) {
                maximum := y 
            } 
            if gt(x,y) {
                maximum := x
            }
        }
    }

PreviousTypesNextStorage

Last updated 1 year ago