The x86-64 instruction set
The overview of this topic is in Assembly basics. The same topic in M68K, MIPS, RISC-V, Z80.
x86 has a large instruction set, but useful programs begin with a small core. The instructions in that core move values, calculate, compare, and choose which instruction runs next. Each instruction has a short name called a mnemonic, followed by the operands it uses.
The assembler turns these instructions into machine-code bytes. The instruction reference lists the operand forms that NASM accepts for each mnemonic.
Mnemonics and operands
An instruction can have zero, one, two, or occasionally more explicit operands:
ret ; zero explicit operands
inc rax ; one
add rax, rbx ; two
imul rax, rbx, 10 ; three
For common two-operand data instructions in NASM syntax, the destination comes first:
mov rax, 5 ; rax receives 5
add rax, rbx ; rax receives rax + rbx
Some two-operand instructions do not write to either operand. For example, cmp rax, rbx reads both
registers and records their relationship in the flags. It leaves both registers unchanged. The
instruction reference tells you how many operands each instruction takes, which operands it accepts,
and what it writes.
Three common kinds of operand
The instructions in this course often use:
- A register, such as
rax,bl, orr9d. - An immediate, a value written directly in the instruction, such as
5,0x40, or'A'. - A memory reference in square brackets, such as
[total]or[rbx].
In the following two-operand forms, at most one operand can be a memory reference:
mov rax, [source] ; memory to register
mov [destination], rax ; register to memory
mov qword [destination], 5 ; immediate to memory
NASM has no ordinary mov form from one memory location directly to another:
mov [destination], [source] ; assembly error
Use a register as a temporary:
mov rax, [source]
mov [destination], rax
This limit also applies to two-operand forms of add and cmp. Other instructions have their own
operand rules. Check the instruction reference to see which forms an instruction accepts.
Operand width selects the operation width
The operands usually tell NASM whether an instruction works on a byte, word, dword, or qword. A register name supplies that width:
mov al, 1 ; one byte
mov ax, 1 ; two bytes
mov eax, 1 ; four bytes
mov rax, 1 ; eight bytes
mov bl, [value] ; read one byte
mov rbx, [value] ; read eight bytes
When a memory operand is paired with a register, the register usually provides the memory width.
In mov [total], rax, NASM therefore knows to store eight bytes.
When you store an immediate in memory, there is no register to tell NASM the width. Write the size explicitly:
mov byte [total], 5 ; store one byte
mov word [total], 5 ; store two bytes
mov qword [total], 5 ; store eight bytes
Writing mov [total], 5 leaves the size ambiguous, so NASM rejects it. Each instruction also has
rules about operand widths and positions. NASM rejects an operand pair that does not match one of
the instruction's supported forms.
A map of the main families
Instruction mnemonics are easier to learn in small families:
| family | examples | purpose |
|---|---|---|
| moving values | mov, movzx, movsx, lea, xchg | transfer values or produce addresses |
| arithmetic | add, sub, inc, dec, neg, imul, div | calculate with integers |
| logic and bits | and, or, xor, not, test, shl, shr | work with bit patterns |
| comparison | cmp, test | record information for a later decision |
| control flow | jmp, conditional jumps, call, ret | choose which instruction runs next |
The table gives the broad purpose of each family. Individual instructions can have additional
behavior. For instance, lea calculates an address expression without reading memory, while mov
transfers a value. The instruction reference gives the precise behavior of each mnemonic.
One bit pattern, two comparisons
A comparison can treat the same bits as signed or unsigned. cmp left, right records the relation
between its operands in the flags. The instruction that checks those flags chooses which
interpretation to use.
default rel
global _start
section .text
_start:
mov r8, 0xAAAAAAAAAAAAAAAA
mov r9, 0xBBBBBBBBBBBBBBBB
mov rax, -1
mov rbx, 1
cmp rax, rbx
setg r8b ; signed: -1 > 1 is false, so write 0
seta r9b ; unsigned: 0xFFFF... > 1 is true, so write 1
mov rax, 60
mov rdi, 0
syscallsetg and seta each write a one-byte answer: 1 when the requested relation is true and 0 when it
is false. The full results are r8 = 0xAAAAAAAAAAAAAA00 and
r9 = 0xBBBBBBBBBBBBBB01. Both answers came from the same cmp; the condition selected the signed
or unsigned interpretation.
Your turn
source holds a qword. Load it into r10, then copy it into copy. Use the register as the
temporary because an ordinary two-operand mov cannot have two memory operands. Finally, store the
word 0x1234 at marker. That last store must change only the first two bytes, leaving its other
six bytes as 0xFF.
default rel
global _start
section .data
source: dq 0x1122334455667788
copy: dq 0
marker: dq 0xFFFFFFFFFFFFFFFF
section .text
_start:
; your code here
mov rax, 60
mov rdi, 0
syscallShow solution
default rel
global _start
section .data
source: dq 0x1122334455667788
copy: dq 0
marker: dq 0xFFFFFFFFFFFFFFFF
section .text
_start:
mov r10, [source]
mov [copy], r10
mov word [marker], 0x1234
mov rax, 60
mov rdi, 0
syscall