or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

assembly.mddisassembly.mdfork-management.mdindex.mdinstruction-analysis.md
tile.json

tessl/pypi-pyevmasm

Ethereum Virtual Machine (EVM) assembler and disassembler library for working with EVM bytecode and assembly instructions

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/pyevmasm@0.2.x

To install, run

npx @tessl/cli install tessl/pypi-pyevmasm@0.2.0

index.mddocs/

PyEVMAsm

A 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.

Package Information

  • Package Name: pyevmasm
  • Language: Python
  • Installation: pip install pyevmasm
  • Requirements: Python >= 2.7 or Python >= 3.3

Core Imports

import pyevmasm

Common usage imports:

from pyevmasm import (
    assemble_hex, disassemble_hex,
    assemble_one, disassemble_one,
    assemble_all, disassemble_all,
    Instruction, instruction_tables,
    DEFAULT_FORK
)

Basic Usage

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}") # 0x80

Architecture

PyEVMAsm is built around several key components:

  • Instruction Class: Core representation of EVM instructions with complete metadata (opcode, operands, gas costs, stack effects, semantic analysis)
  • InstructionTable: Fork-specific lookup tables mapping opcodes/mnemonics to instruction definitions
  • Fork Support: Comprehensive support for all Ethereum hard forks from Frontier to Istanbul with proper opcode evolution
  • Assembly/Disassembly Engine: Bidirectional conversion between textual assembly and binary bytecode representations

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.

Capabilities

Assembly Operations

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: ...

Assembly Operations

Disassembly Operations

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: ...

Disassembly Operations

Instruction Analysis

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 properties

Instruction Analysis

Fork Management

Support 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: ...

Fork Management

Types

# 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): ...