Chapter 11: Instruction Encoding and String Instructions
Multiple Choice · Instruction Format
Question 1: A Valid Instruction
Which of the following instructions is valid on the 8086?
D: An immediate value can be written to a memory destination; the size is given by BYTE PTR. An instruction cannot have two memory operands: memory-to-memory copying uses an intermediate register (mov al, [si] · mov [di], al), MOVSB, or PUSH/POP. An immediate value cannot be written to a segment register (load it into AX first); AL is a byte and WORD PTR a word, so the sizes do not match.
Step-by-Step Trace · Instruction Encoding
Question 2: Decoding Machine Code
The 9 bytes of code starting at 0100h in memory are to be decoded. How many instructions are there, and what is each one?
8Bh = 100010 1 1: the MOV family; D = 1 → the REG field is the destination; W = 1 → word.
47h = 01 000 111: MOD = 01 → an 8-bit displacement (06h) follows. Instruction: mov ax, [bx+06h] (3 bytes).
26h is not an instruction but a segment prefix: the memory operand of the next instruction is read/written through ES instead of DS.
88h = 100010 0 0: D = 0 → the REG field is the source (register to memory); W = 0 → byte.
25h = 00 100 101: since W = 0, REG 100 = AH (it would be SP if W = 1). Instruction: mov es:[di], ah.
05h = 0000010 1: add immediate to the accumulator, W = 1 → 16-bit data: 34 12 little-endian = 1234h. Instruction: add ax, 1234h. A short form without ModR/M.
8B 47 06 26 88 25 05 34 12
Multiple Choice · Instruction Encoding
Question 3: MOD = 00, R/M = 110
Which instruction has the machine code 88 1E 00 10?
C: 88h = 100010 0 0: D = 0 (REG is the source), W = 0 (byte). 1Eh = 00 011 110: REG = 011 (BL); MOD = 00 with R/M = 110 is a special case: it means a 16-bit direct address, not [BP]: 00 10 → 1000h. With D = 1 (8Ah) the direction would reverse and it would be mov bl, [1000h].
Classic Question · Instruction Encoding
Question 4: Instruction Encoding
Find the machine code of the following instructions. The MOV r/m↔reg opcode is 100010, the ADD r/m↔reg opcode is 000000 (followed by the D and W bits).
mov [si+20h], cxadd bx, [1234h]mov [bp], al
Show answer
| Instruction | 1st byte | ModR/M | Code |
|---|---|---|---|
| mov [si+20h], cx | 100010 0 1 = 89h | 01 001 100 = 4Ch | 89 4C 20 |
| add bx, [1234h] | 000000 1 1 = 03h | 00 011 110 = 1Eh | 03 1E 34 12 |
| mov [bp], al | 100010 0 0 = 88h | 01 000 110 = 46h | 88 46 00 |
1: CX is the source → D = 0; MOD = 01 (d8 = 20h), REG = 001 (CX), R/M = 100 ([SI]).
2: BX is the destination → D = 1; MOD = 00, R/M = 110 direct address, followed by 34 12 (little-endian).
3: MOD = 00 cannot be used for [BP] (it is reserved for the direct address); the assembler produces MOD = 01 and d8 = 00. The default segment is SS.
Classic Question · Instruction Encoding
Question 5: Prefix Bytes
Which instructions are the following byte sequences? Prefix bytes: 26h ES, 2Eh CS, F3h REP; FF /4 is an indirect near JMP.
26 8A 07F3 AB2E FF 27
Show answer
| Bytes | Decoding | Instruction |
|---|---|---|
| 26 8A 07 | ES prefix; 8Ah: d = 1, w = 0; 07h = 00 000 111 | mov al, es:[bx] |
| F3 AB | REP prefix; ABh = STOSW | rep stosw |
| 2E FF 27 | CS prefix; 27h = 00 100 111: REG = 100 (/4), R/M = [BX] | jmp word ptr cs:[bx] |
1: MOD = 00, REG = 000 (AL), R/M = 111 ([BX]); the prefix selects ES instead of the default DS.
2: STOSW writes AX to ES:[DI]; with REP it repeats CX times.
3: in the FF byte the REG field is not a register but an operation selector (/4 = indirect near JMP): IP ← the word at CS:[BX].
Multiple Choice · Data Transfer
Question 6: MOV and the Flags
Which statement about the MOV AX, BX instruction is correct?
A: MOV, XCHG, LEA, PUSH and POP (except POPF) do not change the flags; flags are set by ALU operations. The destination is on the left: the contents of BX are copied to AX, BX is unchanged. Both operands are registers, so this is register addressing (an immediate example: MOV AX, 5). MOV cannot have two memory operands.
Multiple Choice · Data Transfer
Question 7: Translation with XLAT
With hex_tab db '0123456789ABCDEF' defined, what is AL after mov bx, offset hex_tab · mov al, 0Bh · xlat?
B: XLAT does AL ← [DS:BX + AL]: hex_tab[11] = 'B' = 42h. Since the index starts at 0, hex_tab[10] = 'A', hex_tab[11] = 'B'. This converts a hexadecimal digit to an ASCII character in a single instruction. XLAT does not change the flags.
Multiple Choice · Data Transfer
Question 8: LEA or MOV?
With BX = 1000h, SI = 0020h and the word at DS:1024h equal to 5A5Ah, what is AX after lea ax, [bx+si+4]?
A: LEA computes the effective address but does not access memory: AX = 1000h + 0020h + 4 = 1024h. With the same operand mov ax, [bx+si+4] would read the contents at that address (5A5Ah). LEA does not affect the flags, which is why lea ax, [bx+si] is also used for addition without disturbing the flags.
Classic Question · Data Transfer
Question 9: A Far Pointer with LES
DS = 2000h. The bytes starting at 2000:0100h are 50 00 00 B8. The following code runs (DF = 0):
les di, [0100h] mov al, 'A' stosb
- What are ES and DI after LES?
- To which physical address does STOSB write?
- What is DI after STOSB?
Show answer
1: In a far pointer the offset is stored first, then the segment (little-endian): DI = 0050h, ES = B800h.
2: STOSB's destination is always ES:DI: B800h × 10h + 0050h = B8050h (row 0, character 40 in text memory).
3: since DF = 0, DI increases by one: 0051h. LDS does the same job with DS.
Multiple Choice · String Processing
Question 10: The Direction Flag
What is the direction flag (DF) in the 8086 FLAGS register for?
D: DF = 0 (cld): MOVSB, LODSB, STOSB, CMPSB and SCASB increment SI/DI at each step; DF = 1 (std): they decrement. The step is 1 for byte instructions and 2 for word instructions. Interrupts are controlled by IF; the 8086 has no privilege levels.
Classic Question · String Processing
Question 11: Clearing a Row with REP STOSW
The following code runs with ES = B800h.
cld mov ax, 0720h mov cx, 80 mov di, 0 rep stosw
- How many bytes are written? What are DI and CX at the end?
- What is seen on the screen?
- What would happen if it started with CX = 0?
Show answer
1: 80 words = 160 bytes; DI += 2 at each step: DI = 00A0h, CX = 0.
2: In each word AL = 20h (space) is written to the even address and AH = 07h (gray attribute) to the odd address: the first row of the screen is cleared.
3: REP tests CX first; if CX = 0, STOSW is never executed and memory is unchanged. This is the difference from LOOP.
Step-by-Step Trace · String Processing
Question 12: REPE CMPSB
Two strings are compared with REPE CMPSB. What are CX, SI, DI and ZF when the instruction ends? At which index is the first difference?
CLD: DF = 0, the comparison will proceed from left to right.
SI = 010Dh: s1 (DS:SI).
DI = 0112h: s2 (ES:DI; in a .COM program ES = DS).
CX = 5: at most 5 comparisons.
'M' = 'M' → ZF = 1; SI and DI advance by one, CX = 4 → continue.
'I' = 'I' → ZF = 1, CX = 3.
'C' = 'C' → ZF = 1, CX = 2.
'R' (52h) − 'S' (53h): ZF = 0 → REPE stops. CX = 1, SI = 0111h, DI = 0116h; since 'R' < 'S', CF = 1. The difference is at index SI − 1 − s1 = 3 (counting from 0).
ORG 100h cld lea si, s1 lea di, s2 mov cx, 5 repe cmpsb ret s1 db 'MICRO' s2 db 'MICSA'
Output Question · String Processing
Question 13: Overlapping Copy
The string is to be shifted one byte to the right. What is the text when the program ends? What would happen if cld were used instead of std, with SI = msg and DI = msg+1?
std
lea si, msg+3 ; last character
lea di, msg+4
mov cx, 4
rep movsb
...
msg db 'ABCD', '.'
Show answer
With STD: AABCD. The copy runs from the end to the start: D → 4th byte, C → 3rd, B → 2nd, A → 1st; each source byte is read before it is overwritten.
With CLD: AAAAA. In the first step A overwrites B; the next step reads this new A, and the same character spreads through the whole string.
Rule: if the destination overlaps the source on its right, copy from the end to the start (STD); if on its left, from the start to the end (CLD).
Classic Question · Control Transfer
Question 14: Jump Distance
In a relative jump the target = address of the next instruction + displacement. Short JMP: EB d8 (2 bytes), near JMP: E9 d16 (3 bytes).
- The
jmp backat 011Ch jumps to 0100h. What is its machine code? - Between the
jmp forwardat 0100h and its target there are 127 bytes of data. Which form is chosen? - What if there were 128 bytes in between?
Show answer
- The next instruction is at 011Eh; 0100h − 011Eh = −1Eh = E2h → EB E2.
- In the short form the next address is 0102h, the target is 0102h + 7Fh = 0181h; displacement 7Fh = +127 fits → EB 7F.
- +128 does not fit the short range (−128 … +127); the near form is needed. With a 3-byte instruction the target is 0103h + 80h = 0183h → E9 80 00.
Relative encoding lets code be moved in memory. On the 8086 conditional jumps have only the short form; for a distant target the condition is inverted and a near JMP is used.
Multiple Choice · Processor Control
Question 15: The HLT Instruction
Which of the following is not true about the HLT instruction?
C: HLT puts the processor into the halt state; it does not start a program. Leaving that state requires a hardware interrupt (INTR, NMI) or RESET. Other processor control instructions: NOP, WAIT, ESC, LOCK, CLC/STC/CMC, CLD/STD, CLI/STI.
Spot the Bug · Flag Effects
Question 16: The Vanishing ZF
With AX = BX = 5, DX is expected to be 2. What is DX when the program ends? Where is the bug, and how is it fixed?
mov cx, 0
cmp ax, bx
inc cx ; counter
je equal
mov dx, 1
jmp done
equal: mov dx, 2
done:
Show answer
DX = 1.
CMP sets ZF = 1 for equality, but the inc cx in between sets ZF again according to its own result (CX = 1 ≠ 0): ZF = 0 and JE does not jump. INC/DEC preserve only CF; they change OF, SF, ZF, AF and PF.
Fix: move inc cx before the CMP. Only instructions that do not affect the flags, such as MOV, LEA, PUSH and XCHG, may come between CMP and Jcc.