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

Signed arithmetic opcodes

The same word bits can be interpreted as signed or unsigned.

LET’S PICTURE IT

One printed bit pattern can follow different reading conventions.

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.

Keep the bits unchanged

Stack words do not carry a signed-type label. Signed operations interpret their words using two’s-complement rules while unsigned operations apply an unsigned interpretation.

Choose the correct comparison

SLT and SGT perform signed comparisons. LT and GT perform unsigned comparisons, so a word representing minus one behaves very differently under the two choices.

Distinguish signed division

SDIV interprets signed operands and truncates toward zero. It has defined boundary behavior that should not be replaced by floating-point host-language arithmetic.

Check operand order

Non-commutative operations require a clear pop order. Confirm which value is first and second under the opcode specification before predicting subtraction or division.

Trace the idea

all-ff word as uint256: 2^256 - 1
all-ff word as int256: -1

SLT and LT can disagree for the same bit patterns.

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

QUICK CHECK · NO PRESSURE

Does a stack word include a separate signed-type tag?

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.