5

I'm trying to figure out how EVM bytecode works and I created an empty contract in Remix. The assembly version looks like this:

PUSH1 0x60 
PUSH1 0x40 
MSTORE 
CALLVALUE 
ISZERO 
PUSH1 0xE 
JUMPI 
PUSH1 0x0 
DUP1 
REVERT 
JUMPDEST 
PUSH1 0x35 
DUP1 
PUSH1 0x1B 
PUSH1 0x0 
CODECOPY 
PUSH1 0x0 
RETURN 
STOP


PUSH1 0x60 
PUSH1 0x40 
MSTORE 
PUSH1 0x0 
DUP1 
REVERT 
STOP

AFAIK the first part is executed at contract creation, and the second part is the contract code itself.

What I fail to understand though is this part:

PUSH1 0x60 
PUSH1 0x40 
MSTORE 

Why do we need to store smth in memory at the beginning of each program?

4

Answering my own question:

This is the free memory pointer. The first 0x40 bytes are reserved for internal calculations. Then 0x40 - 0x60 (one word) stores a pointer to free memory (available for allocations) - which is in this case obviously is 0x60.

So this code:

PUSH1 0x60 
PUSH1 0x40 
MSTORE 

Initializes memory pointer.

Details: https://solidity.readthedocs.io/en/develop/miscellaneous.html#layout-of-state-variables-in-storage

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.