Two ideas sit underneath almost every 8086 instruction: how an operand names its data, and what the flags record about the last result. Addressing modes answer the first question. The flags register answers the second. This post covers both, because they are constantly used together: a comparison uses an addressing mode to fetch its operands, and a jump reads the flags the comparison left behind.
Segmented addresses: segment times sixteen plus offset
An 8086 address has two parts, a segment and an offset, and the physical address is the segment multiplied by 16 plus the offset. DS:1234h with DS equal to 1000h therefore addresses physical address 11234h. By default, BX, SI and DI use DS, while BP uses SS; a segment override prefix such as ES: or CS: changes the default for that one instruction. Forgetting the default segment of BP is a reliable source of bugs in stack-based code.
The addressing modes
| Mode | Example | What it means |
|---|---|---|
| Immediate | MOV AX, 1234h |
The value is in the instruction. |
| Register | MOV AX, BX |
The value is in a register. |
| Direct | MOV AX, [1234h] |
The value is at a fixed memory address. |
| Register indirect | MOV AX, [BX] |
The register holds the address. |
| Based | MOV AX, [BX+10] |
Base register plus a displacement. |
| Indexed | MOV AX, [SI+10] |
Index register plus a displacement. |
| Base plus index | MOV AX, [BX+SI+10] |
Base plus index plus displacement, the form used for arrays and tables. |
| Implied | STOSB, LOOP |
The operand is fixed by the instruction itself. |
Worked example: walking an array with base plus index
ORG 100h LEA BX, table ; BX = base address of the table MOV SI, 0 ; SI = index into the table MOV CX, 5 ; five entries to add MOV AX, 0 ; running total add_next: ADD AL, [BX+SI] ; fetch table[SI] and add it INC SI ; move to the next entry LOOP add_next ; AL now holds the sum of the five bytes. RET table DB 10, 20, 30, 40, 50
The same [BX+SI] pattern works for any table: change BX to point at a different table, or change SI to pick a different entry, and the instruction does not change. That separation of base and index is the whole point of the mode.
The flags register, bit by bit
| Flag | Bit | Set when |
|---|---|---|
| CF (carry) | 0 | An arithmetic result carries out of, or borrows into, the top bit. |
| PF (parity) | 2 | The low byte of the result has an even number of set bits. |
| AF (auxiliary carry) | 4 | There is a carry out of bit 3, used by decimal adjust instructions. |
| ZF (zero) | 6 | The result is zero. |
| SF (sign) | 7 | The top bit of the result is set, so the result looks negative as a signed value. |
| TF (trap) | 8 | Single-step debugging is enabled. |
| IF (interrupt) | 9 | Maskable hardware interrupts are enabled; cleared by CLI, set by STI. |
| DF (direction) | 10 | String instructions auto-decrement (set by STD) instead of auto-increment (cleared by CLD). |
| OF (overflow) | 11 | A signed result is too large for its destination size. |
Which instructions affect which flags
MOV affects no flags at all. ADD, SUB and CMP affect OF, SF, ZF, AF, PF and CF. INC and DEC affect all of those except CF. Shifts and rotates affect CF and OF in ways that depend on the count, and the logic instructions AND, OR, XOR and TEST clear CF and OF and set SF, ZF and PF from the result. The practical rule is to place the jump immediately after the instruction whose flags it reads, and to check the table whenever another instruction sits in between.
For the jumps that read these flags, see the JMP and LOOP post, and for the arithmetic that sets them, see ADD and CMP.
Further reading
- 8086 Instruction Set and Opcode Reference
- Emu8086 Home
- MOV in 8086 Assembly
- ADD and CMP in 8086 Assembly
- JMP and LOOP in 8086 Assembly
New to assembly? Start with Your First 8086 Program: Hello World, then return here.
