3

Looking at the opcode level, the first few instructions of any smart contracts start with this:

push1 80
push1 40
mstore

I understand that this saves 0x80 to address 0x40, but what is this all for? I looked into the Ethereum Yellow paper, but it never mentioned this anywhere.

Thanks.

1

The solidity docs state the following:

"Solidity manages memory in a very simple way: There is a “free memory pointer” at position 0x40 in memory. If you want to allocate memory, just use the memory from that point on and update the pointer accordingly."

Reference: https://solidity.readthedocs.io/en/v0.4.21/assembly.html

edit

Example from the docs:

mstore(0x40, 0x60) // store the "free memory pointer"
// ...
// memory allocator
function $allocate(size) -> pos {
    pos := mload(0x40)
    mstore(0x40, add(pos, size))
}

First, it stores the address 0x60 as the current memory pointer by using mstore(adr, value) to store 32 bytes. Then it creates a function, allocate(size). The function allocate(size) is functioning in the following way: Given the argument size the function caller can specify how much memory the program requires. The function then loads the current "free memory pointer" at address 0x40 (which is 0x60 before calling this function the first time in this case) and adds size to it. After this the function returns the new "free memory pointer" and the programmer can simply use the size bytes the program has allocated.

edit 2: concrete example

I am very inexperienced in coding assembly code for the EVM, so please take in account that the following example could be wrong. I'll try to give an example that shows how to allocate space (32 bytes) for a variable, store a value, read the value and finally free the memory.

// prepare our variables
// x = variable used to store the contents of the allocated memory
// p = current "free memory pointer"
let x := 0
let p := mload(0x40)

// allocate 32 bytes of space
mstore(0x40, add(p, 0x20))

// store up to 32 bytes in the allocated space
mstore(p, 0xDEADBEEF)

// load the value we just stored
x := mload(p)

// We don't need the allocated space anymore. Free it.
mstore(0x40, p)
  • interesting, does this means EVM does not use memory from 0x40 onwards itself?? – user311703 Aug 19 '18 at 10:50
  • @user311703 The EVM does use memory from the memory location stored in memory address 0x40, but the programmer or indirectly the compiler manages it (increment memory pointer at 0x40 and decrement to free the memory again). I updated my answer so it contains an example now. – sea212 Aug 19 '18 at 12:34
  • thanks a lot, but i am still not sure how the "free memory" can be used in low level opcode? can you give an example? thanks! – user311703 Aug 19 '18 at 16:45
0

Opcodes are what Solidity and other languages compile down to so that the EVM can react with a contract, similar to how Assembly language works.

The EVM understands these opcodes, and each of them have an associated cost, which is where your gasLimit comes into play for each transaction.

  • nice, but this does not really answer my question. thanks! – user311703 Aug 19 '18 at 10:49

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.