Open this lesson in your favourite AI. It'll walk you through the why, explain the demo, and quiz you on the try-it list.
Every contract on Ethereum executes as a sequence of opcodes operating on a 1024-deep stack of 256-bit words. Understanding this model is the difference between treating Solidity as magic and actually predicting what the compiler will emit. Once you can read opcodes, gas costs, security pitfalls, and optimization opportunities all stop being abstract.
A minimal Solidity function compiles to a small sequence of stack ops. Read the disassembly.
a + b when both are uint256 — confirm it's ADD (0x01), not SAFEMATH calls (post-0.8 the compiler emits a CALL? No — it uses arithmetic plus a JUMPI overflow check).forge inspect <Contract> bytecode on a one-function contract and locate the function selector (first 4 bytes after the dispatcher table).Use these three in order. Each builds on the one before.
In one paragraph, explain what the EVM stack machine is like I'm new to it — how a 1024-deep stack works and why opcodes pop and push.
Walk me through how a single Solidity function call becomes a sequence of opcodes — dispatcher, function body, return.
Given a contract that hits a 'stack too deep' compile error, explain mechanically why this happens and the two strategies (struct packing vs. memory variables) for resolving it.
// Solidity:
// function add(uint a, uint b) public pure returns (uint) { return a + b; }
//
// Compiles to (selected ops):
PUSH1 0x40 // free memory pointer
MLOAD // load it
CALLDATALOAD 0x04 // load 'a' from calldata
CALLDATALOAD 0x24 // load 'b' from calldata
ADD // stack top = a + b
PUSH1 0x40 // memory ptr
MSTORE // write result to memory
PUSH1 0x20 // length 32
PUSH1 0x40 // offset
RETURN // return memory[offset..offset+length]