# Understanding EVM Machine State (μ): Building MiniEVM from the Yellow Paper ## The EVM's Machine State (μ) When the Ethereum Virtual Machine executes bytecode, it maintains a **machine state** (denoted as μ in the Yellow Paper). This state is the working memory of the EVM during execution. ### The Five Components of Machine State: 1. **μ_g** - Remaining gas - Tracks how much gas is left for execution - Prevents infinite loops and resource exhaustion 2. **μ_pc** - Program counter - Points to the current instruction in bytecode - Advances after each opcode execution 3. **μ_m** - Memory contents - Byte-addressable temporary storage - Expands dynamically during execution 4. **μ_i** - Active memory size - Tracks how many words (32 bytes) are in use - Used for gas calculation 5. **μ_s** - Stack contents - LIFO stack of 256-bit words - Maximum depth: 1024 items - Primary data structure for EVM operations ## Implementing Machine State in Go I'm building **MiniEVM** - a simplified EVM implementation in Go to learn the execution layer. Here's how I'm implementing machine state: ```go // VM holds code, program counter (PC), and gas tracking type VM struct { Code []byte // Bytecode to execute PC uint64 // μ_pc - Program counter Gas uint64 // μ_g - Remaining gas GasLimit uint64 // Maximum gas allowed // Stack (μ_s) and Memory (μ_m) coming next! } // Execution loop (simplified) func (vm *VM) Execute() error { for vm.HasMore() { opcode := vm.Fetch() // Read instruction at μ_pc // impl, repo coming soon } return nil } ``` ## machine state is crucial because: - **Every opcode** reads/writes from these components - **Gas costs** depend on memory expansion (μ_i) - **Stack operations** (μ_s) are the foundation of EVM computation - **Program counter** (μ_pc) controls execution flow ## Resources - [Ethereum Yellow Paper](https://ethereum.github.io/yellowpaper/paper.pdf) - Section 9 --- *Building MiniEVM one opcode at a time.*