In this track: Inside the EVM
advanced · 4 MIN READ

The program counter

Instruction positions are byte offsets into the executing code.

LET’S PICTURE IT

A bookmark tracks the current position in a machine-readable instruction strip.

SEE IT FOR YOURSELF
Watch a stack machine execute a tiny programStep 1 / 4
Watch a stack machine execute a tiny programProgram→ PUSH1 0x02PUSH1 0x03ADD · STOPStackEmptyPC = 0A fixed local model; not a complete EVM implementationPUSH1 0x02Push immediate 2Next PC = 2Stack · top first2PUSH1 occupies one opcode byte plus one immediate bytePUSH1 0x03Push immediate 3Next PC = 4Stack · top first32The first value is below the most recently pushed valueADD3 + 2 = 5Next: STOP at PC 5Stack5No automatic SSTOREStack, memory and persistent storage are different locations

The program bytes are PUSH1 02, PUSH1 03, ADD, STOP. The program counter begins at byte zero.

Animated local teaching model. No wallet, RPC or live blockchain data. Some labels and hashes are abbreviated for readability.

Count code bytes

The program counter identifies a byte position in the current code. It is not a source line number or the number of instructions already executed.

Skip immediate bytes

PUSH1 consumes one following immediate byte. PUSH2 consumes two. Those data bytes are part of the instruction encoding rather than separate executed opcodes.

Account for control flow

A valid jump can move execution to another permitted instruction position. A trace must follow the actual path rather than simply scanning every code byte once.

Relate positions carefully

Source maps can connect compiled positions to source ranges, but compiler transformations complicate the relationship. Preserve the exact compiled bytecode when debugging.

Trace the idea

PC 0: PUSH2 0x1234
PC 3: POP
PC 4: STOP

Stack examples list the bottom first and the top last. These small traces omit gas and surrounding dispatch unless stated.

QUICK CHECK · NO PRESSURE

After PUSH2 with two immediate bytes at PC 0, where is the next instruction?

Your progress stays in this browser.
Keep exploring the original referenceethereum.org ↗

Original explanations by EtherTools. Examples are educational and source-linked; this site is independent of the Ethereum Foundation. Our approach.