Chapter 11: Instruction Encoding and String Instructions
Introduction
Learning Outcomes
Sort the 8086's roughly 100 instructions into six functional classes and describe what each class is for.
Work out an instruction's machine code from its opcode, D/W bits and ModR/M byte.
Use MOVS, CMPS, SCAS, LODS and STOS with REP prefixes; track how DF, SI, DI and CX change.
Know XLAT, LEA, LDS, LES and the processor control instructions; read from the flag table which flags an instruction changes.
Introduction
The 8086 Instruction Set at a Glance
Instruction Encoding
Machine Code Format
inc ax = 40h (1 byte), mov ax, imm16 = B8h + 2 bytes, add al, imm8 = 04h + 1 byte.Instruction Encoding
The REG and MOD Fields
| REG | W = 0 | W = 1 | SREG |
|---|---|---|---|
000 | AL | AX | ES (00) |
001 | CL | CX | CS (01) |
010 | DL | DX | SS (10) |
011 | BL | BX | DS (11) |
100 | AH | SP | |
101 | CH | BP | |
110 | DH | SI | |
111 | BH | DI |
| MOD | Meaning |
|---|---|
00 | Memory, no displacement (R/M = 110 means a direct 16-bit address) |
01 | Memory + 8-bit displacement (sign-extended) |
10 | Memory + 16-bit displacement |
11 | Register: R/M is a REG code too |
mov ax, bx → 8B C3: 100010 1 1 | 11 000 011 (D = 1, destination REG = AX, R/M = BX). The same instruction can also be encoded as 89 D8 (D = 0).Instruction Encoding
The R/M Field: Effective Address (EA)
| R/M | MOD = 00 | MOD = 01 | MOD = 10 | Default segment |
|---|---|---|---|---|
000 | [BX+SI] | [BX+SI+d8] | [BX+SI+d16] | DS |
001 | [BX+DI] | [BX+DI+d8] | [BX+DI+d16] | DS |
010 | [BP+SI] | [BP+SI+d8] | [BP+SI+d16] | SS |
011 | [BP+DI] | [BP+DI+d8] | [BP+DI+d16] | SS |
100 | [SI] | [SI+d8] | [SI+d16] | DS |
101 | [DI] | [DI+d8] | [DI+d16] | DS |
110 | [d16] direct | [BP+d8] | [BP+d16] | DS / SS |
111 | [BX] | [BX+d8] | [BX+d16] | DS |
- Only BX, BP, SI, DI can be address registers;
[ax],[cx],[si+di]are invalid. - For
[bp]the assembler emits[bp+0](MOD = 01, d8 = 0), because MOD = 00, R/M = 110 is reserved for a direct address. - A segment prefix overrides the default:
mov al, es:[bx]→ prefix 26h.
Instruction Encoding
Encoding Step by Step: MOV CL, [BX+SI+12h]
Instruction: move a byte from memory into CL. Form: MOV reg, r/m (opcode + ModR/M + displacement).
Opcode of the MOV r/m ↔ reg family: 100010.
D = 1: the REG field is the destination. W = 0: byte operation. Byte 1 = 1000 1010b = 8Ah.
MOD = 01: memory + 8-bit displacement. REG = 001 (CL). R/M = 000 ([BX+SI]). Byte 2 = 01 001 000b = 48h.
12h is appended as an 8-bit displacement. Result: 8A 48 12. With a 16-bit displacement it would be MOD = 10 and 4 bytes.
Instruction Encoding
Sample Instructions and Their Lengths
| Instruction | Machine code | Bytes | Note |
|---|---|---|---|
nop | 90 | 1 | really xchg ax, ax |
inc ax / push bx | 40 / 53 | 1 | register code inside the opcode (40h+r, 50h+r) |
mov al, 5 | B0 05 | 2 | B0h+r, immediate byte |
int 21h | CD 21 | 2 | interrupt number in the instruction |
mov ax, 1234h | B8 34 12 | 3 | low byte first (little-endian) |
mov cl, [bx+si+12h] | 8A 48 12 | 3 | ModR/M + d8 |
mov es:[bx], al | 26 88 07 | 3 | 26h = ES prefix |
add [bx+di+1000h], dx | 01 91 00 10 | 4 | MOD = 10, d16 |
mov word ptr [1234h], 5678h | C7 06 34 12 78 56 | 6 | longest form: d16 + imm16 |
rep movsb | F3 A4 | 2 | F3h = REP prefix |
Prefixes: 26h ES, 2Eh CS, 36h SS, 3Eh DS (segment); F3h REP/REPE, F2h REPNE; F0h LOCK. To see machine code in emu8086, open the listing (.lst) file after assembling.
Instruction Encoding
Encoding Forms of MOV
| Form | Opcode (8 / 16 bit) | Followed by | Example | Code |
|---|---|---|---|---|
| register → r/m | 88 / 89 | ModR/M (+ disp) | mov [bx], al | 88 07 |
| r/m → register | 8A / 8B | ModR/M (+ disp) | mov cx, [si] | 8B 0C |
| immediate → r/m | C6 / C7 | ModR/M (REG = 000) + disp + imm | mov byte ptr [bx], 5 | C6 07 05 |
| immediate → register | B0+r / B8+r | imm8 / imm16 | mov si, 1000h | BE 00 10 |
| memory → AL / AX | A0 / A1 | 16-bit address | mov ax, [2000h] | A1 00 20 |
| AL / AX → memory | A2 / A3 | 16-bit address | mov [1000h], al | A2 00 10 |
| segment register → r/m16 | 8C | ModR/M (REG = sreg) | mov ax, es | 8C C0 |
| r/m16 → segment register | 8E | ModR/M (REG = sreg) | mov ds, ax | 8E D8 |
One instruction can have several encodings: mov al, [1000h] = A0 00 10 (3 bytes) or 8A 06 00 10 (4 bytes). The assembler picks the short one.
Instruction Encoding
Encoding Forms of MOV (continued)
mov ds, ax: 11 011 000 = D8h.mov ds, 1234h is invalid), no memory → memory. mov cs, … is not used; CS changes only through JMP/CALL/RET/INT.Instruction Encoding
MOD = 00, R/M = 110: Not [BP] but [d16]
mov [1000h], bl → 88 1E 00 10
| Instruction | Code | Note |
|---|---|---|
mov [1000h], al | A2 00 10 | short form for AL, 3 bytes |
mov [1000h], al | 88 06 00 10 | general form, 4 bytes (same effect): ModR/M 06h = 00 000 110 |
mov [1000h], bl | 88 1E 00 10 | no short form for BL |
mov [bp], bl | 88 5E 00 | 5Eh = 01 011 110: [BP+d8], d8 = 0; segment SS |
88 1E as a 2-byte mov [bp], bl, and the address bytes would become a new instruction: 00 10 = add [bx+si], dl. Every later boundary shifts and memory gets corrupted.Instruction Encoding
Same Bytes, Different Instructions: Decoding from the Wrong Offset
11 bytes in memory. Code or data? The CPU can't tell: whatever byte CS:IP points to, it takes as an opcode.
Offset 0: B4h = MOV AH, imm8 (B0h + 4) → takes 1 immediate byte: mov ah, 02h.
B2h = MOV DL, imm8 → mov dl, 41h (41h = 'A').
CDh = INT imm8 → int 21h: AH = 02h, DOS prints the 'A' in DL.
B8h = MOV AX, imm16 → 2 bytes, little-endian: mov ax, 4C00h.
int 21h, AH = 4Ch: the program exits. Decoding from offset 0 gives the code the programmer wrote.
Now shift IP by one byte (offset 1). 02h = ADD r8, r/m8; ModR/M B2h = 10 110 010 → MOD = 10 (d16), REG = DH, R/M = [BP+SI]; d16 = CD41h → 4 bytes: the 'A' and INT's opcode are swallowed.
21h = AND r/m16, r16; ModR/M B8h = 10 111 000 → [BX+SI+d16], REG = DI; d16 = 4C00h. This instruction writes to memory.
The final CD 21 lands in the same place in both decodings: the decoding has resynchronized. But AX = 4C00h was never loaded, so INT 21h runs with whatever AH holds. The meaning comes not from the bytes but from where IP starts.
The 8086 has no hardware distinction between code and data: a JMP into data or a miscomputed IP executes the bytes as instructions. A disassembler, too, has to know where to start.
Data Transfer
Data Transfer Instructions
| Instruction | Operation | Example |
|---|---|---|
MOV d, s | d ← s (copy) | mov bx, [si] |
PUSH s / POP d | SP ← SP−2, [SS:SP] ← s / d ← [SS:SP], SP ← SP+2 | push ax |
XCHG a, b | a ↔ b (one must be a register) | xchg al, [bx] |
IN / OUT | AL/AX ↔ I/O port | in al, 60h |
XLAT | AL ← [DS:BX + AL] | table lookup |
LEA r, m | r ← address of m (EA) | lea si, msg |
LDS / LES r, m | r ← [m], DS/ES ← [m+2] (far pointer) | les di, farptr |
LAHF / SAHF | AH ← low byte of FLAGS / the reverse | |
PUSHF / POPF | push FLAGS / pop FLAGS |
Data Transfer · Step through the code
Code Trace: Printing a Byte in Hex with XLAT
BX gets the offset of the lookup table: BX = 011Dh.
The byte to print: AL = 3Ch (0011 1100b).
On the 8086 a shift count above 1 must be in CL.
shr al, cl: 0011 1100 → 0000 0011. AL = 03h (high nibble).
xlat: AL ← [DS:BX + AL] = hex_tab[3] = '3' (33h).
The character is copied to DL (the INT 21h/02h argument).
AH = 02h: the DOS print character service.
DOS prints '3' (AL also takes DL's value).
Reload the same byte: AL = 3Ch.
and al, 0Fh: the high nibble is masked off → AL = 0Ch.
hex_tab[12] = 'C' (43h).
DL = 43h. AH is still 02h, so no need to reload it.
'C' is printed → output 3C.
The program ends. XLAT translates through a table of up to 256 bytes in a single instruction.
org 100h mov bx, offset hex_tab mov al, num ; AL = 3Ch mov cl, 4 shr al, cl ; high nibble xlat ; AL = [BX+AL] mov dl, al mov ah, 2 int 21h ; print DL mov al, num and al, 0Fh ; low nibble xlat mov dl, al int 21h ret num db 3Ch hex_tab db '0123456789ABCDEF'
Data Transfer
Loading Addresses: LEA, LDS, LES
lea si, msg≡mov si, offset msg; the difference: LEA also handles addresses computed at run time:lea si, [bx+di+4].- LEA doesn't touch memory or the flags, so it doubles as an adder:
lea ax, [bx+si]→ AX = BX + SI. lds si, ptr: SI ← [ptr], DS ← [ptr+2].les di, ptrdoes the same with ES.- A far pointer is stored in memory offset first, segment second.
mov bx, 0010h mov di, 0004h lea ax, [bx+di+2] ; AX = 0016h mov ax, [bx+di+2] ; AX = [0016h] ; video memory: B800:0000 les di, screen ; DI=0000h ES=B800h mov al, 'A' stosb ; 'A' in the corner lds si, screen ; SI=0000h DS=B800h lodsb ; AL = 'A' (corner) ret screen dw 0000h, 0B800h
lea ax, [bx+di+2] and mov ax, [bx+di+2] is an exam favourite.Data Transfer · Step through the code
Address or Contents? LEA, MOV and LES
org 100h lea bx, vals ; BX ← address mov di, 2 lea ax, [bx+di+2] ; AX ← EA mov ax, [bx+di+2] ; AX ← [EA] les di, screen ; far pointer mov al, 'A' stosb ; ES:[DI] ← AL ret vals dw 1111h, 2222h, 3333h screen dw 0000h, 0B800h
LEA gives the address of the vals variable: BX = 0114h. Memory is not read.
DI = 2.
LEA AX, [BX+DI+2]: only the effective address is computed → AX = 0114h + 2 + 2 = 0118h. Memory is untouched, no flags change.
MOV AX, [BX+DI+2]: the contents at the same EA are read → AX = 3333h (the 3rd word of vals).
LES DI, screen: the far pointer in memory (offset first, segment second) → DI = 0000h, ES = B800h.
AL = 41h ('A').
STOSB: ES:[DI] = B800h × 10h + 0000h = B8000h ← 41h; DF = 0, so DI = 1. An 'A' in the top-left corner of the screen.
RET: the program ends.
Arithmetic and Logic
Arithmetic, Logic and Shift Instructions: A Summary
| Group | Instructions | Description |
|---|---|---|
| Addition | ADD ADC INC | ADC adds the carry (CF) too; INC leaves CF alone |
| Subtraction | SUB SBB DEC NEG CMP | SBB subtracts the borrow (CF) too; NEG = 0 − d; CMP doesn't store the result |
| Multiplication | MUL IMUL | unsigned / signed; implicit AL or AX |
| Division | DIV IDIV | unsigned / signed; quotient + remainder |
| BCD adjust | DAA DAS AAA AAS AAM AAD | corrects a binary result to decimal |
| Sign extension | CBW CWD | AL → AX, AX → DX:AX |
| Logic | AND OR XOR NOT TEST | clear, set, invert bits; TEST doesn't store the result |
| Shift / rotate | SHL SAL SHR SAR ROL ROR RCL RCR | count is 1 or CL; the bit shifted out goes to CF |
String Processing
String Instructions
| Instruction | Operation | Updates | Flags |
|---|---|---|---|
MOVSB / MOVSW | [ES:DI] ← [DS:SI] | SI, DI | – |
CMPSB / CMPSW | [DS:SI] − [ES:DI] | SI, DI | like CMP |
SCASB / SCASW | AL/AX − [ES:DI] | DI | like CMP |
LODSB / LODSW | AL/AX ← [DS:SI] | SI | – |
STOSB / STOSW | [ES:DI] ← AL/AX | DI | – |
cld → DF = 0, addresses increase; std → DF = 1, they decrease.In EXE programs ES must be set up separately (mov ax, @data / mov es, ax); in COM programs DS = ES = CS.
String Processing
Repeat Prefixes: REP, REPE, REPNE
| Prefix | Repeat while | Used with |
|---|---|---|
REP | CX ≠ 0 | MOVS, STOS |
REPE / REPZ | CX ≠ 0 and ZF = 1 | CMPS, SCAS: "while equal" |
REPNE / REPNZ | CX ≠ 0 and ZF = 0 | CMPS, SCAS: "while not equal" |
; copy a 100-byte block cld lea si, src lea di, dst mov cx, 100 rep movsb ; fill the screen with blanks mov ax, 0B800h mov es, ax xor di, di mov ax, 0720h ; ' ' + attribute mov cx, 2000 ; 80×25 rep stosw
String Processing · Step through the code
Code Trace: Copying with MOVSB
DF = 0: copy forward.
SI = 010Eh: source string.
DI = 0112h: destination area ('....').
Byte count CX = 4.
movsb: one byte copied; SI = 010Fh, DI = 0113h. Flags unchanged.
CX = 0003h.
movsb: one byte copied; SI = 0110h, DI = 0114h. Flags unchanged.
CX = 0002h.
movsb: one byte copied; SI = 0111h, DI = 0115h. Flags unchanged.
CX = 0001h.
movsb: one byte copied; SI = 0112h, DI = 0116h. Flags unchanged.
CX = 0000h.
dst = 'ABCD'. mov cx, 4 + rep movsb does the same in one instruction.
org 100h cld lea si, src ; DS:SI source lea di, dst ; ES:DI destination mov cx, 4 copy_lp: movsb ; [DI]←[SI], SI++, DI++ loop copy_lp ; = rep movsb ret src db 'ABCD' dst db 4 dup('.')
String Processing · Step through the code
Code Trace: Converting to Upper Case with LODSB + STOSB
cld: DF = 0 → string instructions increment SI/DI.
SI points to the source: SI = 0110h (msg).
The destination is the same string: DI = 0110h. In a COM program ES = DS, so ES:DI points to the same place.
5 characters to process.
lodsb: AL = [DS:SI] = 6Dh, then SI = 0111h.
Clear bit 5: AL = 4Dh.
stosb: [ES:DI] ← AL, then DI = 0111h.
CX = 0004h.
lodsb: AL = [DS:SI] = 69h, then SI = 0112h.
Clear bit 5: AL = 49h.
stosb: [ES:DI] ← AL, then DI = 0112h.
CX = 0003h.
lodsb: AL = [DS:SI] = 63h, then SI = 0113h.
Clear bit 5: AL = 43h.
stosb: [ES:DI] ← AL, then DI = 0113h.
CX = 0002h.
lodsb: AL = [DS:SI] = 72h, then SI = 0114h.
Clear bit 5: AL = 52h.
stosb: [ES:DI] ← AL, then DI = 0114h.
CX = 0001h.
lodsb: AL = [DS:SI] = 6Fh, then SI = 0115h.
Clear bit 5: AL = 4Fh.
stosb: [ES:DI] ← AL, then DI = 0115h.
CX = 0000h.
msg = 'MICRO'. A LODS/STOS pair is the standard pattern for transforming an array in place.
org 100h cld ; DF = 0: forward lea si, msg mov di, si ; write in place mov cx, 5 next_ch: lodsb ; AL = [SI], SI++ and al, 0DFh ; upper case stosb ; [DI] = AL, DI++ loop next_ch ret msg db 'micro'
String Processing · Step through the code
Code Trace: Finding a Character with REPNE SCASB
Scan forward.
DI = 0113h: the string to scan must be at ES:DI.
At most 7 comparisons.
Character to find: AL = 'h' (68h).
repne scasb: compares against 'p','u','s','h'; on the 4th comparison ZF = 1 and it stops. Each pass does DI++ and CX−−: DI = 0117h, CX = 0003h.
ZF = 1 (found) → no jump.
DI points one past the match; step back: DI = 0116h.
Subtract the start: DI = 3 → 'h' is at index 3 (counting from 0).
Had it not been found, the scan would end with CX = 0 and ZF = 0, and jne not_found would jump.
org 100h cld lea di, msg mov cx, 7 ; length mov al, 'h' ; target repne scasb ; search jne not_found dec di ; match position sub di, offset msg not_found: ret msg db 'pushing'
Control Transfer
Unconditional Jump: JMP
| Kind | Code | Range | Example |
|---|---|---|---|
| Short | EB d8 | −128 … +127 bytes | jmp short again |
| Near | E9 d16 | same segment, ±32 K | jmp start |
| Far | EA IP CS | any segment | jmp far ptr dst |
| Indirect near | FF /4 | IP ← register/memory | jmp bx, jmp jmp_tab[bx] |
| Indirect far | FF /5 | CS:IP ← memory (4 bytes) | jmp dword ptr [si] |
- Short and near jumps are relative: target = address of the next instruction + displacement, so the code can be moved in memory.
- Indirect jumps build a jump table (switch-case):
shl bx, 1/jmp hex_tab[bx]. EB FEjumps to itself (an endless loop): FEh = −2 and the instruction is 2 bytes.
Processor Control
Processor Control Instructions
| Instruction | Effect |
|---|---|
CLC / STC | CF ← 0 / CF ← 1 |
CMC | CF ← ¬CF |
CLD / STD | DF ← 0 / 1 (string direction) |
CLI / STI | IF ← 0 / 1 (INTR interrupts) |
| Instruction | Effect |
|---|---|
HLT | halt until an interrupt or RESET |
WAIT | wait until the TEST̅ pin goes active (8087 sync) |
ESC | pass the instruction to the coprocessor (8087) |
LOCK | prefix: LOCK̅ pin active for the next instruction → bus locked |
NOP | do nothing (90h), 3 clocks |
stc on error, and the caller does jc fail.lock xchg builds an atomic semaphore.Flag Effects
Flag Effects: Arithmetic and Logic Instructions
| Instruction | OF | SF | ZF | AF | PF | CF |
|---|---|---|---|---|---|---|
ADD ADC SUB SBB CMP NEG | * | * | * | * | * | * |
CMPS SCAS | * | * | * | * | * | * |
INC DEC | * | * | * | * | * | – |
MUL IMUL | * | ? | ? | ? | ? | * |
DIV IDIV | ? | ? | ? | ? | ? | ? |
DAA DAS | ? | * | * | * | * | * |
AAA AAS | ? | ? | ? | * | ? | * |
AAM AAD | ? | * | * | ? | * | ? |
AND OR XOR TEST | 0 | * | * | ? | * | 0 |
SHL SAL SHR SAR | *¹ | * | * | ? | * | * |
ROL ROR RCL RCR | *¹ | – | – | – | – | * |
* set by the result · 0 cleared · – unchanged · ? undefined (don't rely on it) · ¹ OF is defined only for a shift count of 1. DF, IF, TF are not affected by these instructions.
Flag Effects
Flag Effects: Other Instructions
| Instructions | Flags affected |
|---|---|
MOV XCHG LEA LDS LES XLAT PUSH POP IN OUT LAHF PUSHF CBW CWD NOT | none |
MOVS LODS STOS, REP | none |
JMP Jcc LOOP LOOPE LOOPNE JCXZ CALL RET | none (only read) |
SAHF | SF ZF AF PF CF ← AH |
POPF, IRET | all (from the stack) |
INT n, INTO | IF ← 0, TF ← 0 (old FLAGS on the stack) |
CLC STC CMC | CF |
CLD STD | DF |
CLI STI | IF |
inc, add) between cmp and jcc. Only flag-neutral instructions such as MOV, PUSH, LEA may go in between.Lab · 8086 instructions step by step
Lab: The Instruction Set
Summary
Summary
Summary
Exercises
- Encode
mov dx, [bp+di+8](opcode 100010, d = 1, w = 1). - DS = 1000h, BX = 0200h, AL = 05h: from which physical address does
xlatload AL? farptr ddholds the bytes 34 12 00 B8. What are ES and DI afterles di, farptr?- How many bytes does
rep movsbcopy when CX = 0? - CLD, CX = 5, SI = 0100h, DI = 0200h;
repe cmpsbfinds a mismatch at byte 3. CX, SI, DI and ZF afterwards? - A
jmp shortat 0150h jumps to 0130h. What is its machine code? - Count the spaces in a string with
repne scasb.
Hints / answers
- MOD = 01, REG = 010 (DX), R/M = 011 ([BP+DI]) → 8B 53 08; segment SS.
- EA = BX + AL = 0205h → 10000h + 0205h = 10205h.
- Offset first, then segment: DI = 1234h, ES = B800h.
- 0: REP tests CX first; with CX = 0 the instruction never runs.
- Three comparisons: CX = 2, SI = 0103h, DI = 0203h, ZF = 0. The mismatch is at SI − 1 = 0102h.
- The next instruction is at 0152h; 0130h − 0152h = −22h = DEh → EB DE.
- Count each hit; while CX ≠ 0, repeat
repne scasb(DI resumes where it stopped).
Summary
Exercises (2)
- Which instruction is
8A 1E 34 12? What do the same bytes decode to one byte later (from offset 1)? - Encode
mov es, dxandmov al, [0020h].
Hints / answers
mov bl, [1234h](8Ah: d = 1, w = 0; 1Eh = 00 011 110 → MOD = 00, R/M = 110: d16). From offset 1:1E=push ds,34 12=xor al, 12h.8E C2(11 000 010: sreg = ES, R/M = DX);A0 20 00(the short AL form, address little-endian).
Summary
References
- Intel, The 8086 Family User's Manual, 1979 (Chapter 2: the instruction set, Chapter 4: instruction encoding).
- Barry B. Brey, The Intel Microprocessors, 8th ed., Pearson, 2009.
- Kip R. Irvine, Assembly Language for x86 Processors, 7th ed., Pearson, 2014.
- Ş. Ç. Öztürk, O. Ergin, Intel 8086 ile Mikroişlemci Programlamaya Giriş, 2009 (in Turkish).
- emu8086 help: 8086 instructions and 8086 assembler tutorial.