Core model

ISA (architecture) specifies instructions, encodings, registers, and observable behavior. Microarchitecture implements it: pipelines, caches, clock frequency, etc. x86 is a CISC ISA with complex addressing and arithmetic that can access memory; load-store RISC designs restrict memory access to loads and stores.

The programmer sees registers, condition flags, %rip (instruction pointer), and byte-addressable memory containing code, data, and the stack. Arrays and structures are just consecutive bytes.

AT&T syntax / grammar

mnemonic[suffix] source, destination
immediate := $constant                  # e.g. $42, $0x10
register  := %name                      # e.g. %rax, %eax
memory    := D(base,index,scale)         # effective address: D + base + index*scale

Destination comes last. The suffix specifies the data width, independently of the address width:

SuffixBytesExample register
b (byte)1%al
w (word)2%ax
l (long word)4%eax
q (quad word)8%rax

Registers

There are 16 general-purpose 64-bit registers. Smaller names refer to overlapping portions: %eax is the low 32 bits of %rax. Writing a 32-bit register zeros the upper 32 bits; 8/16-bit writes preserve the other bits. %rsp is the stack pointer; %rbp can serve as a frame pointer.

Moving data and calculating addresses

mov copies bits. It supports immediate → register/memory, register → register/memory, and memory → register; ordinary mov cannot copy memory → memory.

movq %rdi, %rax          # copy a pointer
movl (%rdi), %eax        # load 4 bytes from that pointer
movl %eax, 8(%rsi)       # store 4 bytes at address rsi + 8
  • In D(base,index,scale), displacement D is in bytes, and scale is 1, 2, 4, or 8. ()
    • Pseudocode for this general form Mem[Reg[Rb]+S*Reg[Ri]+ D]
  • Components can be omitted: (%rax), 8(%rbp), (%rax,%rcx,4), 8(,%rcx,4).
  • %rsp cannot be the index.
  • RIP-relative addressing uses forms such as label(%rip).

mov versus lea

movq 8(%rdi,%rsi,4), %rax loads memory at rdi + 4*rsi + 8. leaq 8(%rdi,%rsi,4), %rax computes that address/value, without reading memory or changing flags. The destination is a register. leacan be understood as a convenient integer arithmetic instruction that computes expressions using memory-addressing syntax without using extra arithmetic instructions

Integer arithmetic

Two-operand operations overwrite the destination; replace l with the appropriate width suffix.

InstructionEffect
addl S,D / subl S,DD = D + S / D = D - S
imull S,DLow 32 bits of D * S
andl S,D / orl S,D / xorl S,DBitwise AND / OR / XOR
sall k,D (also shll)Left shift
sarl k,D / shrl k,DRight shift: sign-fill / zero-fill
incl D / decl DIncrement / decrement
negl D / notl DArithmetic negation / bitwise complement

Shift counts use an immediate ($4) or %cl. Addition, subtraction, and the low product bits have the same bit-level result for signed and unsigned operands; flags and comparisons distinguish their interpretation.

Example

Further x86 instruction set

Inspecting machine code

gcc -O0 -S code.c produces assembly; the assembler produces .o; the linker resolves references into an executable. objdump -d file disassembles; GDB supports disassemble function and x/30xb function (30 bytes in hex). x86 instructions have variable byte lengths.

see GNU gcc Toolchain