wire / wire-format / x86-instruction

x86-64 instruction encoding (basics)

x86-64 instructions are variable-length (1-15 bytes). Structure: optional prefixes (1-4 bytes: REX, operand-size, address-size, segment, lock, repeat), opcode (1-3 bytes), ModR/M byte (1 byte: mod(2) + reg(3) + r/m(3)), optional SIB byte, optional displacement (1/2/4/8 bytes), optional immediate (1/2/4/8 bytes). REX prefix (0x40-0x4F) extends registers to 16 (r8-r15) and operand size to 64-bit.

wire-format kind wire-format status standard verification verified tier B wire-format@1

aka: x86-64 instruction encoding (basics)

wire format

spec: Intel 64 and IA-32 Architectures Software Developer's Manual

x86-64 instructions are variable-length (1-15 bytes). Structure: optional prefixes (1-4 bytes: REX, operand-size, address-size, segment, lock, repeat), opcode (1-3 bytes), ModR/M byte (1 byte: mod(2) + reg(3) + r/m(3)), optional SIB byte, optional displacement (1/2/4/8 bytes), optional immediate (1/2/4/8 bytes). REX prefix (0x40-0x4F) extends registers to 16 (r8-r15) and operand size to 64-bit.

fieldsizemeaning
Prefixes0-4 bytesREX (0x40-0x4F), operand-size override (0x66), address-size override (0x67), segment override (0x26/0x2E/0x36/0x3E/0x64/0x65), lock (0xF0), repeat (0xF2/F3).
REX prefix1 byte (0x40-0x4F)Bit 3 (W): 64-bit operand size. Bit 2 (R): extends ModR/M reg field. Bit 1 (X): extends SIB index. Bit 0 (B): extends ModR/M r/m or opcode reg.
Opcode1-3 bytesPrimary opcode. 1-byte (0x00-0xFF), 2-byte (0x0F + second), 3-byte (0x0F 0x38/0x3A + third).
ModR/M1 bytemod(2 bits) + reg/opcode(3 bits) + r/m(3 bits). mod=00: memory (no disp or disp32), 01: memory+disp8, 10: memory+disp32, 11: register.
SIB0 or 1 bytescale(2) + index(3) + base(3). Used for indexed addressing (e.g., [base + index*scale + disp]).
Displacement0/1/2/4/8 bytesMemory offset (signed, sign-extended).
Immediate0/1/2/4/8 bytesConstant value for the instruction.
Registers16 (64-bit)rax, rcx, rdx, rbx, rsp, rbp, rsi, rdi, r8-r15. Plus rip (instruction pointer), rflags (status). In 32-bit mode: eax, ecx, edx, ebx, esp, ebp, esi, edi.

example:

55 48 89 e5

x86-64: PUSH rbp; MOV rbp, rsp (function prologue)


provenance

agent: curl -H 'accept: application/json' wire.phall.io/wire-format/x86-instruction or /wire-format/x86-instruction.json