1. The 8086 Programming Model
Definition – Programmer-visible registers
All named registers of the 8086 are 16 bits wide.
| Group | Registers | Typical role |
|---|---|---|
| General purpose | AX, BX, CX, DX | Accumulator, base pointer, counter, data / I/O port |
| Pointer & index | SP, BP, SI, DI | Stack pointer, base pointer, source index, destination index |
| Segment | CS, DS, SS, ES | Code, data, stack, extra segment |
| Control | IP, FLAGS | Instruction pointer, status/control bits |
Definition – 8-bit halves
AX, BX, CX, DX each expose two independent 8-bit halves:
AL= low byte (bits 0–7)AH= high byte (bits 8–15)
⚠️ SP, BP, SI, DI and the segment registers have no 8-bit halves.
2. The FLAGS Register
Definition – Status and control flags
| Flag | Bit | Type | Set to 1 when… |
|---|---|---|---|
| CF – Carry | 0 | Status | Unsigned result carried out / needed a borrow |
| PF – Parity | 2 | Status | Low byte of result has an even number of 1s |
| AF – Auxiliary carry | 4 | Status | Carry/borrow between bit 3 and bit 4 (BCD) |
| ZF – Zero | 6 | Status | Result is all zeros |
| SF – Sign | 7 | Status | MSB of the result is 1 |
| OF – Overflow | 11 | Status | Signed result does not fit |
| TF – Trap | 8 | Control | Single-step mode (debugging) |
| IF – Interrupt | 9 | Control | Maskable interrupts enabled |
| DF – Direction | 10 | Control | String operations move backward |
The remaining bits are reserved and complete the 16-bit register.
Which flag answers which question
| Question | Flag |
|---|---|
| Did an unsigned operation go out of range? | CF |
| Did a signed operation go out of range? | OF |
| Is the result zero / were the operands equal? | ZF |
| Is the result negative (as signed)? | SF |
3. A Basic Instruction Vocabulary
| Purpose | Instructions |
|---|---|
| Data transfer | MOV, PUSH, POP, XCHG |
| Arithmetic | ADD, ADC, SUB, SBB, INC, DEC |
| Logic | AND, OR, XOR, NOT, TEST |
| Control flow | JMP, Jcc, CALL, RET, LOOP |
| Shift & rotate | SHL, SHR, SAR, ROL, ROR |
| Processor control | CLC, STC, CLD, STD, NOP |
4. The MOV Instruction
Definition – MOV
MOV dest, src copies src into dest.
mov ax, 1234h ; immediate → register
mov bx, ax ; register → register
mov [result], ax ; register → memory
mov al, [inputByte] ; memory → register (byte)
mov ax, [wordValue] ; memory → register (word)Rules
- The source is not altered.
- Most forms do not change FLAGS.
- Operand widths must match (byte↔byte, word↔word).
- At most one memory operand.
⚠️ mov [A],[B] is illegal — memory-to-memory transfers must go through a register.
⚠️ An immediate cannot be moved directly into a segment register (mov ds,1200h is illegal) — see Exercise 2.
5. Operand Size
Definition – Byte vs word operations
| Size | Registers | Example | Bytes read |
|---|---|---|---|
| Byte (8 bits) | AL AH BL BH CL CH DL DH | mov al,[value] | 1 |
| Word (16 bits) | AX BX CX DX SP BP SI DI | mov ax,[value] | 2 |
Size specifiers
When no register fixes the size, the assembler cannot guess it:
mov byte [flag], 1 ; writes 1 byte
mov word [count], 1 ; writes 2 bytes⚠️ mov [count],1 is ambiguous — NASM rejects it (“operation size not specified”).
6. Data Movement Trace
Worked example – Partial-register updates
mov ax, 1234h
mov al, 56h
mov bh, ah| After | AH | AL | AX | BH |
|---|---|---|---|---|
mov ax,1234h | 12h | 34h | 1234h | ? |
mov al,56h | 12h | 56h | 1256h | ? |
mov bh,ah | 12h | 56h | 1256h | 12h |
- Writing
ALpreservesAH. - Writing
BHpreservesBL.
⚠️ Always trace the exact destination width — writing a half never clears the other half.
7. Immediate Addressing
Definition – Immediate operand
The value is encoded inside the instruction itself.
mov ax, 25
add al, 5
cmp bx, 1000h
and dx, 0FFh| Notation | Meaning |
|---|---|
25 | Decimal (default) |
1000h | Hexadecimal (h suffix) |
0FFh | Hex starting with a letter → leading 0 |
00000001b | Binary (b suffix) |
⚠️ NASM uses no # prefix (unlike the 68K).
⚠️ FFh without the leading zero is read as a label name, not a number.
8. Direct Memory Addressing
Definition – Direct operand
A label names the memory location; brackets request its contents.
mov ax, [total] ; AX ← contents of total
mov [result], ax ; contents of result ← AX
add byte [counter], 1| Syntax | Meaning |
|---|---|
[total] | The value stored at total |
total | The address (offset) of total |
- The variable definition (
db/dw) determines its storage size. - The default segment is usually
DS.
Worked example
mov dx, message ; DX ← offset of message (an address)
mov al, [message] ; AL ← first character of message9. Register-Indirect Addressing
Definition – Register-indirect operand
A register holds the effective offset of the operand.
mov al, [bx]
mov ax, [si]
mov [di], al
mov ax, [bp+4]| Register | Default segment |
|---|---|
BX, SI, DI | DS |
BP | SS |
⚠️ Only BX, BP, SI, DI can be used as address registers — [ax], [cx], [dx], [sp] are illegal on the 8086.
10. ADD and SUB
Definition
| Instruction | Operation |
|---|---|
ADD d, s | |
SUB d, s |
Both update CF, PF, AF, ZF, SF, OF.
Worked example – and
mov ax, [P]
add ax, [Q]
mov [R], ax
mov bx, [X]
sub bx, 10
mov [Y], bxIf and , then , ZF = 0, CF = 0.
⚠️ add [R],[P] is illegal — memory-to-memory arithmetic needs a register in between.
11. Carry and Signed Overflow
Definition – Two interpretations of the same bits
- CF = 1 → carry/borrow outside the unsigned width.
- OF = 1 → result is invalid as a signed number.
Overflow rule for addition: OF = 1 exactly when two operands of the same sign produce a result of the opposite sign.
Worked example – two 8-bit additions
| Operation | Result | Unsigned view | Signed view | CF | OF | ZF | SF |
|---|---|---|---|---|---|---|---|
FFh + 01h | 00h | 255 + 1 = 256 ✗ | −1 + 1 = 0 ✓ | 1 | 0 | 1 | 0 |
7Fh + 01h | 80h | 127 + 1 = 128 ✓ | 127 + 1 = −128 ✗ | 0 | 1 | 0 | 1 |
flowchart LR A[Same bit pattern] --> U[Unsigned data?] --> C[Check CF] A --> S[Signed data?] --> O[Check OF]
⚠️ The intended data type decides which flag matters — the CPU always computes both.
12. ADC and SBB – Multiword Arithmetic
Definition
| Instruction | Operation |
|---|---|
ADC d, s | |
SBB d, s |
32-bit addition and subtraction (DX:AX op CX:BX)
; DX:AX = DX:AX + CX:BX
add ax, bx ; low words, sets CF
adc dx, cx ; high words + carry
; DX:AX = DX:AX - CX:BX
sub ax, bx ; low words, sets CF (borrow)
sbb dx, cx ; high words - borrowWorked example –
| Step | Operation | Result | CF |
|---|---|---|---|
| 1 | add ax,bx → FFFFh + 0001h | AX = 0000h | 1 |
| 2 | adc dx,cx → 0001h + 0000h + 1 | DX = 0002h | 0 |
Result: ✓
flowchart LR L["ADD low words"] -- CF --> H["ADC high words"]
⚠️ Do not modify CF between the paired instructions (no ADD, SUB, CMP, shifts… in between).
13. INC, DEC, NEG, and CMP
Definitions
| Instruction | Operation | Flag notes |
|---|---|---|
INC d | Preserves CF | |
DEC d | Preserves CF | |
NEG d | (two’s complement) | CF = 1 unless operand was 0 |
CMP d, s | Computes , discards result | Sets flags like SUB |
Worked examples
NEG ALwith AL =05h→ AL =FBh(−5), CF = 1.NEG ALwith AL =00h→ AL =00h, CF = 0.CMP AL,7with AL = 5 → computes 5 − 7: CF = 1 (borrow), ZF = 0, SF = 1; AL still 5.
💡 Because INC/DEC preserve CF, they can update a loop counter or pointer inside an ADC/SBB carry chain without breaking it.
14. Conditional Control
Definition – Compare-and-branch pattern
CMP sets the flags; a conditional jump (Jcc) tests them.
cmp ax, bx
je equal_values ; ZF = 1
ja unsigned_above ; unsigned AX > BX
jg signed_greater ; signed AX > BX
equal_values:
mov byte [same], 1Worked example – same bits, different decisions
AX = FFFFh, BX = 0001h:
| Interpretation | AX | BX | AX > BX? | Jump taken |
|---|---|---|---|---|
| Unsigned | 65535 | 1 | Yes | JA ✓ |
| Signed | −1 | 1 | No | JG ✗ |
⚠️ Choose the jump by the meaning of the data, not by the bit pattern.
15. 8086 Conditional Jumps
Reference table
| Category | Jump | Meaning | Condition tested |
|---|---|---|---|
| Equality | JE / JZ | equal / zero | ZF = 1 |
JNE / JNZ | not equal | ZF = 0 | |
| Unsigned | JA | above | CF = 0 and ZF = 0 |
JAE / JNC | above or equal | CF = 0 | |
JB / JC | below | CF = 1 | |
JBE | below or equal | CF = 1 or ZF = 1 | |
| Signed | JG | greater | ZF = 0 and SF = OF |
JGE | greater or equal | SF = OF | |
JL | less | SF ≠ OF | |
JLE | less or equal | ZF = 1 or SF ≠ OF | |
| Overflow | JO / JNO | overflow / none | OF = 1 / 0 |
| Sign | JS / JNS | negative / not | SF = 1 / 0 |
| Parity | JP / JNP | even / odd parity | PF = 1 / 0 |
💡 Memory aid: Above/Below = unsigned; Greater/Less = signed.
16. If–Else Translation
Worked example
High-level code:
mov ax, [P]
add ax, [Q]
mov [X], ax
cmp ax, 4
je equal_four
add ax, [W] ; false case
mov [Y], ax
jmp finished ; skip the true case
equal_four:
mov word [Y], 6 ; true case
finished:flowchart TD A["AX ← P + Q; X ← AX"] --> B{"AX = 4 ?"} B -- "yes (JE)" --> T["Y ← 6"] B -- no --> F["Y ← AX + W"] F -- JMP --> E[finished] T --> E
CMPleaves AX unchanged.JEenters the true case.JMPprevents the false case from falling through into the true case.wordspecifies the memory destination size.
⚠️ Forgetting the JMP finished makes the false branch also execute Y ← 6.
17. Logical Instructions
Definitions
| Instruction | Effect | Typical use |
|---|---|---|
AND d,s | bitwise AND | Clear selected bits (mask with 0) |
OR d,s | bitwise OR | Set selected bits (mask with 1) |
XOR d,s | bitwise XOR | Toggle selected bits; xor r,r zeros r |
NOT d | complement every bit | One’s complement |
TEST d,s | AND for flags only | Test bits without changing d |
Flag effects: AND, OR, XOR, TEST clear CF and OF (and set ZF, SF, PF from the result). NOT changes no flags.
Worked example – AL = 10110110b
| Instruction | Mask | Result |
|---|---|---|
and al,0Fh | 00001111b | 00000110b (upper nibble cleared) |
or al,01h | 00000001b | 10110111b (bit 0 set) |
xor al,80h | 10000000b | 00110110b (bit 7 toggled) |
not al | — | 01001001b |
test al,04h | 00000100b | AL unchanged, ZF = 0 (bit 2 is 1) |
18. Input-Bit Masking
Worked example – combine bit 0 and bit 7
mov al, [input]
mov bl, al ; keep a copy
and al, 00000001b ; AL = input bit 0
and bl, 10000000b ; BL = input bit 7 (still at position 7)
shr bl, 7 ; move it to position 0
or al, bl ; combine
mov [output], alTrace with input = 10000110b:
| Step | AL | BL |
|---|---|---|
| after copies | 10000110 | 10000110 |
and al,1 | 00000000 | — |
and bl,80h | — | 10000000 |
shr bl,7 | — | 00000001 |
or al,bl | 00000001 | — |
flowchart LR I[input] --> A["AND 01h → bit 0"] I --> B["AND 80h → bit 7"] --> S["SHR 7 → bit 0"] A --> O["OR → output"] S --> O
💡 Copying into BL first preserves the original input value.
19. Shift Instructions
Definitions
| Instruction | Direction | Bit inserted | Interpretation |
|---|---|---|---|
SHL | left | 0 at bit 0 | × 2 (if no significant bit lost) |
SHR | right | 0 at MSB | unsigned ÷ 2 |
SAR | right | copy of sign bit at MSB | signed ÷ 2 |
The last bit shifted out goes into CF.
Worked example – AL = 96h = 10010110b (unsigned 150, signed −106)
| Instruction | Result | CF | Meaning |
|---|---|---|---|
shl al,1 | 00101100b = 2Ch | 1 | MSB lost → ×2 invalid in 8 bits |
shr al,1 | 01001011b = 4Bh = 75 | 0 | 150 / 2 = 75 ✓ |
sar al,1 | 11001011b = CBh = −53 | 0 | −106 / 2 = −53 ✓ |
⚠️ Rounding caveat: SAR rounds toward −∞. Example: −5 (FBh) sar 1 → FDh = −3, whereas signed division (IDIV) gives −2.
20. Count-Controlled Loop
Definition – LOOP
LOOP label: ; if , jump to label.
Worked example – sum 16 bytes
mov cx, 16 ; iteration count
mov si, buffer ; pointer to first byte
xor al, al ; sum = 0
next_byte:
add al, [si]
inc si
loop next_byteflowchart TD I["CX ← 16, SI ← buffer, AL ← 0"] --> B["AL ← AL + [SI]; SI ← SI + 1"] B --> L["CX ← CX − 1"] L --> C{"CX ≠ 0 ?"} C -- yes --> B C -- no --> E[done]
LOOPdoes not alter arithmetic flags.
⚠️ Starting with CX = 0 decrements to FFFFh first → 65 536 iterations. Guard with jcxz if CX can be zero.
⚠️ The 8-bit sum in AL silently wraps past 255 — use a word accumulator for larger totals (see Lecture 5, Array traversal).
21. Assembler Source Structure
Definition – Instructions vs directives
| Element | Role | Example |
|---|---|---|
| Instruction | Executed by the CPU | mov ax,5 |
| Directive | Guides the assembler (no CPU code) | bits 16, org 100h |
| Label | Names an address | start: |
| Comment | Documents intent | ; init counter |
| Directive | Meaning |
|---|---|
bits 16 | Generate 16-bit code |
org 100h | Code starts at offset 100h (DOS .COM image) |
db | Define initialised byte(s) |
dw | Define initialised word(s) |
equ | Define a symbolic constant (no storage) |
22. Minimal Executable Program
Worked example – “Hello” in NASM / DOSBox-X
bits 16
org 100h
start:
mov dx, message ; DS:DX → string
mov ah, 09h ; DOS: print $-terminated string
int 21h
mov ax, 4C00h ; DOS: terminate, return code 0
int 21h
message db "Hello$"sequenceDiagram participant P as Program participant D as DOS (INT 21h) P->>D: AH=09h, DX=message D-->>P: prints "Hello" P->>D: AH=4Ch, AL=00h D-->>P: process ends
INTis a processor instruction.- Function
AH = 09hand the$terminator belong to the DOS service, not the CPU. AH = 4Chterminates the process (AL = return code).
⚠️ Without the $, function 09h keeps printing memory until it happens to find one.
23. Debugging Workflow
- Assemble and read every diagnostic.
- Set a breakpoint before the faulty region.
- Record registers and flags.
- Single-step one instruction at a time.
- Verify memory addresses and operand widths.
- Distinguish a CPU instruction from a DOS service.
- Test both sides of every branch.
- Check loop entry and termination conditions.
⚠️ Trace before changing the program — guessing fixes often hide the real bug.
24. Exercises
Exercise 1 – Registers and flags
Execute ADD AL,01h with AL = FFh, then with AL = 7Fh. Find AL, CF, ZF, SF, OF.
| Start AL | Result AL | CF | ZF | SF | OF |
|---|---|---|---|---|---|
FFh | 00h | 1 | 1 | 0 | 0 |
7Fh | 80h | 0 | 0 | 1 | 1 |
Why they differ: FFh + 01h overflows the unsigned range (255 + 1) but is a valid signed sum (−1 + 1 = 0). 7Fh + 01h is a valid unsigned sum (128) but overflows the signed range (127 + 1 ≠ −128).
Exercise 2 – Repair the invalid instructions
| Invalid | Problem | Repair |
|---|---|---|
mov [A],[B] | Two memory operands | mov ax,[B] / mov [A],ax |
add ax,bl | Width mismatch (16 vs 8) | xor bx,bx / mov bl,[small] / add ax,bx |
mov ds,1200h | Immediate → segment register | mov ax,1200h / mov ds,ax |
mov [count],1 | Size unspecified | mov word [count],1 |
- A register bridges memory operands.
- Mixed widths require deliberate zero extension.
- A segment register is loaded through a general register.
- The memory write states its size explicitly.