Skip to main content

Try it Live

Run Opcode examples in the interactive playground
Conceptual Guide - For API reference and method documentation, see Opcode API.
EVM opcodes are the low-level machine instructions that smart contracts execute on the Ethereum Virtual Machine. This guide teaches opcode fundamentals using Tevm.

What Are Opcodes?

Opcodes are single-byte instructions (0x00-0xFF) that tell the EVM what to do. The EVM has ~140 unique opcodes organized by category:
  • Arithmetic - ADD, MUL, SUB, DIV, MOD
  • Logic - AND, OR, XOR, NOT, LT, GT, EQ
  • Storage - SLOAD, SSTORE (persistent contract storage)
  • Memory - MLOAD, MSTORE (temporary execution data)
  • Control Flow - JUMP, JUMPI, JUMPDEST (loops, conditionals)
  • Calls - CALL, DELEGATECALL, STATICCALL, CREATE
  • System - ADDRESS, CALLER, CALLVALUE, RETURN, REVERT

Stack Machine Architecture

The EVM is a stack machine - all operations push/pop values to/from a runtime stack:
Every opcode has stack inputs (items consumed) and stack outputs (items produced):

Opcode Categories

Arithmetic Operations

Binary operations pop 2 values, push 1 result:

Storage Operations

SLOAD reads from persistent contract storage, SSTORE writes:

Memory Operations

MLOAD reads from temporary memory, MSTORE writes:

Control Flow

JUMP and JUMPI control program flow. Jump targets must be JUMPDEST (0x5B):

External Calls

CALL invokes other contracts:

Gas Costs

Every opcode has a base gas cost. Some have dynamic costs:
Gas costs changed across hardforks. EIP-2929 (Berlin) introduced warm/cold access costs for storage and contract calls.

PUSH Instructions

PUSH1-PUSH32 (0x60-0x7F) embed immediate data in bytecode:
PUSH instruction data is NOT executable. Parsing must skip immediate bytes or you’ll misinterpret data as opcodes.

Stack Depth Limit

The EVM stack has a 1024-item maximum. Stack overflow causes execution to fail:

Common Patterns

Function Dispatch

Contracts check function selectors (first 4 bytes of calldata):

Storage Access

Reading and modifying storage:

Memory Layout

Common memory usage pattern:

Complete Example: Return Value

Contract that returns the value 42:

Resources

Next Steps