xur_

> module

std::re::asm::x86

x86 and x64 disassembly and assembly

use std::re::asm::x86; feature re-asm in the browser

std::re::asm::x86 decodes, builds, encodes and formats x86 machine code in 16, 32 and 64-bit modes. decode turns bytes into a list of Instructions; insn builds one from a Code and operands; encode turns a list back into bytes.

Backed by iced-x86. Names that come back as strings (mnemonics, registers, operand kinds) are iced-x86's enum variant names, such as "Mov", "RAX" or "Register".

Code names every instruction encoding (e.g. Code::Mov_r32_imm32) and Register every register (e.g. Register::EAX). Both are integer-backed enums. Printing an Instruction formats it in MASM syntax.

> functions

# decode(bitness: i64, data: bytes, ip?: i64) -> list<Instruction>

Disassemble all of data as if it were loaded at ip (default 0). bitness is 16, 32 or 64; anything else raises an error. Bytes that don't decode give an instruction whose is_invalid() is true.

# encode(bitness: i64, insns: list<Instruction>, ip?: i64) -> Result<bytes, str>

Encode insns one after another, the first at ip (default 0), so relative branches are fixed up for their new addresses. Returns Err with iced-x86's message if an instruction can't be encoded.

# format(insn: Instruction, syntax?: str) -> str

Format one instruction as text. syntax is "masm" (the default), "intel", "nasm" or "gas" (also "att"); anything else raises an error.

# insn(code: Code | str, ops?: Register | str | i64 | MemOp) -> Result<Instruction, str>

Build an instruction from a Code (the enum value, its integer, or its name such as "Mov_r64_imm64") and up to three operands. An operand is a Register value, a register name (any case), an integer immediate, or a memory operand from mem(). An unknown code or register name raises an error; operands that don't fit the code give Err.

# mem(base: str, args?: str | i64) -> MemOp

Build a memory operand from register names: mem(base) for [base], mem(base, disp) for [base+disp], or mem(base, index, scale, disp) for [base+index*scale+disp]. Other argument shapes raise an error.

> types

┌ struct Code

An opaque value; see the functions that return it.

┌ type Instruction

The wrapped instruction — `Clone` + `Facet` so it can live as a native value. `Display` delegates to iced-x86's MASM formatter so it shows as readable assembly when printed.

# instruction.code() -> str

The exact encoding, such as "Mov_r64_rm64"; the name of its Code variant.

# instruction.flow_control() -> str

How the instruction affects control flow: "Next", "UnconditionalBranch", "ConditionalBranch", "Call", "Return", "IndirectBranch", "IndirectCall", "Interrupt" and so on.

# instruction.format(syntax?: str) -> str

Format the instruction, as x86::format(insn, syntax) does.

# instruction.has_lock_prefix() -> bool

Whether it has a LOCK prefix.

# instruction.has_rep_prefix() -> bool

Whether it has a REP (or REPE) prefix, byte F3.

# instruction.has_repe_prefix() -> bool

Whether it has a REPE (or REP) prefix, byte F3.

# instruction.has_repne_prefix() -> bool

Whether it has a REPNE prefix, byte F2.

# instruction.immediate(n: u32) -> u64

The value of immediate operand n, sign- or zero-extended to 64 bits as the encoding says. Gives 0 for an operand that isn't an immediate.

# instruction.ip() -> u64

The address the instruction was decoded at.

# instruction.ip_rel_memory_address() -> u64

The absolute address an RIP- or EIP-relative memory operand refers to. Only meaningful when is_ip_rel_memory_operand() is true.

# instruction.is_invalid() -> bool

Whether the bytes didn't decode to a valid instruction.

# instruction.is_ip_rel_memory_operand() -> bool

Whether the memory operand is RIP- or EIP-relative.

# instruction.is_privileged() -> bool

Whether it needs ring 0 (CPL 0) or I/O privilege, like HLT, IN or CLI.

# instruction.is_stack_instruction() -> bool

Whether it changes the stack pointer by pushing or popping, like PUSH, POP or CALL.

# instruction.len() -> u64

Its length in bytes (0 for one built with insn()).

# instruction.memory_base() -> str

The base register of the memory operand, or "None".

# instruction.memory_displacement() -> u64

The memory operand's displacement as a 64-bit value. For an RIP-relative operand this is already the absolute address.

# instruction.memory_index() -> str

The index register of the memory operand, or "None".

# instruction.memory_index_scale() -> u32

The index scale: 1, 2, 4 or 8.

# instruction.memory_segment() -> str

The segment register the memory operand actually uses, including the default one, such as "DS" or "SS".

# instruction.memory_size() -> str

The size of the memory location, such as "UInt32" or "QwordOffset".

# instruction.mnemonic() -> str

The mnemonic, such as "Mov" or "Jne".

# instruction.near_branch_target() -> u64

The target address of a near CALL, JMP or Jcc, or 0 for other instructions.

# instruction.next_ip() -> u64

The address just after it: ip() + len() for a decoded instruction.

# instruction.op_count() -> u32

How many operands it has.

# instruction.op_kind(n: u32) -> str

The kind of operand n, such as "Register", "Memory", "Immediate32" or "NearBranch64".

# instruction.op_register(n: u32) -> str

The register of operand n, or "None" if it isn't a register operand.

# instruction.segment_prefix() -> str

The segment override prefix, such as "FS", or "None" if there isn't one.

# instruction.stack_pointer_increment() -> i32

How many bytes it adds to the stack pointer: -8 for a 64-bit PUSH, 8 for a POP, 0 if it doesn't push or pop.

┌ struct Register

An opaque value; see the functions that return it.

> examples

use std::io;
use std::re::asm::x86;

fn main() {
    let code = b"\\x55\\x48\\x89\\xe5\\xc3";
    for insn in x86::decode(64, code, 0x1000) {
        io::println("{}  {}", insn.ip().to_hex(), insn.format("intel"));
    }
}
use std::re::asm::x86;
use std::mem;

fn main() -> Result<()> {
    let mov = x86::insn(
        x86::Code::Mov_r32_imm32,
        x86::Register::EAX, 42)?;
    let ret = x86::insn(x86::Code::Retnq)?;
    let code = x86::encode(64, [mov, ret], 0)?;
    let page: *fn() = mem::alloc_page(4096);
    mem::write_bytes(page, code);
    mem::protect(page, 4096,
        mem::Protection::Read
        | mem::Protection::Execute);
    let result = page();
    mem::free_page(page, 4096);
    Ok(())
}