EVM Architecture
The Ethereum Virtual Machine (EVM) is a stack-based, quasi-Turing-complete virtual machine that executes smart contract bytecode identically on every Ethereum node worldwide. Understanding its architecture helps you write more gas-efficient and correct contracts.
EVM Data Areas
The EVM has five primary data areas:
1. Stack (LIFO, max depth 1024, 32-byte words)
- All arithmetic and logic operates here
- ADD pops top 2, pushes sum
- SWAP, DUP manipulate positions
- Stack overflow = OOG or invalid jump
2. Memory (linear, 32-byte aligned, temporary)
- Expands on demand (each new 32-byte page costs gas)
- Cleared at end of call frame
- Used for: function args, local variables, ABI encoding
- Costs: 3 gas per 32 bytes, quadratic for large expansions
3. Storage (256-bit key/value, persistent)
- Each contract has its own isolated storage namespace
- Persists between transactions (permanent state)
- Most expensive: 20,000 gas (new slot) / 2,900 gas (update)
- SLOAD: 100-2,100 gas depending on access list (EIP-2929)
4. Calldata (read-only, from transaction data)
- Input data from external callers
- Cheapest to read: 4 gas (zero byte) / 16 gas (non-zero byte)
- Cannot be modified within the call
5. Code (immutable, bytecode of the contract)
- CODECOPY copies bytecode ranges to memory
- EXTCODECOPY copies external contract codeCommon Mistakes
- Reading storage in loops — each SLOAD costs 100-2100 gas. Cache storage values in memory variables inside loops.
- Growing memory excessively — memory expansion has quadratic cost above ~724 bytes. Avoid allocating large arrays in memory if they can be processed iteratively.
- Ignoring warm vs cold storage slots — EIP-2929: first access to a storage slot costs 2100 gas (cold); subsequent accesses cost 100 gas (warm). Use access lists for known slots.
Tip
Tip
Practice EVM Architecture in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Once deployed, smart contracts are immutable — code is law
Practice Task
Note
Practice Task — (1) Write a working example of EVM Architecture from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with EVM Architecture is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready web3 code.
Key Takeaways
- The Ethereum Virtual Machine (EVM) is a stack-based, quasi-Turing-complete virtual machine that executes smart contract bytecode identically on every Ethereum node worldwide.
- Reading storage in loops — each SLOAD costs 100-2100 gas. Cache storage values in memory variables inside loops.
- Growing memory excessively — memory expansion has quadratic cost above ~724 bytes. Avoid allocating large arrays in memory if they can be processed iteratively.
- Ignoring warm vs cold storage slots — EIP-2929: first access to a storage slot costs 2100 gas (cold); subsequent accesses cost 100 gas (warm). Use access lists for known slots.