Chapter 2.2: 8086 Registers

Chapter 2.2: 8086 Registers · Study Questions
Register setAH ve ALCX and LOOPMUL / DIVSPSI, DI and DFStatus flagsControl flagsFlags and IP
Sercan KÜLCÜ · Giresun University · Computer Engineering

Multiple Choice · Register Set

Question 1: The Job of the Segment Registers

What do the CS, DS, SS and ES segment registers hold on the 8086?

A: A segment register holds a paragraph number: segment base = register × 10h. If DS = 2000h, the data segment starts at 20000h and extends to 2FFFFh. Registers such as IP, SP, BP, SI, DI and BX hold the offsets within a segment; data and results are in the general-purpose registers.

Output Question · Register Set

Question 2: AH and AL

What are AX and CF after each instruction?

MOV AX, 12F0h
ADD AL, 20h
INC AH
ADD AX, 0F0h
Show answer
InstructionAXCF
MOV AX, 12F0h12F0h0
ADD AL, 20h1210h1
INC AH1310h1
ADD AX, 0F0h1400h0

F0h + 20h = 110h: in an 8-bit operation the carry does not move into AH, it goes to CF; AL = 10h and AH is unchanged. INC does not touch CF. The last addition is 16-bit: 1310h + 00F0h = 1400h, and the carry moves into AH.

Step-by-Step Trace · Register Set

Question 3: 8-Bit Register Parts

Initially the registers are 0. (a) What are AX, BX and CX after the code runs? (b) What if the first line were MOV AX, 0x4231 and the last line were MOV CH, AH?

AX4321AHALBX0000BHBLCX0000CHCL

AX ← 4321h: AH = 43h, AL = 21h. The 0x prefix means hexadecimal.

AX4321AHALBX0021BHBLCX0000CHCL

BL ← AL = 21h. Only the low byte of BX is written; BH stays 00h → BX = 0021h.

AX4321AHALBX0021BHBLCX0043CHCL

CL ← AH = 43h → CX = 0043h. MOV affects no flags. (b) With MOV AX, 0x4231 · MOV BL, AL · MOV CH, AH: AX = 4231h, BX = 0031h, CX = 4200h.

MOV AX, 0x4321
MOV BL, AL
MOV CL, AH

Multiple Choice · General-Purpose Registers

Question 4: The CX Register

For which task is the CX register used implicitly (without being written in the instruction)?

B: CX (count): LOOP first decrements CX and jumps if it is not zero; REP repeats the string instruction CX times; in multi-bit shifts such as SHL AX, CL the count is in CL. The high half and the port number are DX's job, the string destination is DI's, and the stack-frame base is BP's.

Multiple Choice · General-Purpose Registers

Question 5: A Register That Can Hold an Address

On the 8086, which of the following instructions does not assemble?

C: Only BX, BP, SI and DI (and a displacement) can be used inside square brackets. AX, CX and DX are not addressing registers; among the data registers, BX is the only one that can hold an address. Correct form: MOV BX, CX + MOV AX, [BX].

Step-by-Step Trace · General-Purpose Registers

Question 6: MUL and DIV with DX:AX

After each instruction, what are DX, AX, BX and CX? Why are CF and OF 1 after MUL? Where are the quotient and the remainder of DIV written?

DX0000hAX0300hBX0000hCX0000hCF0OF0DX:AX = 00000300h

AX = 0300h (768).

DX0000hAX0300hBX0200hCX0000hCF0OF0DX:AX = 00000300h

BX = 0200h (512).

DX0006hAX0000hBX0200hCX0000hCF1OF1DX:AX = 00060000h

16-bit MUL: DX:AX = AX × BX = 768 × 512 = 393,216 = 0006 0000h → DX = 0006h, AX = 0000h. The high half is not zero → CF = OF = 1; SF, ZF, AF, PF are undefined.

DX0006hAX0000hBX0200hCX0007hCF1OF1DX:AX = 00060000h

CX = 7. The 32-bit value in DX:AX stays as the dividend.

DX0005hAXDB6DhBX0200hCX0007hCF?OF?DX:AX = 0005DB6Dh

DIV CX: 393,216 / 7 = 56,173 remainder 5 → quotient AX = DB6Dh, remainder DX = 0005h. The quotient fits in 16 bits, so there is no divide error; the six flags are undefined (?).

mov ax, 0300h
mov bx, 0200h
mul bx
mov cx, 0007h
div cx

Output Question · General-Purpose Registers

Question 7: How Many Times Does LOOP Run?

What is AX when the program finishes? If the first line were MOV CX, 0 instead, how many times would the loop run, and what would AX be?

MOV CX, 4
MOV AX, 1
D: ADD AX, AX
LOOP D
Show answer

The body runs for CX = 4, 3, 2, 1; AX doubles on each pass: 1 → 2 → 4 → 8 → 16. AX = 0010h, CX = 0.

LOOP first decrements CX, then tests it. If entered with CX = 0, the first LOOP makes CX FFFFh; the loop runs 65,536 times. 265536 does not fit in 16 bits: after the 16th pass AX = 0 and stays that way → AX = 0000h.

To guard against this trap, before the loop JCXZ is used to skip the CX = 0 case.

Multiple Choice · Pointer and Index Registers

Question 8: The Stack Pointer

Which statement about the stack pointer (SP) is correct?

D: SP is 16 bits and holds the offset of the top of the stack within the SS segment. The stack grows toward lower addresses: PUSH does SP ← SP − 2, then writes; POP reads first, then SP ← SP + 2. Items on the stack are always 16 bits; POP does not erase the old cell, it merely regards it as invalid.

Output Question · Pointer and Index Registers

Question 9: Copying Backwards

DS = ES, and the string "ABCDE" is at DS:0200h–0204h. What are SI, DI and CX when the program finishes? Which bytes are copied, in which order? What ends up at DS:0300h–0304h?

STD
MOV SI, 0204h
MOV DI, 0304h
MOV CX, 5
REP MOVSB
Show answer

STD → DF = 1: at each step SI and DI decrease by 1. The copy runs from the end to the start: 0204h → 0304h ('E'), 0203h → 0303h ('D'), …, 0200h → 0300h ('A').

Result: SI = 01FFh, DI = 02FFh, CX = 0; DS:0300h–0304h = "ABCDE", the order is preserved.

Copying backwards is needed for overlapping areas where the destination lies slightly ahead of the source; copying forward (CLD) would overwrite source bytes that have not been read yet.

Multiple Choice · Pointer and Index Registers

Question 10: Operands of a String Instruction

MOVSB instruction: what are its source and destination?

A: In string instructions the source is always DS:SI and the destination always ES:DI; the operands are not written in the instruction (implicit). After each step SI and DI increase or decrease by 1 (byte) or 2 (word) according to DF. Outside string instructions, [DI] is an ordinary address and uses the DS segment.

Multiple Choice · Flags Register

Question 11: Status or Control?

Which of the following flags is not a status flag but a control flag?

B: The processor itself sets the status flags (CF, PF, AF, ZF, SF, OF) after every arithmetic/logic operation according to the result. The programmer sets the control flags (TF, IF, DF): with TF = 1, INT 1 occurs after every instruction and the program can be run one step at a time.

Multiple Choice · Flags Register

Question 12: The Flags Register

Which statement about the 16-bit flags register (FLAGS) is correct?

D: The status flags CF, PF, AF, ZF, SF, OF describe the result of the last arithmetic/logic operation and are set by hardware. The control flags TF, IF, DF manage the behavior of the processor; the programmer sets them with STD/CLD, STI/CLI or PUSHF/POPF. The remaining 7 bits are unused.

Step-by-Step Trace · Flags Register

Question 13: Tracing the Flags

After each instruction, what are AL and CF, ZF, SF, OF, PF, AF? (The flags are 0 initially.)

AL64h= 0110 0100bsigned: 100CF0ZF0SF0OF0PF0AF0

AL = 64h (+100). MOV does not change the flags.

AL96h= 1001 0110bsigned: −106CF0ZF0SF1OF1PF1AF0

64h + 32h = 96h: +100 + 50 = 150 > 127 → OF = 1, SF = 1. Unsigned 150 is correct → CF = 0. 4 + 2 produces no nibble carry → AF = 0; 1001 0110 has four 1s → PF = 1.

ALFFh= 1111 1111bsigned: −1CF1ZF0SF1OF0PF1AF1

96h − 97h = FFh: a larger value was subtracted from a smaller one → CF = 1; AF = 1. Signed −106 − (−105) = −1 is correct → OF = 0.

AL00h= 0000 0000bsigned: 0CF1ZF1SF0OF0PF1AF1

FFh + 1 = 00h → ZF = 1. INC does not change CF: CF stays 1.

AL00h= 0000 0000bsigned: 0CF1ZF0SF1OF0PF1AF1

CMP AL, 1: 00h − 01h is computed but not written to AL. Borrow → CF = 1, result FFh → SF = 1, ZF = 0. AL is still 00h.

mov al, 64h
add al, 32h
sub al, 97h
inc al
cmp al, 1

Multiple Choice · Flags Register

Question 14: Reading the Flags

While AL = 80h, ADD AL, 80h executes. What are the flags afterward?

C: 80h + 80h = 100h → AL = 00h, the 1 that spills out → CF = 1; the result is zero → ZF = 1; MSB is 0 → SF = 0. In the signed interpretation −128 + (−128) = −256 does not fit and the sum of two negatives came out positive (0) → OF = 1.

Classic Question · Flags Register

Question 15: Computing Flags by Hand

After each operation, find the result and the flags CF, ZF, SF, OF, PF, AF.

  1. MOV AX, 7FF8h / ADD AX, 0009h
  2. MOV BX, 0005h / SUB BX, 0007h
Show answer
ResultCFZFSFOFPFAF
18001h001101
2FFFEh101001
  1. +32760 + 9 = 32769 > 32767 → OF = 1, MSB = 1 → SF = 1; the unsigned sum fit → CF = 0. 8h + 9h carries out of the low nibble → AF = 1. PF looks only at the low byte: 01h has one 1 → PF = 0.
  2. 5 − 7: borrow → CF = 1; signed 5 − 7 = −2 is correct → OF = 0. 5h − 7h borrows from the nibble → AF = 1. The low byte FEh has seven 1s → PF = 0.

Step-by-Step Trace · Flags Register

Question 16: Flags and IP

A .COM program starts at 0100h; the registers and flags are 0. After each instruction, what are IP, AX, BL and CF, ZF, SF, OF, PF, AF?

IP0103hAX7FF0hBL00hCF0ZF0SF0OF0PF0AF0

AX = 7FF0h. The instruction is 3 bytes → IP = 0103h. MOV does not change the flags.

IP0106hAX8000hBL00hCF0ZF0SF1OF1PF1AF0

7FF0h + 0010h = 8000h. Signed +32752 + 16 = +32768 does not fit in 16 bits → OF = 1, SF = 1. No carry: CF = 0. The low byte is 00h (zero 1s) → PF = 1.

IP0108hAX8000hBLF0hCF0ZF0SF1OF1PF1AF0

BL = F0h. An 8-bit MOV, 2 bytes → IP = 0108h.

IP010BhAX8000hBL10hCF1ZF0SF0OF0PF0AF0

F0h + 20h = 110h → BL = 10h, CF = 1. Signed −16 + 32 = 16 is correct → OF = 0. 10h has a single 1 → PF = 0.

IP010DhAX8000hBL0FhCF1ZF0SF0OF0PF1AF1

10h − 1 = 0Fh: borrow from bit 4 → AF = 1. DEC does not change CF: CF stays 1.

IP0110hAX7FFFhBL0FhCF0ZF0SF0OF1PF1AF1

8000h − 1 = 7FFFh: −32768 − 1 does not fit → OF = 1. Unsigned 32768 − 1 is correct → CF = 0.

IP0000hAX7FFFhBL0FhCF0ZF0SF0OF1PF1AF1

RET: a .COM program returns to DOS. IP had advanced from 0100h to 0110h according to the instruction lengths; RET loads the 0000h on the stack into IP.

org 100h
mov ax, 7FF0h
add ax, 0010h
mov bl, 0F0h
add bl, 20h
dec bl
sub ax, 1
ret

Coding Question · Flags Register

Question 17: Setting the Trap Flag

No instruction sets TF (bit 8) directly. Write a short piece of code that sets TF to 1 without changing the other flags. What happens after each instruction once TF = 1? Why is no such code needed for IF and DF?

Show answer
PUSHF            ; FLAGS to the stack
POP AX           ; AX = FLAGS
OR AX, 0100h     ; bit 8 = TF ← 1
PUSH AX
POPF             ; FLAGS = AX

OR sets only bit 8 to 1; the other bits are preserved. To clear it, use AND AX, 0FEFFh.

With TF = 1, a type 1 interrupt (INT 1) occurs after every instruction; a debugger runs the program instruction by instruction. IF and DF have their own instructions: STI/CLI and STD/CLD.

The End

Chapter 2.2: 8086 Registers: Study Questions
Lecture slides: Chapter 2.2: 8086 Registers · Lecture notes
1 / 1 Sercan KÜLCÜ. All rights reserved.