Ethereum Virtual Machine (EVM) assembler and disassembler library for working with EVM bytecode and assembly instructions
npx @tessl/cli install tessl/pypi-pyevmasm@0.2.0A comprehensive Ethereum Virtual Machine (EVM) assembler and disassembler library that enables developers to work with EVM bytecode and assembly instructions. PyEVMAsm supports multiple Ethereum hard forks, provides detailed instruction metadata including gas costs and stack effects, and offers both a Python API and command-line interface for converting between EVM assembly language and bytecode.
pip install pyevmasmimport pyevmasmCommon usage imports:
from pyevmasm import (
assemble_hex, disassemble_hex,
assemble_one, disassemble_one,
assemble_all, disassemble_all,
Instruction, instruction_tables,
DEFAULT_FORK
)from pyevmasm import assemble_hex, disassemble_hex, disassemble_one
# Assemble EVM assembly to bytecode
bytecode = assemble_hex('''
PUSH1 0x80
PUSH1 0x40
MSTORE
PUSH1 0x4
CALLDATASIZE
LT
''')
print(bytecode) # "0x608060405260043610"
# Disassemble bytecode to assembly
assembly = disassemble_hex("0x608060405260043610")
print(assembly)
# PUSH1 0x80
# PUSH1 0x40
# MSTORE
# PUSH1 0x4
# CALLDATASIZE
# LT
# Analyze individual instructions
instruction = disassemble_one(b'\x60\x80')
print(f"Opcode: 0x{instruction.opcode:02x}") # 0x60
print(f"Name: {instruction.name}") # PUSH1
print(f"Gas cost: {instruction.fee}") # 3
print(f"Operand: 0x{instruction.operand:x}") # 0x80PyEVMAsm is built around several key components:
The library provides both high-level convenience functions for common use cases and low-level access to instruction metadata for detailed EVM analysis and tooling development.
Convert EVM assembly language to bytecode with support for individual instructions, multiple instructions, and complete programs. Includes both binary and hexadecimal output formats.
def assemble_one(asmcode: str, pc: int = 0, fork: str = DEFAULT_FORK) -> Instruction: ...
def assemble_all(asmcode: str, pc: int = 0, fork: str = DEFAULT_FORK): ... # generator
def assemble_hex(asmcode: str, pc: int = 0, fork: str = DEFAULT_FORK) -> str: ...Convert EVM bytecode to assembly language with support for various input formats (bytes, hex strings, iterators) and detailed instruction analysis.
def disassemble_one(bytecode, pc: int = 0, fork: str = DEFAULT_FORK) -> Instruction: ...
def disassemble_all(bytecode, pc: int = 0, fork: str = DEFAULT_FORK): ... # generator
def disassemble_hex(bytecode: str, pc: int = 0, fork: str = DEFAULT_FORK) -> str: ...Rich instruction metadata and semantic analysis capabilities for EVM instruction inspection, including stack effects, memory/storage access patterns, and control flow analysis.
class Instruction:
opcode: int
name: str
operand: int
pops: int
pushes: int
fee: int
# ... 20+ additional propertiesSupport for all Ethereum hard forks with proper opcode evolution, gas cost updates, and block number to fork conversion utilities.
instruction_tables: dict # Fork name -> InstructionTable mappings
def block_to_fork(block_number: int) -> str: ...# Core instruction representation
class Instruction:
def __init__(self, opcode: int, name: str, operand_size: int,
pops: int, pushes: int, fee: int, description: str,
operand: int = None, pc: int = 0): ...
# Fork-specific instruction lookup
class InstructionTable:
def __getitem__(self, key) -> Instruction: ... # key: int (opcode) or str (mnemonic)
def get(self, key, default=None) -> Instruction: ...
def __contains__(self, key) -> bool: ...
def __iter__(self): ...
# Exception types
class UnknownMnemonicError(Exception): ...
class UnknownOpcodeError(Exception): ...
class AssembleError(Exception): ...
class ParseError(Exception): ...