All About EVM Instructions And Opcodes:
Tags: blockchain, web3, solidity, smartcontract
Published: 2022-11-06 10:29:55

## 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.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dj854y97mpk5efokqadi.png)

**EVM instruction sets:**
1. Stack Instruction Set
2. Arthmetic Instruction Set
3. Comparision Instruction Set
4. Memory Instruction Set
5. 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:
1. **PUSHN** - pushes a value into the top of the stack where N is the byte size of value Example: PUSH1, PUSH2, PUSH3, etc.
2. **POP** - pops out a value from the top of the stack
3. **SWAPN** - swaps a value from the top of the stack at the index N
4. **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:
1. **ADD** - Add two values from the top of the stack and pushes to the stack.
2. **SUB** - Subtract two values from the top of the stack and pushes to the stack.
3. **DIV** - Divide two values from the top of the stack and pushes to the stack.
4. **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:
1. **LT** - Compares the top value from stack with second top value and pushes the result into the top of the stack.
2. **GT** - Compares the top value from stack with second top value and pushes the result into the top of the stack.
3. **EQ** - Pushes true if the top two values are equal
4. **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
1. **MSTORE** -  writes a value into the memory 
2. **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):
1. **SLOAD** - Loads the values from the storage with respect to the pointer value
2. **CALLER** - Loads the caller of the contract context
3. **TIMESTAMP** - Loads the current block's timestamp

There are many instructions available on the Context Instruction Set (READ).

Context Instructions (WRITE):
1. **SSTORE** - Stores the values into the storage with respect to the pointer
2. **CALL** - Used to make a call to external contract and update the global state
3. **CREATE** - Used to deploy the contract in the blockchain state
4. **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](https://github.com/crytic/evm-opcodes) and if you want to play with these opcodes in the real time playground, please visit [EVM codes playground website ](https://www.evm.codes/playground).

Hope you guys have learnt something new today, do follow me to get more exiting contents on #blockchain #Web3 #SmartContracts #Solidity #Rust #Solana 

You can reach out to me via [LinkedIn](https://www.linkedin.com/in/venkatesh16031999/), [Github](https://github.com/venkatesh16031999)