Overview
Control flow instructions manage program execution flow in the EVM. These opcodes enable conditional logic, loops, function calls, and execution termination with or without state changes. Unlike traditional architectures with unrestricted jumps, the EVM enforces strict jump validation through JUMPDEST markers to prevent arbitrary code execution and maintain security guarantees.Instructions
Execution Control
- STOP (0x00) - Halt execution successfully with no output
- RETURN (0xf3) - Halt execution and return output data
- REVERT (0xfd) - Halt execution, revert state changes, return error data (Byzantium+)
Jump Operations
- JUMP (0x56) - Unconditional jump to destination
- JUMPI (0x57) - Conditional jump based on stack value
- JUMPDEST (0x5b) - Valid jump destination marker
Program Counter
- PC (0x58) - Get current program counter value
Jump Validation
The EVM enforces strict jump validation to prevent arbitrary code execution: Valid Jump Requirements:- Destination must be a JUMPDEST opcode (0x5b)
- JUMPDEST must not be inside PUSH data
- Destination must be within bytecode bounds
InvalidJump error and halt execution.
State Changes
Successful Termination
STOP and RETURN:- Preserve all state changes
- Mark execution as stopped
- Return output data (RETURN only)
- Consume remaining gas
Failed Termination
REVERT (Byzantium+):- Revert all state changes in current call
- Mark execution as reverted
- Return error data to caller
- Refund remaining gas
- Only INVALID (0xfe) for reverting state
- No error data returned
- All gas consumed
Common Patterns
Function Call Pattern
Loop Pattern
Conditional Return
Gas Costs
| Opcode | Gas Cost | Notes |
|---|---|---|
| STOP | 0 | Free (execution halted) |
| JUMP | 8 | GasMidStep |
| JUMPI | 10 | GasSlowStep |
| PC | 2 | GasQuickStep |
| JUMPDEST | 1 | JumpdestGas |
| RETURN | Memory expansion | Dynamic based on output size |
| REVERT | Memory expansion | Dynamic based on error data size |
- RETURN/REVERT charge gas for memory expansion
- Cost depends on output offset + length
- Formula:
words^2 / 512 + 3 * words
Security Considerations
Jump Validation
CRITICAL: JUMP and JUMPI must target JUMPDEST:Dynamic Jumps
Dangerous Pattern:REVERT vs INVALID
REVERT (0xfd) - Byzantium+:- Refunds remaining gas
- Returns error data
- Graceful failure
- Use for: input validation, business logic checks
- Consumes all gas
- No error data
- Hard failure
- Use for: should-never-happen conditions
Reentrancy
Control flow opcodes don’t directly cause reentrancy, but improper state management around RETURN can:Compiler Behavior
Solidity Function Dispatch
Solidity generates jump tables for function dispatch:Loop Optimization
Modern Solidity optimizes loops with JUMPI:Hardfork Changes
| Hardfork | Changes |
|---|---|
| Frontier | STOP, JUMP, JUMPI, PC, JUMPDEST, RETURN |
| Byzantium | Added REVERT (EIP-140) - graceful reversion with gas refund |
- Only INVALID (0xfe) available
- Consumes all gas
- No error data
- Poor UX for failed transactions
- REVERT preferred over INVALID
- Refunds unused gas
- Returns error data via RETURN data
- Better UX and gas efficiency
References
- Yellow Paper - Section 9.4.3 (Jump Operations), 9.4.4 (Halting)
- EIP-140: REVERT instruction
- EVM Codes - Control Flow
- Solidity Docs - Assembly

