Try it Live
Run Bytecode examples in the interactive playground
Conceptual Guide - For API reference and method documentation, see Bytecode API.
Structure
Bytecode is a sequence of bytes where each byte (or sequence of bytes) represents either:- An opcode - Instruction telling the EVM what to do (ADD, PUSH1, JUMP, etc.)
- Data - Constant values embedded after PUSH opcodes
Parsing Bytecode
The main challenge: PUSH1-PUSH32 opcodes (0x60-0x7f) are followed by 1-32 bytes of data that are NOT opcodes.- TypeScript (Correct)
- Naive Parsing ❌
Analyzing Structure
Useanalyze() for complete bytecode analysis:
- TypeScript
What analyze() Returns
Jump Destinations
JUMPDEST (0x5b) markers indicate valid jump targets. The EVM validates that JUMP/JUMPI only target JUMPDEST opcodes:- TypeScript
Disassembly
UseformatInstructions() to disassemble bytecode to human-readable strings:
- TypeScript
Complete Example: ADD Operation
Here’s bytecode that adds 5 + 3 and returns the result:- TypeScript
Execution Flow
Parsing Individual Instructions
UseparseInstructions() for detailed instruction data:
- TypeScript
Metadata Detection
Solidity compilers append metadata (typically 50-100 bytes) containing compiler version and IPFS hash:- TypeScript
hasMetadata() and stripMetadata().
Extracting Runtime Code
Deployment bytecode includes initialization code that runs once. Extract just the runtime portion:- TypeScript
extractRuntime().
EVM Instructions
The EVM has ~140 single-byte opcodes organized by category:- Arithmetic & Logic - ADD, MUL, SUB, DIV, AND, OR, XOR
- Storage - SLOAD, SSTORE (persistent contract storage)
- Memory - MLOAD, MSTORE (temporary execution data)
- Control Flow - JUMP, JUMPI, JUMPDEST (loops, conditionals)
- Contract Calls - CALL, DELEGATECALL, STATICCALL
- Contract Creation - CREATE, CREATE2
- System - SELFDESTRUCT, REVERT
PUSH Instructions
PUSH1-PUSH32 (0x60-0x7f) embed 1-32 bytes of immediate data:Validation
Usevalidate() to check bytecode structure:
- TypeScript
Resources
- evm.codes - Interactive EVM opcode reference with gas costs
- wolflo/evm-opcodes - Comprehensive opcode reference
- Ethereum Yellow Paper - Formal EVM specification (Appendix H)
- Opcode Reference - Tevm’s opcode documentation
Next Steps
- Overview - Type definition and API reference
- analyze() - Complete bytecode analysis
- parseInstructions() - Parse individual instructions
- formatInstructions() - Disassemble to strings

