No history yet

EVM Architecture for Systems Engineers

Stack Machines vs. Register Machines

Your experience with x86 or ARM has trained you to think in terms of registers. You load data into specific, named locations like RAX or R1, perform an operation, and store the result. The Ethereum Virtual Machine, or EVM, throws this model away. It's a stack-based machine.

Imagine a stack of plates. You can only add a new plate to the top or take the top plate off. All EVM operations work with the items at the top of this stack. To add 5 and 7, you'd push 7 onto the stack, then push 5. The ADD opcode would then pop both values, add them, and push the result, 12, back onto the top. This Last-In, First-Out (LIFO) approach simplifies the machine's design, which is crucial for a deterministic, decentralized environment where every node must get the exact same result.

Another immediate difference is the word size. Instead of 32 or 64 bits, the EVM uses a 256-bit word size. This isn't arbitrary. It's tailored for the cryptographic operations that underpin blockchain technology. Common tasks like calculating Keccak-256 hashes or handling public-key cryptography become much more efficient when the machine's native word size matches the data. The EVM also stores data in big-endian format, where the most significant byte is stored at the lowest memory address.

The Three Data Realms

In a traditional C++ environment, you manage memory across the stack, the heap, and data segments. The EVM formalizes its memory model into three distinct locations, each with a specific purpose and cost structure. Understanding these is the key to writing efficient and secure smart contracts.

LocationVolatilityScopeAnalogyPrimary Use
MemoryVolatilePer-callRAMStaging data, complex operations
StoragePersistentContract-wideHard DriveStoring contract state
CalldataImmutablePer-callFunction arguments (argv)Input data from external calls

Let's break these down. is a byte-array that acts like temporary scratch space. It's clean for every new message call and is used for complex computations, building data structures, or passing arguments to other contracts. It's cheaper than storage but not free; it expands in 32-byte chunks, and the cost increases quadratically as it grows.

Lesson image

Storage is where the contract's state lives permanently on the blockchain. It's a key-value store mapping 256-bit keys to 256-bit values. Think of it as the contract's hard drive. Because this data is replicated and verified by every node in the network, writing to storage is the most expensive operation in the EVM. The opcodes for this are SSTORE (write) and SLOAD (read).

A simple SSTORE operation can cost 20,000 gas, whereas writing to memory (MSTORE) might only cost 3 gas. This massive difference is a central design consideration for any smart contract.

Finally, there's Calldata. This is a special, read-only location that contains the data payload of a transaction or call. It's where function arguments and other input data are stored. Because it's immutable and doesn't require nodes to modify their state, accessing calldata is significantly cheaper than accessing storage or even memory. It's the most gas-efficient way to pass large amounts of data into a contract.

A New Kind of Storage

The high cost and permanence of Storage create challenges. What if you need to share data between calls within the same transaction, but don't need it to persist forever? Previously, developers resorted to inefficient workarounds using storage, writing a value and then clearing it later to get a gas refund.

introduced a fourth data location: transient storage. This is a game-changer for certain design patterns.

Transient storage behaves like storage—it's a key-value map—but it's discarded at the end of a transaction. The TSTORE and TLOAD opcodes provide a cheap way to pass state between different contract calls in a single, atomic transaction without incurring the high cost of permanent storage writes. This is particularly useful for re-entrancy locks or for passing calculated values along a call chain, which previously required complex and expensive workarounds.

As a systems engineer, recognizing the performance hierarchy is crucial. Calldata is for input, Memory is for execution, and Storage is for state. Using an expensive location when a cheaper one would suffice is a common source of gas-inefficiency. Likewise, misunderstanding how Storage works can lead to subtle but critical bugs like storage collisions in upgradeable contracts. These low-level details are the foundation of secure and efficient decentralized systems.

Quiz Questions 1/6

How does the Ethereum Virtual Machine (EVM) architecture fundamentally differ from traditional processor architectures like x86 or ARM?

Quiz Questions 2/6

What is the primary reason the EVM uses a 256-bit word size?