EVM & Architecture
The Ethereum Virtual Machine (EVM) is a stack-based computer. It has no registers, but it has access to three main data locations: the Stack (for calculations), Memory (temporary byte array), and Storage (persistent key-value store).
Syntax Structure
Key Components
- Boilerplate structure required by the Solc compiler.
- Placement of state variables versus functions.
Practical Implementation
This contract demonstrates the cost difference between Memory (cheap, temporary) and Storage (expensive, persistent).
Core Concepts
Stack
LIFO data structure. Max 1024 depth. Used for all mathematical operations and local variables.
Memory
Linear, byte-addressable volatile storage. Cleared at the end of transaction. Costs grow quadratically with size.
Storage
Permanent key-value store. 32-byte keys map to 32-byte values. Extremely expensive to write.
Mental Model
The Industrial Workbench
Imagine a workbench. The **Stack** is your hands (can only hold a few things, where work happens). **Memory** is the table surface (plenty of room for current project, wiped clean daily). **Storage** is the filing cabinet in the back (infinite space, lasts forever, but takes a long walk to retrieve/store files).
Under the Hood
The EVM is a quasi-Turing complete machine. It is 'quasi' because execution is limited by Gas. It is a big loop that fetches the next instruction (PC), executes it, pays gas, and updates state.
Gas Analysis
Memory is cheap but not free. Storage is very expensive.
- • Use `calldata` instead of `memory` for read-only function arguments.
- • Cache storage variables in stack/memory if reading multiple times.
Security Risks
Deep understanding of the EVM prevents subtle bugs.
- ! Stack Too Deep: Solidity can't access variables deep in the stack (limit ~16).
- ! Out of Gas: Infinite loops or massive allocations will revert the transaction.
Common Use Cases
Interactive Visualization
See exactly how the EVM handles this code in memory, storage, and the stack.
Click Run to execute the simulation
Then use the step controls to navigate