All About EVM Instructions And Opcodes:
Venkatesh R
Posted on November 6, 2022
EVM Instructions and Opcodes:
Do you want to gain a deep understanding about EVM Instructions and Opcodes? Or Do you want to become Senior Smart Contract developer?
Well, good news! you're in the right place.
What is EVM ?
The Ethereum Virtual Machine or EVM is a piece of software that executes smart contracts and computes the state of the Ethereum network after each new block is added to the chain
What is Assembly ?
Assembly refers to any low-level programming language that is converted to machine code using an assembler. Assembler languages are tied to either a physical or a virtual machine, because they implement its instruction set. An instruction tells simply the CPU to do some fundamental task, such as adding two numbers, etc.
Yul language is being used as low level Assembly Language for the Ethereum.
Getting Started:
EVM is a stack based virtual machine which uses few instruction set to perform its fundamental tasks.
EVM instruction sets:
- Stack Instruction Set
- Arthmetic Instruction Set
- Comparision Instruction Set
- Memory Instruction Set
- Storage / Context Instruction Set
Stack Instruction Set:
Stack Instruction is being used to push / pop the one or more values into the stack to perform its fundamental tasks.
Stack Instructions:
- PUSHN - pushes a value into the top of the stack where N is the byte size of value Example: PUSH1, PUSH2, PUSH3, etc.
- POP - pops out a value from the top of the stack
- SWAPN - swaps a value from the top of the stack at the index N
- DUPN - duplicate the value from the stack at the position N and push the duplicated value to the top of the stack.
There are many instructions available on the Stack Instruction Set.
Example:
PUSH1 0x01 // pushes 1 [1]
PUSH1 0x02 // pushes 2 [2, 1]
PUSH1 0x02 // pushes 2 [2, 2, 1]
POP // pops out the value 2 [2, 1]
DUP1 // duplicates the value 2 and push the value [2, 2, 1]
Arthmetic Instruction Set:
Arthmetic Instruction takes two / more values from the top of the stack and performs arthmetic operation and push the resultant value into the top of the stack
Arthmetic Instructions:
- ADD - Add two values from the top of the stack and pushes to the stack.
- SUB - Subtract two values from the top of the stack and pushes to the stack.
- DIV - Divide two values from the top of the stack and pushes to the stack.
- MUL - Multiplies two values from the top of the stack and pushes to the stack.
There are many instructions available on the Arthmetic Instruction Set.
Example:
PUSH1 0x01 // pushes 1 [1]
PUSH1 0x02 // pushes 2 [2, 1]
ADD // Adds two values from top of the stack [3]
PUSH1 0x01 // pushes 1 [1, 3]
SUB // Subtract the two value [2]
Comparision Instruction Set:
Comparision Instruction takes two / more values from the top of the stack and performs comparision operation and push the resultant value into the top of the stack
Comparision Instructions:
- LT - Compares the top value from stack with second top value and pushes the result into the top of the stack.
- GT - Compares the top value from stack with second top value and pushes the result into the top of the stack.
- EQ - Pushes true if the top two values are equal
- ISZERO - Pushes true if the top value of the stack is 0
There are many instructions available on the Comparision Instruction Set.
Example:
// false = 0 and true = 1
PUSH1 0x01 // pushes 1 [1]
PUSH1 0x01 // pushes 2 [1, 1]
EQ // stores the result [1] / [true]
ISZERO // stores the result [0] / [false]
POP // pops out the values []
Memory Instruction Set:
Memory Instruction are used to read and write the chunks of values into the memory.
Memory Instructions
- MSTORE - writes a value into the memory
- MLOAD - reads a value from the memory
Example:
PUSH1 0x01 // pushes 1 [1]
PUSH1 0x00 // pushes 0 [0, 1]
// Stores the value 1 into the memory slot zero
MSTORE
PUSH1 0x00 // pushes 0 [0]
// Loads the value from the memory at slot zero
MLOAD // [O]
Context Instruction Set:
Context Instruction are used to read and write the data from the global state / execution context.
Context Instructions (READ):
- SLOAD - Loads the values from the storage with respect to the pointer value
- CALLER - Loads the caller of the contract context
- TIMESTAMP - Loads the current block's timestamp
There are many instructions available on the Context Instruction Set (READ).
Context Instructions (WRITE):
- SSTORE - Stores the values into the storage with respect to the pointer
- CALL - Used to make a call to external contract and update the global state
- CREATE - Used to deploy the contract in the blockchain state
- LOGN - Used to log the contract's execution data, where N is the number of indexed Topics.
There are many instructions available on the Context Instruction Set (WRITE).
Example:
CALLER // pushes contract caller [caller_address]
PUSH1 0x00 // pushes 0 [0, caller_address]
// Loads the caller_address into the first storage slot
SSTORE // []
PUSH1 0x00 // pushes 0 [0]
// Loads the caller_address from the first storage slot
SLOAD // [caller_address]
Conslusion:
It is very important for the smart contract developers and auditors to undertand that how solidity based smart contracts/EVM executes its fundamental tasks behind the scenes to get deep foundation of EVM. To know more about the other EVM opcodes, please visit Here and if you want to play with these opcodes in the real time playground, please visit EVM codes playground website .
Hope you guys have learnt something new today, do follow me to get more exiting contents on #blockchain #Web3 #SmartContracts #Solidity #Rust #Solana
Posted on November 6, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
March 16, 2022