Learning the Dark Arts of Yul ( Part I )

Learning the Dark Arts of Yul ( Part I )

Introduction

Yul is an intermediate language that can be used with Solidity or as a standalone language . It is particularly useful for writing optimized Smart Contract

Lets look at some Coding Examples

{
    // Declare variables without specifying types
    let a 
    let b

    //Assign values to variables
    a:=10
    b:=20

    // Perform arithmetic operation (implicility handled by EVM )
    let c:= add(a,b)

    // Store result in memory
    mstore(0x00,c)

    // Return memory position and size
    return(0x00,0x20)

}

Storage in Yul

contract StorageExample{
    // slot 0 is used for storing the number
    uint256 private storedNumber = 10;

    // Function to store a number in Storage in Yul
    function storeNumber(uint256 num) public {
        assembly{
            sstore(0,num)
        }        

    }

    // Function to retrieve the stored number from storage using Yul
    function retreiveNumber() public view returns(uint256){
        uint num
        assembly{
            // load the number from slot 0
            num := sload(0)
        }

        return num;

    }

}

This is just a basic overview of understanding the Yul syntax , we’ll be deep diving in the next part to some complex operations which can be performed using Yul , So stay tuned .