Q&A-18Walk me through how the EVM executes a contract call at the opcode level, from the function selector dispatch to returning data to the caller.
Answer
The 90-second version. Assume a Solidity 0.8.x contract called with CALL from an EOA or another contract. The EVM has no concept of functions: it sees calldata bytes and a program counter. The selector, the argument decoding and the return encoding are all bytecode the compiler emitted. The path is: the caller assembles selector || abi.encode(args) and CALLs; the target's runtime code starts at PC 0 with a dispatcher; the dispatcher matches the selector and JUMPIs to the body; the body reads args from calldata, computes, writes results to memory, and RETURN hands back a byte range; the caller reads it with RETURNDATASIZE/RETURNDATACOPY.
Dispatch. The prologue extracts the top 4 bytes:
PUSH1 0x00 ; offset 0
CALLDATALOAD ; 32 bytes from calldata[0..32), zero-padded past the end
PUSH1 0xE0 ; 224
SHR ; >> 224 leaves the high 4 bytes = selectorThen a linear scan, one block per external function:
DUP1
PUSH4 0xa9059cbb ; transfer(address,uint256)
EQ
PUSH2 0x00XX ; destination PC of the body
JUMPINo match falls through to the fallback, or reverts with no data. Each candidate costs about 22 gas (DUP1 3 + PUSH4 3 + EQ 3 + PUSH2 3 + JUMPI 10), so a 10-function contract spends ~220 gas worst case before any work.
Arguments. Static types are read in place: for transfer(address,uint256), to is CALLDATALOAD(0x04) and amount is CALLDATALOAD(0x24). Dynamic types (bytes, string, arrays) put a 32-byte offset in the head; the decoder follows it to a length-prefixed tail and copies with CALLDATACOPY. Memory holds dynamic data, structs and spilled locals; the stack holds 256-bit words, capped at 1024, with DUP/SWAP reaching only 16 deep.
Return. The body writes results to memory and returns a range: a bool is PUSH1 0x01 PUSH1 0x00 MSTORE PUSH1 0x20 PUSH1 0x00 RETURN. The caller then does RETURNDATASIZE / RETURNDATACOPY.
Trade-offs, with the deciding criterion.
- Compiler dispatcher vs hand-written router. Solidity emits a linear scan; a binary-search or jump-table router (Seaport, EIP-2535 Diamond) cuts per-call dispatch cost, but you own ABI decoding and collision safety. Flip: under ~10-15 external functions the linear scan is a rounding error and the audit cost of a custom router dominates; above that, or when the contract is hit thousands of times per block, the router pays for itself. A proxy adds a cold DELEGATECALL (2600 gas) plus the implementation's own dispatch on every call.
- Calldata vs memory for arguments. Calldata is read-only and needs no copy; memory is mutable but the compiler copies dynamic args in. Flip: the moment the function mutates, slices, or forwards the data to an internal function taking memory, you pay the copy anyway, then memory is the right home. Static args often never leave calldata.
- Stack vs memory. PUSH/DUP/SWAP/POP are 3 gas; MLOAD/MSTORE are 3 gas plus quadratic expansion. Flip: past ~16 live values DUP/SWAP can't reach them and the compiler spills, which is why "stack too deep" is a compile error, not a runtime one.
- Checking return data. Bounding the copy with RETURNDATASIZE costs a few gas. Flip: for a trusted callee with a known ABI, skip it; for routers, hooks and flash-loan callbacks, always bound it.
Failure modes. Selector collision: Solidity rejects two functions with the same selector in one contract, but proxies and hand-written routers can collide silently and route to the wrong body. Truncated calldata: CALLDATALOAD zero-pads past the end, so a short static argument decodes as zero rather than reverting. Memory expansion OOG: the cost is 3*words + words²/512, quadratic, so a callee that forces a huge allocation is a DoS vector. Return-data bomb: a malicious callee returns a huge blob; a caller that copies a size taken from the callee pays the expansion and OOGs.
| Data location | Access | Mutability | Gas |
|---|---|---|---|
| Calldata | CALLDATALOAD, CALLDATACOPY | Read-only | 3 gas + copy cost |
| Memory | MLOAD, MSTORE, MSTORE8 | Mutable | 3 gas + 3*words + words²/512 |
| Stack | PUSH, DUP, SWAP, POP | Mutable | 3 gas, 1024 deep |
| Storage | SLOAD, SSTORE | Mutable | SLOAD 2100 cold / 100 warm; SSTORE 20000 for 0→nonzero |
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.