Track 3 · Ethereum & the EVM · lesson 4
Stepping the EVM
11 min
Track 3 · Ethereum & the EVM · lesson 4
11 min
Your Solidity compiles to about 140 possible opcodes. Here is what
count = count + 1 actually becomes.
The EVM is a stack machine. There is nowhere to put a named value — every operation pops its arguments off a stack and pushes its result back.
ADD takes the top two items and replaces them with their sum. SLOAD takes an
address off the stack and pushes back what is stored there. That is the whole
model.
The stack holds up to 1024 items, each 32 bytes wide. 32 bytes is the machine's
natural word size, which is why uint256 is the default and why smaller types
are often more expensive — they need extra instructions to mask down to size.
Six instructions. Total 5,012 gas. Five thousand of it is SLOAD and SSTORE.
The arithmetic is free by comparison. Everything you will ever read about "gas optimisation" is, at bottom, about touching storage less.
Predict
No floating point. No randomness. No network access. No system calls. No threads.
Every one of those would let two nodes reach different answers, and consensus would break. The EVM is not a small machine because nobody got round to extending it — it is small because everything it can do, thousands of machines must be able to agree on exactly.
Solidity lets you drop into raw opcodes with assembly { }. Production code
uses it for things the language cannot express cheaply — reading the size of
calldata, custom memory layouts, cheap hashing.
You do not need to write it. You do need to not be frightened of it, because it shows up in exactly the audited, high-value contracts most worth reading — OpenZeppelin and Uniswap both use it in hot paths.
Check