Chapter 11: Instruction Encoding and String Instructions

Microprocessors
Instruction classesMachine codeModR/MMOV formsXLATLEA · LDS · LESString instructionsREP prefixesJMP encodingProcessor controlFlag effects
Sercan KÜLCÜ · Giresun University · Computer Engineering

Introduction

Learning Outcomes

Instruction classes

Sort the 8086's roughly 100 instructions into six functional classes and describe what each class is for.

Machine code

Work out an instruction's machine code from its opcode, D/W bits and ModR/M byte.

String instructions

Use MOVS, CMPS, SCAS, LODS and STOS with REP prefixes; track how DF, SI, DI and CX change.

Reference

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

1. Data transferCopy data between registers, memory, the stack and I/O.
MOV PUSH POP XCHG IN OUT XLAT LEA LDS LES LAHF SAHF PUSHF POPF
2. ArithmeticThe four operations, BCD adjust, sign extension.
ADD ADC INC AAA DAA SUB SBB DEC NEG CMP AAS DAS MUL IMUL AAM DIV IDIV AAD CBW CWD
3. Logic, shift, rotateBit-level operations.
NOT AND OR XOR TEST SHL SAL SHR SAR ROL ROR RCL RCR
4. String processingScan and copy memory blocks through SI/DI.
MOVSB/W CMPSB/W SCASB/W LODSB/W STOSB/W REP REPE REPZ REPNE REPNZ
5. Control transferBranches, loops, procedures and interrupts.
JMP Jcc (JE JNE JA JB JG JL …) LOOP LOOPE LOOPNE JCXZ CALL RET RETF INT INTO IRET
6. Processor controlSet/clear flags and control the processor state.
CLC STC CMC CLD STD CLI STI HLT WAIT ESC LOCK NOP

Instruction Encoding

Machine Code Format

opcode6 bitDWMOD2REG3R/M3disp lowdisp highdata lowdata highbyte 1byte 2 (ModR/M)0 / 1 / 2 displacement bytes0 / 1 / 2 immediate bytesInstructions are 1–6 bytes long; prefixes (segment, REP, LOCK) can go in front.
D (direction)1: the REG field is the destination; 0: the REG field is the source.
W (word)0: byte (8-bit) operation; 1: word (16-bit) operation.
ModR/MMOD says whether the operand is a register or memory, R/M picks the addressing form, REG the other register.
Common instructions have shorter special forms: inc ax = 40h (1 byte), mov ax, imm16 = B8h + 2 bytes, add al, imm8 = 04h + 1 byte.

Instruction Encoding

The REG and MOD Fields

REGW = 0W = 1SREG
000ALAXES (00)
001CLCXCS (01)
010DLDXSS (10)
011BLBXDS (11)
100AHSP
101CHBP
110DHSI
111BHDI
MODMeaning
00Memory, no displacement (R/M = 110 means a direct 16-bit address)
01Memory + 8-bit displacement (sign-extended)
10Memory + 16-bit displacement
11Register: R/M is a REG code too
Example: 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/MMOD = 00MOD = 01MOD = 10Default 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]

OPCODEDWMODREGR/Mdisp8byte 1byte 2MOV CL, [BX+SI+12h]

Instruction: move a byte from memory into CL. Form: MOV reg, r/m (opcode + ModR/M + displacement).

100010OPCODEDWMODREGR/Mdisp8byte 1byte 2MOV CL, [BX+SI+12h]

Opcode of the MOV r/m ↔ reg family: 100010.

100010OPCODE1D0WMODREGR/Mdisp8byte 1byte 28AhMOV CL, [BX+SI+12h]

D = 1: the REG field is the destination. W = 0: byte operation. Byte 1 = 1000 1010b = 8Ah.

100010OPCODE1D0W01MOD001REG000R/Mdisp8byte 1byte 28Ah48hMOV CL, [BX+SI+12h]

MOD = 01: memory + 8-bit displacement. REG = 001 (CL). R/M = 000 ([BX+SI]). Byte 2 = 01 001 000b = 48h.

100010OPCODE1D0W01MOD001REG000R/M12hdisp8byte 1byte 28Ah48h12hMachine code: 8A 48 12 (3 bytes)MOV CL, [BX+SI+12h]

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

InstructionMachine codeBytesNote
nop901really xchg ax, ax
inc ax / push bx40 / 531register code inside the opcode (40h+r, 50h+r)
mov al, 5B0 052B0h+r, immediate byte
int 21hCD 212interrupt number in the instruction
mov ax, 1234hB8 34 123low byte first (little-endian)
mov cl, [bx+si+12h]8A 48 123ModR/M + d8
mov es:[bx], al26 88 07326h = ES prefix
add [bx+di+1000h], dx01 91 00 104MOD = 10, d16
mov word ptr [1234h], 5678hC7 06 34 12 78 566longest form: d16 + imm16
rep movsbF3 A42F3h = 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

FormOpcode (8 / 16 bit)Followed byExampleCode
register → r/m88 / 89ModR/M (+ disp)mov [bx], al88 07
r/m → register8A / 8BModR/M (+ disp)mov cx, [si]8B 0C
immediate → r/mC6 / C7ModR/M (REG = 000) + disp + immmov byte ptr [bx], 5C6 07 05
immediate → registerB0+r / B8+rimm8 / imm16mov si, 1000hBE 00 10
memory → AL / AXA0 / A116-bit addressmov ax, [2000h]A1 00 20
AL / AX → memoryA2 / A316-bit addressmov [1000h], alA2 00 10
segment register → r/m168CModR/M (REG = sreg)mov ax, es8C C0
r/m16 → segment register8EModR/M (REG = sreg)mov ds, ax8E 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)

Segment register codes (sreg)ES = 00, CS = 01, SS = 10, DS = 11; they go into the ModR/M REG field as 0 + sreg. mov ds, ax: 11 011 000 = D8h.
Forms that don't existNo immediate → segment register (mov ds, 1234h is invalid), no memory → memory. mov cs, … is not used; CS changes only through JMP/CALL/RET/INT.
mov ds, ax → 8E D811011000MOD = 11REG = 011 → DSR/M = 000 → AXES 00 · CS 01 · SS 10 · DS 11 (REG field = 0 + sreg)

Instruction Encoding

MOD = 00, R/M = 110: Not [BP] but [d16]

mov [1000h], bl → 88 1E 00 10

88h10001000d = 0, w = 0
1Eh00011110REG = BL
MOD = 00 and R/M = 110 → a 2-byte address follows: 00 10 = 1000h. The instruction is 4 bytes. For [BP] the assembler uses MOD = 01, d8 = 0.
InstructionCodeNote
mov [1000h], alA2 00 10short form for AL, 3 bytes
mov [1000h], al88 06 00 10general form, 4 bytes (same effect): ModR/M 06h = 00 000 110
mov [1000h], bl88 1E 00 10no short form for BL
mov [bp], bl88 5E 005Eh = 01 011 110: [BP+d8], d8 = 0; segment SS
A decoder that wrongly took 1Eh as [BP] would read 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

from offset 0from offset 10000B40001020002B20003410004CD0005210006B800070000084C0009CD000A21

11 bytes in memory. Code or data? The CPU can't tell: whatever byte CS:IP points to, it takes as an opcode.

from offset 0from offset 10000B40001020002B20003410004CD0005210006B800070000084C0009CD000A21MOV AH, 02h

Offset 0: B4h = MOV AH, imm8 (B0h + 4) → takes 1 immediate byte: mov ah, 02h.

from offset 0from offset 10000B40001020002B20003410004CD0005210006B800070000084C0009CD000A21MOV AH, 02hMOV DL, 41h

B2h = MOV DL, imm8 → mov dl, 41h (41h = 'A').

from offset 0from offset 10000B40001020002B20003410004CD0005210006B800070000084C0009CD000A21MOV AH, 02hMOV DL, 41hINT 21h

CDh = INT imm8 → int 21h: AH = 02h, DOS prints the 'A' in DL.

from offset 0from offset 10000B40001020002B20003410004CD0005210006B800070000084C0009CD000A21MOV AH, 02hMOV DL, 41hINT 21hMOV AX, 4C00h

B8h = MOV AX, imm16 → 2 bytes, little-endian: mov ax, 4C00h.

from offset 0from offset 10000B40001020002B20003410004CD0005210006B800070000084C0009CD000A21MOV AH, 02hMOV DL, 41hINT 21hMOV AX, 4C00hINT 21h

int 21h, AH = 4Ch: the program exits. Decoding from offset 0 gives the code the programmer wrote.

from offset 0from offset 10000B40001020002B20003410004CD0005210006B800070000084C0009CD000A21MOV AH, 02hMOV DL, 41hINT 21hMOV AX, 4C00hINT 21hADD DH, [BP+SI+CD41h]

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.

from offset 0from offset 10000B40001020002B20003410004CD0005210006B800070000084C0009CD000A21MOV AH, 02hMOV DL, 41hINT 21hMOV AX, 4C00hINT 21hADD DH, [BP+SI+CD41h]AND [BX+SI+4C00h], DI

21h = AND r/m16, r16; ModR/M B8h = 10 111 000 → [BX+SI+d16], REG = DI; d16 = 4C00h. This instruction writes to memory.

from offset 0from offset 10000B40001020002B20003410004CD0005210006B800070000084C0009CD000A21MOV AH, 02hMOV DL, 41hINT 21hMOV AX, 4C00hINT 21hADD DH, [BP+SI+CD41h]AND [BX+SI+4C00h], DIINT 21h

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

InstructionOperationExample
MOV d, sd ← s (copy)mov bx, [si]
PUSH s / POP dSP ← SP−2, [SS:SP] ← s / d ← [SS:SP], SP ← SP+2push ax
XCHG a, ba ↔ b (one must be a register)xchg al, [bx]
IN / OUTAL/AX ↔ I/O portin al, 60h
XLATAL ← [DS:BX + AL]table lookup
LEA r, mr ← address of m (EA)lea si, msg
LDS / LES r, mr ← [m], DS/ES ← [m+2] (far pointer)les di, farptr
LAHF / SAHFAH ← low byte of FLAGS / the reverse
PUSHF / POPFpush FLAGS / pop FLAGS
Data transfer instructions don't affect the flags; the exceptions are SAHF and POPF, which write the flags directly.

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, ptr does 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 gives the address, MOV the contents: the difference between 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
BX0114hDI0000hAX0000hES0700hvals (DS:0114h)1111h0114h2222h0116h3333h0118hscreen (far pointer)0000hoffsetB800hsegment

LEA gives the address of the vals variable: BX = 0114h. Memory is not read.

BX0114hDI0002hAX0000hES0700hvals (DS:0114h)1111h0114h2222h0116h3333h0118hscreen (far pointer)0000hoffsetB800hsegment

DI = 2.

BX0114hDI0002hAX0118hES0700hvals (DS:0114h)1111h0114h2222h0116h3333h0118hscreen (far pointer)0000hoffsetB800hsegmentEA = BX + DI + 2 = 0118h (address)

LEA AX, [BX+DI+2]: only the effective address is computed → AX = 0114h + 2 + 2 = 0118h. Memory is untouched, no flags change.

BX0114hDI0002hAX3333hES0700hvals (DS:0114h)1111h0114h2222h0116h3333h0118hscreen (far pointer)0000hoffsetB800hsegment[EA] = 3333h (contents)

MOV AX, [BX+DI+2]: the contents at the same EA are read → AX = 3333h (the 3rd word of vals).

BX0114hDI0000hAX3333hESB800hvals (DS:0114h)1111h0114h2222h0116h3333h0118hscreen (far pointer)0000hoffsetB800hsegmentES:DI → B8000h:00

LES DI, screen: the far pointer in memory (offset first, segment second) → DI = 0000h, ES = B800h.

BX0114hDI0000hAX3341hESB800hvals (DS:0114h)1111h0114h2222h0116h3333h0118hscreen (far pointer)0000hoffsetB800hsegmentES:DI → B8000h:00

AL = 41h ('A').

BX0114hDI0001hAX3341hESB800hvals (DS:0114h)1111h0114h2222h0116h3333h0118hscreen (far pointer)0000hoffsetB800hsegmentES:DI → B8000h:'A'

STOSB: ES:[DI] = B800h × 10h + 0000h = B8000h ← 41h; DF = 0, so DI = 1. An 'A' in the top-left corner of the screen.

BX0114hDI0001hAX3341hESB800hvals (DS:0114h)1111h0114h2222h0116h3333h0118hscreen (far pointer)0000hoffsetB800hsegmentES:DI → B8000h:'A'

RET: the program ends.

Arithmetic and Logic

Arithmetic, Logic and Shift Instructions: A Summary

GroupInstructionsDescription
AdditionADD ADC INCADC adds the carry (CF) too; INC leaves CF alone
SubtractionSUB SBB DEC NEG CMPSBB subtracts the borrow (CF) too; NEG = 0 − d; CMP doesn't store the result
MultiplicationMUL IMULunsigned / signed; implicit AL or AX
DivisionDIV IDIVunsigned / signed; quotient + remainder
BCD adjustDAA DAS AAA AAS AAM AADcorrects a binary result to decimal
Sign extensionCBW CWDAL → AX, AX → DX:AX
LogicAND OR XOR NOT TESTclear, set, invert bits; TEST doesn't store the result
Shift / rotateSHL SAL SHR SAR ROL ROR RCL RCRcount is 1 or CL; the bit shifted out goes to CF
The CPU adds and subtracts with one circuit and doesn't know whether a number is signed or unsigned, so it sets flags for both readings: CF flags unsigned overflow, OF signed overflow.

String Processing

String Instructions

InstructionOperationUpdatesFlags
MOVSB / MOVSW[ES:DI] ← [DS:SI]SI, DI–
CMPSB / CMPSW[DS:SI] − [ES:DI]SI, DIlike CMP
SCASB / SCASWAL/AX − [ES:DI]DIlike CMP
LODSB / LODSWAL/AX ← [DS:SI]SI–
STOSB / STOSW[ES:DI] ← AL/AXDI–
Source / destinationThe source is DS:SI (a segment prefix can change it), the destination is always ES:DI.
Direction flagcld → DF = 0, addresses increase; std → DF = 1, they decrease.
Step±1 for the B (byte) form, ±2 for the W (word) form.

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

PrefixRepeat whileUsed with
REPCX ≠ 0MOVS, STOS
REPE / REPZCX ≠ 0 and ZF = 1CMPS, SCAS: "while equal"
REPNE / REPNZCX ≠ 0 and ZF = 0CMPS, SCAS: "while not equal"
1. If CX = 0, stop (the instruction may never run).
2. Execute the string instruction once, update SI/DI.
3. CX ← CX − 1 (flags unchanged).
4. For REPE/REPNE, test the ZF condition; stop if it fails, otherwise go back to 1.
; 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

KindCodeRangeExample
ShortEB d8−128 … +127 bytesjmp short again
NearE9 d16same segment, ±32 Kjmp start
FarEA IP CSany segmentjmp far ptr dst
Indirect nearFF /4IP ← register/memoryjmp bx, jmp jmp_tab[bx]
Indirect farFF /5CS: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 FE jumps to itself (an endless loop): FEh = −2 and the instruction is 2 bytes.
0120: EB 1001220132: target+10h (counted from the next instruction)

Processor Control

Processor Control Instructions

InstructionEffect
CLC / STCCF ← 0 / CF ← 1
CMCCF ← ¬CF
CLD / STDDF ← 0 / 1 (string direction)
CLI / STIIF ← 0 / 1 (INTR interrupts)
InstructionEffect
HLThalt until an interrupt or RESET
WAITwait until the TEST̅ pin goes active (8087 sync)
ESCpass the instruction to the coprocessor (8087)
LOCKprefix: LOCK̅ pin active for the next instruction → bus locked
NOPdo nothing (90h), 3 clocks
CLC/STCA common way to return success/failure from a procedure: stc on error, and the caller does jc fail.
CLI/STIProtect a critical section (e.g. changing SS:SP) from interrupts. NMI can't be masked.
LOCKIn a multiprocessor system lock xchg builds an atomic semaphore.

Flag Effects

Flag Effects: Arithmetic and Logic Instructions

InstructionOFSFZFAFPFCF
ADD ADC SUB SBB CMP NEG******
CMPS SCAS******
INC DEC*****–
MUL IMUL*????*
DIV IDIV??????
DAA DAS?*****
AAA AAS???*?*
AAM AAD?**?*?
AND OR XOR TEST0**?*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

InstructionsFlags affected
MOV XCHG LEA LDS LES XLAT PUSH POP IN OUT LAHF PUSHF CBW CWD NOTnone
MOVS LODS STOS, REPnone
JMP Jcc LOOP LOOPE LOOPNE JCXZ CALL RETnone (only read)
SAHFSF ZF AF PF CF ← AH
POPF, IRETall (from the stack)
INT n, INTOIF ← 0, TF ← 0 (old FLAGS on the stack)
CLC STC CMCCF
CLD STDDF
CLI STIIF
A classic mistake: putting a flag-changing instruction (e.g. 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

EncodingAn instruction is 1–6 bytes: opcode (D, W) + ModR/M + displacement + immediate; prefixes go in front.
ModR/MMOD = 11 register, 00/01/10 memory; MOD = 00, R/M = 110 is a direct address, so [BP] is encoded with d8 = 0.
Data transferXLAT: AL ← [DS:BX + AL]. LEA gives the address, MOV the contents; LDS/LES load a far pointer. No flags change.
StringsDS:SI → ES:DI, DF sets the direction, REP/REPE/REPNE repeat up to CX times; with CX = 0 the instruction never runs.
JMPShort (EB d8) and near (E9 d16) jumps are relative; far and indirect jumps use absolute addresses.
Flag effectsMoves, string moves and jumps leave the flags alone; never put a flag-changing instruction between CMP and Jcc.

Summary

Exercises

  1. Encode mov dx, [bp+di+8] (opcode 100010, d = 1, w = 1).
  2. DS = 1000h, BX = 0200h, AL = 05h: from which physical address does xlat load AL?
  3. farptr dd holds the bytes 34 12 00 B8. What are ES and DI after les di, farptr?
  4. How many bytes does rep movsb copy when CX = 0?
  5. CLD, CX = 5, SI = 0100h, DI = 0200h; repe cmpsb finds a mismatch at byte 3. CX, SI, DI and ZF afterwards?
  6. A jmp short at 0150h jumps to 0130h. What is its machine code?
  7. Count the spaces in a string with repne scasb.
Hints / answers
  1. MOD = 01, REG = 010 (DX), R/M = 011 ([BP+DI]) → 8B 53 08; segment SS.
  2. EA = BX + AL = 0205h → 10000h + 0205h = 10205h.
  3. Offset first, then segment: DI = 1234h, ES = B800h.
  4. 0: REP tests CX first; with CX = 0 the instruction never runs.
  5. Three comparisons: CX = 2, SI = 0103h, DI = 0203h, ZF = 0. The mismatch is at SI − 1 = 0102h.
  6. The next instruction is at 0152h; 0130h − 0152h = −22h = DEh → EB DE.
  7. Count each hit; while CX ≠ 0, repeat repne scasb (DI resumes where it stopped).

Summary

Exercises (2)

  1. Which instruction is 8A 1E 34 12? What do the same bytes decode to one byte later (from offset 1)?
  2. Encode mov es, dx and mov al, [0020h].
Hints / answers
  1. 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.
  2. 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.

The End

Chapter 11: Instruction Encoding and String Instructions
Previous: Chapter 10.2: Device Control
1 / 1 Sercan KÜLCÜ. All rights reserved.