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

ADD, MUL and modular arithmetic

Raw word arithmetic wraps modulo two to the power of 256.

LET’S PICTURE IT

A fixed-width counter returns to zero after reaching its largest representable value.

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.

Distinguish raw opcodes from language checks

EVM ADD and MUL operate on fixed-width words. Solidity can compile additional checks around arithmetic, so checked Solidity behavior is not simply the raw opcode alone.

Apply the modulus

Adding one to the largest 256-bit unsigned word produces zero under raw ADD. Multiplication also retains the result modulo 2²⁵⁶.

Track consumed values

ADD consumes two stack items and pushes one result. A trace must remove the operands instead of leaving them beside the result.

Use wider reasoning where required

ADDMOD and MULMOD have distinct semantics for a supplied modulus. Do not reproduce them by wrapping ordinary ADD or MUL first and then applying that modulus.

Trace the idea

[2, 3] → ADD → [5]
[5, 4] → MUL → [20]
[(2^256)-1, 1] → ADD → [0]

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

QUICK CHECK · NO PRESSURE

What does raw ADD produce for maximum uint256 plus one?

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.