Learn EVM Opcodes IX

Solidity Programming Language
3 min readMar 17, 2023
Learn EVM Opcodes IX

We focus on SLOAD Opcode and how storage works in EVM.

SLOAD

Storage is the area where we store state variables on EVM. It is not a temporary space like Memory. This space has (2²⁵⁶)-1 slots, each slot is 32 bytes (256 bits).

If a state variable is uint256, 1 slot is occupied.

If the state variable you will use is small, you can define it as a smaller sized variable (uint8, uint16, uint32, uint64, uint128).

This structure works based on the key and value relationship.

Now let’s see how the recording is done on the storage with the help of the following code. Note that all our variables are uint256, each of them occupies a 1 slot.

I illustrated this code as follows. Every slot has one variable because we define every variable as uint256.

Could you see key-value relationship ? It is really easy to understand.

“0” is a key and “9” is a value.

“1” is a key and “8” is a value.

“2” is a key and “7” is a value.

“3” is a key and “6” is a value.

As you can see, we directly define 4 state variables on the contract. We will use the readStorageSlot0 function to see the state of these variables on the storage.

We use SLOAD opcode. This opcode loads slot from storage.

For example, if you would like to the second slot from storage, you can use SLOAD(2)

If you want to see which slot on Remix, enter the number of that slot. I first wanted to see what was in the 2nd slot.

I first wanted to see what was in the 2nd slot.

When we call the 2nd slot after compile-deploy on Remix, we will get the answer “7”. The view of the 2nd slot is as follows.

0x0000000000000000000000000000000000000000000000000000000000000007

I wanted to see what was in the 3nd slot.

When we call the 3nd slot after compile-deploy on Remix, we will get the answer “6”. The view of the 3nd slot is as follows.

0x0000000000000000000000000000000000000000000000000000000000000006

As a result of the operation as a whole, we opened 4 slots. Its appearance will be as follows.

0x0000000000000000000000000000000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000070000000000000000000000000000000000000000000000000000000000000006

Ok.

We need to change our code.

Please read carefully. All variables are uint64. Therefore, all variables are in the Slot 0.

I wanted to see what was in the 0nd slot.

When we call the 0nd slot after compile-deploy on Remix, the view of the 0nd slot is as follows.

0x0000000000000006000000000000000700000000000000080000000000000009

Attention ! We have only 1 slot and all variables are in the same slot.

Associate Professor Engin YILMAZ (VeriDelisi)

--

--

Solidity Programming Language

Solidity basics for beginners: Learn the fundamentals of smart contract development and build your first DApp! #Solidity #Foundry #Ethereum #Opcodes #DApps