3

In the tutorial here, the program counter increases by 2 for a PUSH1 opcode. Then it increases by 1 for other opcodes. Why is this? Does it have to do with the opcodes size in memory? Aren't they all 256 bit?

PC: 11 STACK: [32] MEM: [], STORAGE: {}

PC: 13 STACK: [2020202020] MEM: [], STORAGE: {}

PC: 14 STACK: [2020202020, 0] MEM: [], STORAGE: {}

PC: 16 STACK: [2020202020, 54] MEM: [], STORAGE: {}

PC: 17 STACK: [] MEM: [], STORAGE: {54: 2020202020}
2
  • FYI refer here to learn how to write a URL markdown Commented Aug 29, 2017 at 13:56
  • Hi, can you accept the answer if it helped? or need more clarification? Commented Jan 15, 2019 at 6:51

2 Answers 2

2

As shown in the example code there

PUSH1 0 CALLDATALOAD SLOAD NOT PUSH1 9 JUMPI STOP JUMPDEST PUSH1 32 CALLDATALOAD PUSH1 0 CALLDATALOAD SSTORE

the indexing is arranged as;

0 -> PUSH1

1 -> 0

2 -> CALLDATALOAD

3 -> SLOAD ......

In the program 0, it refers to PUSH1 and pushes the value at 1 to stack and the next instruction at the 2 should be executed. Hence the PC becomes 2.

0
0

The EVM increases the program counter (PC) by one for all opcodes except for PUSH and its derivatives. As per the docs on Source Mappings:

Each of these elements corresponds to an instruction, i.e. you cannot use the byte offset but have to use the instruction offset (push instructions are longer than a single byte).

Thus when the EVM sees this:

PUSH1 0

It will increase the PC by two, because there is firstly the PUSH1 opcode, and then there is 0, the value pushed on the stack.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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