> module
std::re::asm::x86
x86 and x64 disassembly and assembly
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
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 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) -> strFormat one instruction as text. syntax is "masm" (the default), "intel", "nasm" or "gas" (also "att"); anything else raises an error.
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.
> types
An opaque value; see the functions that return it.
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.
The exact encoding, such as "Mov_r64_rm64"; the name of its Code variant.
How the instruction affects control flow: "Next", "UnconditionalBranch", "ConditionalBranch", "Call", "Return", "IndirectBranch", "IndirectCall", "Interrupt" and so on.
Format the instruction, as x86::format(insn, syntax) does.
instruction.has_lock_prefix() -> boolWhether it has a LOCK prefix.
instruction.has_rep_prefix() -> boolWhether it has a REP (or REPE) prefix, byte F3.
instruction.has_repe_prefix() -> boolWhether it has a REPE (or REP) prefix, byte F3.
instruction.has_repne_prefix() -> boolWhether it has a REPNE prefix, byte F2.
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.
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() -> boolWhether the bytes didn't decode to a valid instruction.
instruction.is_ip_rel_memory_operand() -> boolWhether the memory operand is RIP- or EIP-relative.
instruction.is_privileged() -> boolWhether it needs ring 0 (CPL 0) or I/O privilege, like HLT, IN or CLI.
instruction.is_stack_instruction() -> boolWhether it changes the stack pointer by pushing or popping, like PUSH, POP or CALL.
The memory operand's displacement as a 64-bit value. For an RIP-relative operand this is already the absolute address.
The segment register the memory operand actually uses, including the default one, such as "DS" or "SS".
The size of the memory location, such as "UInt32" or "QwordOffset".
The target address of a near CALL, JMP or Jcc, or 0 for other instructions.
The kind of operand n, such as "Register", "Memory", "Immediate32" or "NearBranch64".
The register of operand n, or "None" if it isn't a register operand.
The segment override prefix, such as "FS", or "None" if there isn't one.
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(())
}