Understanding EVM(Ethereum Virtual Machine)

NAMASTE 🙏,
If you’re a blockchain developer or security researcher or even into web3, there’s one thing you need to know: the Ethereum Virtual Machine (EVM). It’s what makes apps on Ethereum run, and if you want to build in Web3, learning about the EV…


This content originally appeared on DEV Community and was authored by Dhanyosmi

NAMASTE 🙏,
If you’re a blockchain developer or security researcher or even into web3, there’s one thing you need to know: the Ethereum Virtual Machine (EVM). It’s what makes apps on Ethereum run, and if you want to build in Web3, learning about the EVM is a must. But how it's working behind the scene? Let’s break down .

Ethereum

Where data is stored in EVM?

Let's have a look at where data is stored in EVM.
There are total 6 areas where data can be stored in EVM

  1. Stack
  2. Memory
  3. Call
  4. Storage
  5. Code
  6. Log

Now let's see top 4 which is necessary for understanding EVM.
Stack, Memory and call are temporary while storage is permanent.

STACK

  • In computing, a stack is a data structure that follows the Last In, First Out (LIFO) principle. This means that the last element added to the stack is the first one to be removed.
  • In EVM stack we store 32 bytes long word. Storing something in stack means storing it in hex form .For example – If I want to store 1 in stack then I will push hex representation of 1 in stack.

Note: Top item in stack is widely used for interacting with opcode.
(What are OPCODES? – we will discuss it later in this blog).

Four basic operation that we can perform in stack:-

  1. Push – Place a new value onto the top of stack
  2. Pop – remove the top value from stack
  3. Swap - exchange the position of the top element with an element further down in the stack.
  4. Duplicate – copies one of the stack elements and pushes the duplicate onto the top of stack .

What are the type of thing that we can store in the stack, that can be used to interact with opcodes?

Data type could be address, uint, bool, pointer, array…etc
Maximum number of item that EVM stack can have is 1024.
Pushing more than 1024 item in stack will lead to stack overflow.

MEMORY

  • It's similar to a stack
  • It also stores 32 bytes long word but it does not use any sort of pushing or popping property like stack.
  • Instead of asking the environment for more memory, you simply use the memory location you need. It allows direct access to specific memory locations.

Memory has particular layout

  1. 0x00 to 0x20 - it is reserved for solidity to have some scratch space to work around while doing hashing…etc.

  2. 0x40 – This address is home to free memory and it points to 0x00000...80.
    Free memory pointer points to the first spot in memory that dosn't have something written to it .

  3. 0x60 - This address is used for initial value for dynamic memory as just a zero value .

  4. 0x80 - This is the first safe, empty space where new data can be stored in memory. Solidity writes new data here when performing memory allocations.

CALL DATA

It also stores arbitrary bytes but the difference is that in call data you can only read from this area. It is read-only. Reading from call data is cheaper than using memory or storage.

Why call data is cheaper than memory?

  • Gas cost for reading from call data is lower because it involves retrieving data that already exists in the transaction and not copying it into memory or storage.
  • Memory, on the other hand, is dynamically allocated during contract execution. When you store data in memory, the EVM needs to allocate space, manage it, and handle memory expansion.
  • If you have memory parameter for one of your function but never need to modify that memory parameter then use call data.

STORAGE

  • It is the only place to store data permanently on chain.
  • It works somewhat similar to memory where we have 32 bytes word that get stored. This time in storage we have storage slot. There is no reserved area in storage like we saw in memory.

How do all these different area of EVM come together to give us what we know as EVM?

OPCODE brings it all together and make it all of these things to interact with each other and really workable.

OPCODE is a specific set of instruction give to the EVM in order to do something.

Example:
ADD - Its hex value is 0x01 – It is use to take item that are on the top of STACK, add them and store the result of that addition on the stack.
Lets look at few OPCODE that interact with the area of storage, memory and stack .
0x60 – PUSH1 - place 1-Byte item on stack.
0x61 – PUSH2 - place 2-Byte item on stack.
.

.
.
.
.
.
0x7f – PUSH32 - place 32 byte item on stack.
0x50 – POP- Remove item from stack.

  • MLOAD- load word from memory
  • MSTORE- save word to memory
  • MSTORES- save byte to memory .
  • SLOAD- load word from storage
  • SSTORE- store word in storage There are plenty of OPCODE which have different function. You can look into Ethereum yellow paper.(refer to page no.31)

ETH yellow Paper

How this opcode come into play and actually used by EVM?

In order to understand this visually let's move to EVM Codes.

EVM code website

Here you can understand how the stacks, memory and storage is manipulated throughout the transaction.

STEP 1: -
Go to EVM codes and click on Playground.

EVM code playground

STEP 2:
Select Solidity and Click on RUN command.

EVM code - Visual representation of Opcode

STEP 3:
In below image you can see the multiple opcode listed down there, MEMORY, STACK area and one area that I have marked by clicking on that you can see sequential execution of OPCODE and change in memory, stack field accordingly.

Opcode working

Now let's have a look of some of initial lines of opcode.

First one is PUSH1 opcode of 80. So we are placing 80 on the top of the stack, that’s our first 32 byte long word. Now we are pushing another word that is 0x40.
Now what we do with that two thing on the stack?
You can clearly see the next one is MSTORE.

Now its happening exactly what we talked about in memory.(free memory pointer).
So we are taking 0x80 and mstoring it at ox40 .
So this is the set up of free memory pointer that will happened in beginning of any txn.

Now there is sequence of OPCODE which will give a specific set of instruction to the EVM in order to do something. I highly recommend you to go through it once and take help from yellow white paper and chatGPT.

The EVM is, computation engine that executes smart contracts and manages state changes on the Ethereum blockchain. It reads bytecode instructions, which are compiled from high-level code (like Solidity), allowing interactions with Ethereum's blockchain.

Let me know if you've any questions or feedback.
Have a great day. Till next time!


This content originally appeared on DEV Community and was authored by Dhanyosmi


Print Share Comment Cite Upload Translate Updates
APA

Dhanyosmi | Sciencx (2024-10-27T05:01:52+00:00) Understanding EVM(Ethereum Virtual Machine). Retrieved from https://www.scien.cx/2024/10/27/understanding-evmethereum-virtual-machine/

MLA
" » Understanding EVM(Ethereum Virtual Machine)." Dhanyosmi | Sciencx - Sunday October 27, 2024, https://www.scien.cx/2024/10/27/understanding-evmethereum-virtual-machine/
HARVARD
Dhanyosmi | Sciencx Sunday October 27, 2024 » Understanding EVM(Ethereum Virtual Machine)., viewed ,<https://www.scien.cx/2024/10/27/understanding-evmethereum-virtual-machine/>
VANCOUVER
Dhanyosmi | Sciencx - » Understanding EVM(Ethereum Virtual Machine). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/27/understanding-evmethereum-virtual-machine/
CHICAGO
" » Understanding EVM(Ethereum Virtual Machine)." Dhanyosmi | Sciencx - Accessed . https://www.scien.cx/2024/10/27/understanding-evmethereum-virtual-machine/
IEEE
" » Understanding EVM(Ethereum Virtual Machine)." Dhanyosmi | Sciencx [Online]. Available: https://www.scien.cx/2024/10/27/understanding-evmethereum-virtual-machine/. [Accessed: ]
rf:citation
» Understanding EVM(Ethereum Virtual Machine) | Dhanyosmi | Sciencx | https://www.scien.cx/2024/10/27/understanding-evmethereum-virtual-machine/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.