Introduction to EVM
The Ethereum Virtual Machine (EVM) is a stack-based virtual machine that executes bytecode instructions to modify the blockchain state. It operates within a defined execution environment and handles 140 instructions across 11 categories.
Key Insight:
The EVM is a quasi-Turing complete machine due to gas limitations, which cap the total computational operations. Its logic is formalized by the code execution function (Ξ):\((\boldsymbol{\sigma}', g', A, \mathbf{o}) \equiv \Xi(\boldsymbol{\sigma}, g, I)\)
Here, σ = state, g = available gas, I = execution environment; σ' = updated state, g' = remaining gas, A = accrued substate, o = output.
EVM Implementation Logic
Core Definitions
- Environment, instruction set, and EVM state specifications.
Contract Bytecode Execution
- Parses bytecode into instructions and executes them sequentially.
Steps:
- Instruction Execution: Decodes, charges gas, and executes.
- Stack Operations: Manages 256-bit words (max depth: 1024).
- Memory Operations: Handles reads/writes and allocations.
- Control Flow: Handles jumps (
JUMP,JUMPI) and termination (STOP,RETURN).
State Modifications
- Processes gas payments, cleans empty accounts, and updates storage.
Core Definitions
Instruction Set
EVM supports 140 instructions, including:
- Arithmetic:
ADD,SUB,MUL,EXP(gas:VeryLowTier). - Bitwise Logic:
AND,OR,SHL(EIP-145). - System Operations:
CREATE2(EIP-1014),DELEGATECALL.
Example: ADD Instruction
- Bytecode:
0x01 - Stack: Pops 2 operands, pushes 1 result.
- Gas Cost: 3 (
VeryLowTier).
ADD(0x01, 2, 1, VeryLowTier)Execution Environment (I)
Ia: Executing account address.Id: Input data (e.g., transaction payload).Iv: Transferred value (wei).IH: Current block header.
Note: For contract calls,Ib= target contract code; for creations,Ib= init code.
EVM State (μ)
- Memory: Byte-addressable, zero-initialized (
μm). - Stack: 1024-depth, 256-bit words (
μs). - Gas:
μg = gasLimit - gasUsed. - Program Counter: Starts at 0 (
μpc).
Transaction Substate
- Self-Destruct Set (
As): Accounts marked for deletion. - Logs (
Al): Indexable events for DApps. - Gas Refunds (
Ar): Reimbursed for storage cleanup.
FAQs
Q1: How does gas limit EVM computations?
A1: Each instruction consumes gas (μg). Exhaustion halts execution, preventing infinite loops.
Q2: What’s the role of CREATE2?
A2: Predictably generates contract addresses, enabling counterfactual deployments.
Q3: Why is the stack depth limited?
A3: Prevents resource exhaustion attacks (max: 1024 items).
👉 Explore EVM Opcodes in Depth
👉 Master Smart Contract Development
Keywords: Ethereum Virtual Machine, EVM opcodes, gas computation, smart contracts, bytecode execution, stack-based VM, EIP standards, transaction substate.
**Notes**:
- Structured for SEO with multi-level headings and keyword integration.
- Removed ads/sensitive content; retained academic tone.
- Added FAQs and anchor texts per guidelines.
- Expanded definitions for clarity (e.g., gas mechanics).