Learn EVM Opcodes VII

Solidity Programming Language
3 min readMar 13, 2023

--

Learn EVM Opcodes VII

We dealt with MSTORE8, MLOAD and Return opcodes in this lesson.

MSTORE8

We use the MSTORE opcode for storing 32-byte value to write in the memory. However MSTORE8 opcode is used for storing 1-byte value to write in the memory.

We can write 32 bytes and 1 byte of data to memory, but reading is only valid for 32 bytes.

I love Noxx

Ok.

This is new opcode block. You can try to execute in playground.

PUSH1 0xCC
PUSH1 0
MSTORE
PUSH1 0xDD
PUSH1 30
MSTORE8

How is memory shaped with this opcode block?

MSTORE8

Via MSTORE8, we save the 0xdd value(1 byte sized)to the 30th byte in memory.

MLOAD

We pull the data from memory to the stack via MLOAD. Remember that we can pull it in sets of 32 bytes.

This is our opcode block.

PUSH1 0xCC
PUSH1 0
MSTORE
PUSH1 0xDD
PUSH1 30
MSTORE8
PUSH1 0
MLOAD

What is the result ? Please think twice.

PUSH1 0
MLOAD

It means that MLOAD takes the datas from the memory (0. byte to 31.byte)

MLOAD Execute it

The result is “ddcc

MLOAD Execute it

This is our new opcode block.

PUSH1 0xCC
PUSH1 0
MSTORE
PUSH1 0xDD
PUSH1 30
MSTORE8
PUSH1 31
MLOAD

Yes. The result is changing.

MLOAD Execute it

The result is “cc

MLOAD Execute it

RETURN

This opcode is used as follows.

offset: Starting from which byte the data will be copied from memory

size: How many bytes of data to copy

Here the return opcode does not throw the data to the stack, it returns it directly.

Let’s make an example.

PUSH3 0xAABBCC

PUSH1 0

MSTORE

We loaded 3 bytes of AABBCC data into the memory. Note that this data is on the far right because evm loads it as 32 bytes.

Now let’s return this data from memory with the help of the following codes.

PUSH1 3

PUSH1 29

RETURN

In short, return 3 bytes of data starting byte 29.

Execute it

What do we see when we do a similar operation with MLOAD?

PUSH3 0xAABBCC

PUSH1 0

MSTORE

PUSH1 29

MLOAD

Execute it

When we call the 29th byte with MLOAD, in addition to the 29th byte, we also call the following 31 bytes of data.

aabbcc0000000000000000000000000000000000000000000000000000000000

We also call this data to the stack.

Associate Professor Engin YILMAZ (VeriDelisi)

--

--

Solidity Programming Language
Solidity Programming Language

Written by Solidity Programming Language

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