# The x86 instruction set

[Courses](https://asm-editor.specy.app/learn/courses.md) › [x86-64 assembly](https://asm-editor.specy.app/learn/courses/x86.md) › Introduction

Web page: https://asm-editor.specy.app/learn/courses/x86/introduction/the-x86-instruction-set

> Learn about mnemonics, register, immediate, and memory operands, and how NASM determines operand sizes. Check which operand forms each instruction accepts.

Overview of this topic: [Assembly basics](https://asm-editor.specy.app/learn/courses/assembly-basics/introduction/instruction-set.md)

The same topic in: [M68K](https://asm-editor.specy.app/learn/courses/m68k/introduction/the-m68k-instruction-set.md), [MIPS](https://asm-editor.specy.app/learn/courses/mips/introduction/the-mips-instruction-set.md), [RISC-V](https://asm-editor.specy.app/learn/courses/risc-v/introduction/the-risc-v-instruction-set.md), [Z80](https://asm-editor.specy.app/learn/courses/z80/introduction/the-z80-instruction-set.md)

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](https://asm-editor.specy.app/documentation/x86/instruction.md) 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:

```x86
    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:

```x86
    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`, or `r9d`.
- 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:

```x86
    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:

```x86
    mov [destination], [source] ; assembly error
```

Use a register as a temporary:

```x86
    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:

```x86
    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:

```x86
    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.

```x86
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
    syscall
```

`setg` 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`.

*Exercise: the code to start from, checked by the testcase after it:*

```x86
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
    syscall
```

*Testcase for the program above (JSON):*

```json
{
    "expectedRegisters": {
        "r10": "0x1122334455667788"
    },
    "expectedMemory": [
        {
            "type": "number-chunk",
            "address": "0x402008",
            "bytes": 8,
            "expected": ["0x1122334455667788"]
        },
        {
            "type": "number-chunk",
            "address": "0x402010",
            "bytes": 8,
            "expected": ["0xFFFFFFFFFFFF1234"]
        }
    ]
}
```

<details>
<summary>Show solution</summary>

*Solution:*

```x86
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
```

</details>

## Instructions in this Lecture

- x86-64: [`ret`](https://asm-editor.specy.app/documentation/x86/instruction/ret.md), [`inc`](https://asm-editor.specy.app/documentation/x86/instruction/inc.md), [`add`](https://asm-editor.specy.app/documentation/x86/instruction/add.md), [`imul`](https://asm-editor.specy.app/documentation/x86/instruction/imul.md), [`mov`](https://asm-editor.specy.app/documentation/x86/instruction/mov.md), [`cmp`](https://asm-editor.specy.app/documentation/x86/instruction/cmp.md), [`setg`](https://asm-editor.specy.app/documentation/x86/instruction/setg.md), [`seta`](https://asm-editor.specy.app/documentation/x86/instruction/seta.md), [`syscall`](https://asm-editor.specy.app/documentation/x86/instruction/syscall.md)

Previous: [Bytes, words, dwords and qwords](https://asm-editor.specy.app/learn/courses/x86/introduction/bytes-words-dwords-and-qwords.md)

Next: [Effective addresses](https://asm-editor.specy.app/learn/courses/x86/introduction/effective-addresses.md)
