Pacific Dial

ethereum virtual machine execution

How Ethereum Virtual Machine Execution Works: Everything You Need to Know

June 12, 2026 By Hayden Hutchins

Introduction to the Ethereum Virtual Machine

The Ethereum Virtual Machine (EVM) is the runtime environment that executes smart contracts on the Ethereum blockchain. It acts as a global, decentralized computer where every node runs the same set of instructions to achieve consensus. Unlike traditional virtual machines, the EVM is sandboxed, deterministic, and operates on a state-transition model. Every transaction triggers a sequence of EVM operations, which are priced in gas to prevent infinite loops and resource abuse. Understanding how EVM execution works is essential for developers building decentralized applications (dApps), auditors reviewing smart contract security, and traders analyzing on-chain behavior.

The EVM is not a physical machine but a logical construct maintained by thousands of ethereum clients. It processes bytecode—a low-level language compiled from high-level Solidity or Vyper code. Each bytecode instruction, called an opcode, performs a specific operation like arithmetic, storage access, or contract calls. The EVM's stack-based architecture, 256-bit word size, and gas metering system create a unique execution environment that differs sharply from conventional CPUs.

Core Architecture: Stack, Memory, and Storage

The EVM operates with three distinct data areas, each with its own cost and persistence characteristics.

  • Stack: A last-in-first-out (LIFO) data structure with a maximum depth of 1024 elements. Each element is a 256-bit word. All arithmetic, logical, and control flow operations operate on the stack. For example, the ADD opcode pops two words, computes their sum, and pushes the result. Stack overflow or underflow causes an immediate exception.
  • Memory: A linear, byte-addressable array that grows dynamically. Memory is used for temporary data during execution (e.g., constructing return values or hash inputs). It is cleared after each external call. Gas costs for memory expansion scale quadratically—the first 256 bytes cost 3 gas per word, with additional costs increasing by a factor of every 512 bytes.
  • Storage: A persistent key-value store that maps 256-bit keys to 256-bit values. Storage is part of the blockchain state—it survives after execution and is the most expensive resource to use. A single SSTORE opcode that changes a zero to non-zero costs 20,000 gas, while clearing a slot refunds 15,000 gas (though refunds are capped at 50% of total gas used since the London upgrade).

Execution begins by loading the contract bytecode into memory, initializing the stack to empty, and setting the program counter (PC) to zero. The EVM then fetches the opcode at the current PC, decodes it, executes the operation, and advances the PC. This cycle repeats until execution halts via STOP, RETURN, REVERT, or an out-of-gas exception.

Gas Model and Opcode Execution

Gas is the fundamental unit of computational effort in the EVM. Every opcode has a fixed or dynamic gas cost, defined in the Ethereum Yellow Paper and adjusted through Ethereum Improvement Proposals (EIPs). The total gas consumed by a transaction is the sum of the base fee (21000 gas for a simple transfer), calldata costs (16 gas per zero byte, 4 gas per non-zero byte), and opcode execution costs.

Key opcode categories and their gas costs:

  1. Arithmetic and Logic: ADD, SUB, MUL, DIV, AND, OR, XOR cost 3–5 gas each. Exponentiation (EXP) costs 10 gas plus 50 gas per byte in the exponent.
  2. Environmental Information: ADDRESS, BALANCE, CALLER, TIMESTAMP cost 2–700 gas. BALANCE is particularly expensive (700 gas) because it requires a state lookup.
  3. Memory and Storage: MLOAD (3 gas + memory expansion), MSTORE (3 gas + memory expansion), SLOAD (100 gas after EIP-2929), SSTORE (varies: 20,000 gas for warm writes to cold slots, 100 gas for warm updates).
  4. Control Flow: JUMP, JUMPI cost 8 gas each. JUMPDEST marks valid jump destinations and costs 1 gas.
  5. External Calls: CALL, STATICCALL, DELEGATECALL cost 700 gas base plus additional gas forwarded to the callee. These opcodes pass 63/64 of the remaining gas to the child context, as defined by EIP-150.

Gas is deducted before execution. If a transaction runs out of gas mid-execution, all state changes are reverted, but the gas spent is still consumed by the miner. This "pay-to-execute" model prevents denial-of-service attacks and incentivizes efficient code. Developers must carefully estimate gas limits and optimize storage-heavy patterns to avoid failed transactions.

State Transition and Contract Interaction

EVM execution is a function of the global state: σ' = EVM(σ, T), where σ is the current world state (account balances, storage, nonces, and code hashes) and T is a transaction. The transition applies the following steps:

  1. Validate the transaction signature, nonce, and sender balance.
  2. Deduct the intrinsic gas cost (21000 + calldata costs) from the sender's balance.
  3. Increment the sender's nonce.
  4. If the transaction targets a contract address, load its code and execute it within the EVM. If it targets a non-existent address (contract creation), deploy the init code.
  5. During execution, all state modifications (balance transfers, storage writes, logs) are recorded in a journal. If any exception occurs (invalid opcode, stack underflow, out-of-gas), the journal is discarded and state reverts to its pre-transaction state.
  6. After successful execution, the remaining gas is refunded to the sender (minus the base fee, which is burned under EIP-1559).

Contracts interact through message calls. When contract A calls contract B, the EVM creates a new execution context with its own stack, memory, and program counter. The callee receives a portion of the gas; if it runs out, the call reverts, but the caller can catch the failure via the return value. This "call-with-gas-limit" design enables composable protocols like DeFi lending and automated market makers (AMMs). For example, a flash loan may chain multiple calls across different contracts within a single transaction, leveraging the atomicity of EVM execution.

For traders and developers looking to leverage these atomic execution properties, understanding the EVM's behavior is foundational. Advanced Defi Trading Strategies often rely on precise knowledge of gas ordering, reentrancy guards, and cross-contract state synchronization to extract value from on-chain arbitrage or liquidation opportunities.

EVM Compatible Chains and Execution Differences

The EVM is not limited to Ethereum mainnet. Dozens of layer-2 solutions and sidechains implement EVM-compatible runtimes (e.g., Avalanche C-Chain, BNB Smart Chain, Polygon, Arbitrum, Optimism). While the opcode set and gas model are broadly similar, subtle differences exist:

  • Gas metering: Layer-2 rollups use compressed calldata (calldata costs are significantly lower on Arbitrum and Optimism because data is posted to L1 in bulk). Polygon uses a modified gas schedule with lower costs for certain opcodes.
  • Block gas limits: Ethereum mainnet has a target of 15M–30M gas per block. BNB Smart Chain uses 140M, allowing much larger transactions. This affects the feasibility of complex multi-call strategies.
  • Precompiled contracts: Some chains add custom precompiles (e.g., BNB Chain's BNB-256 precompile for BNB-specific operations). These are not available on Ethereum mainnet.

Despite these variations, the core execution model remains identical. A smart contract compiled for Ethereum will run on any EVM-compatible chain without modification, as long as the chain supports the same precompiles and block-level opcodes (e.g., CHAINID changed semantics in EIP-1344 but is widely implemented). This portability is the key reason why the EVM has become the dominant smart contract platform—over 80% of all dApps run on EVM-based chains.

For a deeper dive into how the Ethereum Virtual Machine execution model enables trustless computation and composable contracts, many resources examine the interplay between gas optimization, opcode costs, and state access patterns.

Practical Implications for Developers and Auditors

For anyone writing or reviewing smart contracts, understanding EVM execution nuances is non-negotiable.

Gas optimization: The most impactful pattern is minimizing storage operations. Writing to storage costs 20,000–22,100 gas, while memory operations cost 3 gas per word. Using memory-based accumulators and computing final values before writing to storage can reduce gas by 50% or more. Packing variables into fewer 256-bit slots (e.g., using uint128 for two variables in one slot) cuts SLOAD/SSTORE costs proportionally.

Security implications: The stack-based architecture makes certain vulnerabilities easier to miss. For example, unchecked external calls can result in reentrancy, where a malicious contract calls back into the original contract before state updates are finalized. The EVM's "call" opcode gives the callee a fresh stack, but the caller's state remains partially updated. This is why the Checks-Effects-Interactions pattern is mandatory: all state changes must occur before any external call.

Bug detection: Static analysis tools (Slither, Mythril) simulate EVM execution to trace control flow and data dependencies. They flag out-of-gas conditions, integer overflows (the EVM's 256-bit arithmetic wraps unless using SafeMath), and dangerous delegatecall usage. Dynamic fuzzers (Echidna, Foundry) generate random transaction sequences to force edge cases in EVM execution.

MEV and transaction ordering: Since the EVM executes transactions sequentially within a block, miners (or searchers) can reorder transactions to extract value. This is known as maximal extractable value (MEV). Understanding how the EVM orders opcodes within a transaction (e.g., the precise gas cost of each operation) allows sophisticated actors to submit bundles that frontrun or sandwich trades. While controversial, this behavior is an inherent property of the EVM's deterministic execution model.

Conclusion

The EVM is a remarkably elegant design that balances decentralization, security, and expressiveness. Its stack-based architecture, gas metering, and state transition model create a deterministic runtime where every computation is auditable and reproducible. From basic token transfers to complex DeFi protocols, every on-chain action is a composition of opcodes executed by the EVM. By mastering how the EVM works—gas costs, memory hierarchy, call contexts, and state persistence—developers can write more efficient, secure contracts, and traders can devise strategies that exploit the precise mechanics of the world's most widely used smart contract platform.

Background Reading: ethereum virtual machine execution — Expert Guide

Understand Ethereum Virtual Machine execution in depth: opcodes, gas, stack architecture, memory, and state transitions for EVM-compatible chains.

Editor’s note: ethereum virtual machine execution — Expert Guide
Featured Resource

How Ethereum Virtual Machine Execution Works: Everything You Need to Know

Understand Ethereum Virtual Machine execution in depth: opcodes, gas, stack architecture, memory, and state transitions for EVM-compatible chains.

External Sources

H
Hayden Hutchins

Reader-funded updates