2

I'm trying to get my head around how computation in the EVM works in this example.

PC: 3 STACK: [54] MEM: [], STORAGE: {}

SLOAD pops one from the stack, and pushes the value in contract storage at that index onto the stack. Since the contract is used for the first time, it has nothing there, so zero.

PC: 4 STACK: [0] MEM: [], STORAGE: {}

Can someone explain what SLOAD did? It says "pops one from the stack", ok that would be 54. "And pushes the value in contract storage" Which value? The one popped (i.e, the key?) or the message value? and then nothing is added to storage.. no idea whats going on :S

3

This is the state before the SLOAD:

PC: 3 STACK: [54] MEM: [], STORAGE: {}

The phrase "SLOAD pops one from the stack" means it takes the top item from the stack, in this case 54. This is the operand for SLOAD.

So SLOAD then looks up the value of key 54 in contract storage and pushes the value it finds there to the stack. In this case the value of storage at key 54 is zero (since the contract is being run for the first time), so the value pushed to the stack is 0.

Hence the final state (just zero on the stack).

PC: 4 STACK: [0] MEM: [], STORAGE: {}

I think you were misled by the phrase, "pushes the value in contract storage" - this is very lazily worded. It would be clearer to say "takes the value from contract storage and pushes it to the stack".

  • to further clarify this, SLOAD pops the key 54 from the stack and checks in storage what is the value with key 54. Since the storage is empty, zero will be added to the stack. This wasn't clear for me either, one might think that since the key is missing we shouldn't add anything to the stack but SLOAD decides to set the value to 0. – Shakur Dec 8 '17 at 6:49
  • Yes, non-present storage values are defined to be zero in the EVM, and vice-versa. – benjaminion Dec 8 '17 at 6:58

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.