Learning Objectives – after this chapter you will be able to:
  • Describe the EVM as a deterministic, stack-based state machine.
  • Distinguish stack, memory, storage and calldata.
  • Read simple opcodes and explain how gas is charged for them.
  • Explain the ABI, function selectors and EVM-compatible chains.
What is the EVM?
The Ethereum Virtual Machine (EVM) is the runtime environment that executes smart-contract bytecode on every Ethereum node. Ethereum can be viewed as a state machine: given the current state and a block of transactions, the EVM deterministically computes the next state.
  • Deterministic: the same transaction on the same state produces exactly the same result on every node.
  • Isolated (sandboxed): contract code cannot access the node’s files, network or clock directly.
  • Metered: every instruction costs gas, so execution always terminates (Chapter 12).
  • Stack-based with a word size of 256 bits, which suits Keccak-256 hashes and elliptic-curve arithmetic.
Where Data Lives in the EVM
AreaDescriptionLifetimeCost
StackHolds operands for instructions; up to 1,024 items of 256 bits.During one executionCheapest
MemoryByte-addressed, expandable scratch space.During one callCheap, grows with size
StoragePersistent key–value store of 256-bit slots belonging to the contract.Permanent (on-chain)Most expensive
CalldataRead-only input data of the transaction or call.During one callCheap to read
CodeThe contract’s immutable bytecode.PermanentPaid at deployment
LogsEvent data written to receipts; readable off-chain, not by contracts.Permanent (in receipts)Cheaper than storage
Opcodes and Bytecode

Compilers such as solc (Solidity) and Vyper translate source code into bytecode – a sequence of one-byte opcodes and their data.

OpcodeHexAction
ADD0x01Pop two values, push their sum.
MUL0x02Pop two values, push their product.
KECCAK2560x20Hash a region of memory.
CALLDATALOAD0x35Read 32 bytes of input data.
MSTORE0x52Write a word to memory.
SLOAD / SSTORE0x54 / 0x55Read / write contract storage.
PUSH10x60Push a one-byte constant onto the stack.
CALL0xF1Call another contract or send ETH.
RETURN / REVERT0xF3 / 0xFDEnd execution returning data / undo state changes.

Example: the bytecode 60 03 60 05 01 means PUSH1 3, PUSH1 5, ADD – leaving 8 on the stack.

ABI and Function Selectors
  • The ABI (Application Binary Interface) defines how to encode function calls and decode return values and events.
  • The first 4 bytes of a call’s data are the function selector = first 4 bytes of Keccak-256 of the function signature. For example, transfer(address,uint256) → 0xa9059cbb and balanceOf(address) → 0x70a08231.
  • Arguments follow, each padded to 32 bytes.
  • Contracts call each other with CALL, read-only STATICCALL, or DELEGATECALL (runs another contract’s code in the caller’s storage – the basis of proxy upgrade patterns).
EVM-Compatible Chains

Many blockchains and Layer-2 networks run the EVM, so the same Solidity contracts, wallets and tools work on them with little or no change. Examples include Layer-2 rollups such as Arbitrum, Optimism and Base, and other networks such as BNB Smart Chain, Polygon PoS and Avalanche C-Chain. This shared standard is a major reason for Ethereum’s large developer ecosystem.

Key Takeaways
  • The EVM is a deterministic, sandboxed, 256-bit stack machine replicated on every node.
  • Storage is permanent and costly; memory, stack and calldata are temporary and cheaper.
  • Calls are ABI-encoded: 4-byte selector + 32-byte padded arguments.
Question / Answer (15 questions)

Answer : The EVM is the runtime environment, present in every Ethereum node, that executes smart-contract bytecode. It deterministically computes the new state of Ethereum from the current state and the transactions in a block.

Answer : At any time Ethereum has a state (all accounts, balances, code and storage). Each block applies transactions through the EVM’s state-transition function to move from one valid state to the next.

Answer : Every node independently re-executes each transaction. If results could differ between nodes, they would compute different states and could not agree on the blockchain.

Answer : 256 bits. This size fits Keccak-256 hashes and the 256-bit numbers used in elliptic-curve cryptography, which are common in Ethereum operations.

Answer : The stack can hold up to 1,024 items, each 256 bits wide.

Answer : Memory is a temporary byte array that exists only during a single call and is cleared afterwards; it is relatively cheap. Storage is a permanent key–value store of 256-bit slots belonging to a contract and kept by every node; writing to it is the most expensive operation.

Answer : Calldata is the read-only input data sent with a transaction or message call, containing the function selector and encoded arguments.

Answer : Opcodes are the single-byte instructions the EVM executes. Examples: ADD (0x01), SSTORE (0x55) and CALL (0xF1).

Answer : PUSH1 0x03 pushes 3, PUSH1 0x05 pushes 5, and ADD (0x01) pops both and pushes their sum, so the stack ends with 8.

Answer : Take the function signature without spaces, e.g. transfer(address,uint256), hash it with Keccak-256 and keep the first 4 bytes – for this example 0xa9059cbb.

Answer : The Application Binary Interface defines how to encode function calls and arguments into calldata and how to decode return values and event logs, so off-chain applications and other contracts can interact with a contract correctly.

Answer : DELEGATECALL executes another contract’s code in the context of the calling contract, using the caller’s storage, balance and msg.sender. It enables libraries and proxy patterns for upgradeable contracts, but must be used carefully.

Answer : Logs are stored in transaction receipts for off-chain consumers and are not part of the state that the EVM can access during execution. Data needed later by contracts must be kept in storage.

Answer : REVERT stops execution, undoes all state changes made in the current call and returns an error message, while refunding remaining gas (gas used up to that point is still charged).

Answer : Blockchains or Layer-2 networks that implement the EVM, so Ethereum smart contracts and tools work on them with little or no change. Examples include Arbitrum and BNB Smart Chain.
MCQ Test : Check Your Understanding (5 questions)

Select an option and press Check Answer, or answer all questions and press Submit Test to see your score. Each answer comes with an explanation.

Q1. Which EVM data area is PERMANENT and most expensive to write?

Show Answer

Correct Answer : D. Storage
Storage persists on-chain and is replicated by every node.

Q2. The function selector for transfer(address,uint256) is:

Show Answer

Correct Answer : A. 0xa9059cbb
First 4 bytes of keccak256("transfer(address,uint256)") = 0xa9059cbb.

Q3. The EVM word size is:

Show Answer

Correct Answer : C. 256 bits
The EVM operates on 256-bit words.

Q4. Which opcode runs another contract’s code using the CALLER’s storage?

Show Answer

Correct Answer : B. DELEGATECALL
DELEGATECALL executes external code in the caller’s context.

Q5. Bytecode 60 03 60 05 01 leaves which value on the stack?

Show Answer

Correct Answer : A. 8
PUSH1 3, PUSH1 5, ADD → 8.

Bibliography/References

The notes, questions and MCQs on this page are original explanations written for this course. The works listed below were consulted and are cited for attribution and further reading. Product names, logos and trademarks belong to their respective owners. Short quotations, where used, are marked and attributed.

  1. ethereum.org. (n.d.). Ethereum Virtual Machine (EVM). Ethereum Developer Documentation. https://ethereum.org/en/developers/docs/evm/
  2. ethereum.org. (n.d.). Opcodes for the EVM. Ethereum Developer Documentation. https://ethereum.org/en/developers/docs/evm/opcodes/
  3. Wood, G. Ethereum: A Secure Decentralised Generalised Transaction Ledger (Ethereum Yellow Paper). https://ethereum.github.io/yellowpaper/paper.pdf
  4. Crytic (Trail of Bits). (n.d.). evm-opcodes: Ethereum VM (EVM) opcodes and instruction reference. GitHub. https://github.com/crytic/evm-opcodes
  5. Solidity Team. (n.d.). Solidity Documentation. https://docs.soliditylang.org/
  6. Antonopoulos, A. M., & Wood, G. (2018). Mastering Ethereum: Building Smart Contracts and DApps. O’Reilly Media.