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}

Paul Razvan Berg
  • 17,902
  • 6
  • 73
  • 143
Gawnie
  • 331
  • 1
  • 6

2 Answers2

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.

Achala Dissanayake
  • 5,819
  • 15
  • 28
  • 38
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.

Paul Razvan Berg
  • 17,902
  • 6
  • 73
  • 143