Chapter 7: Flow Control
Multiple Choice · Unconditional Jump
Question 1: The JMP Instruction
Which statement about the JMP instruction is true?
C: JMP is unconditional: it always loads a new value into IP and does not touch the flags. The short form is EB + a 1-byte signed displacement (−128…+127); for farther targets the 3-byte near form (E9 rel16) is used. It can jump forward and backward; the instructions that look at CX are the LOOP family.
Multiple Choice · Unconditional Jump
Question 2: A Valid Label
Which of the following label definitions is valid?
D: A label may contain letters, digits and _ but cannot start with a digit (A). Instruction and register names cannot be labels (B). It cannot contain spaces (C). A label takes no space in memory; the assembler turns it into an address (offset).
Classic Question · Unconditional Jump
Question 3: Computing the Displacement
The instructions are 2 bytes (opcode + rel8). What are the machine codes?
(a) At 0105h: JMP SHORT, target 0130h
(b) At 0120h: JNE (75h), target 0100h
(c) At 0100h: JE, target 0190h
Show answer
Displacement = target − (instruction address + 2).
- (a) 0130h − 0107h = 29h → EB 29.
- (b) 0100h − 0122h = −22h; two's complement → 75 DE. The processor computes 0122h + FFDEh = 0100h (the overflowing bit is discarded).
- (c) 0190h − 0102h = 8Eh = 142 > 127: outside the short range. The 8086 has no long form of a conditional jump; the instruction does not fit in 2 bytes (the assembler reports an error or generates the pair below itself). Solution: a short jump with the opposite condition (
JNE cont) followed byJMPto reach the target.
Multiple Choice · Conditional Jumps
Question 4: The Flag No Jump Uses
Which of the following flags is never tested by any conditional jump instruction?
B: No jump tests AF; AF is used only by the BCD adjust instructions (DAA, DAS, AAA, AAS). ZF → JZ/JNZ, CF → JC/JNC, PF → JP/JNP, SF → JS, OF → JO. Instructions such as JA and JG look at more than one flag.
Multiple Choice · Conditional Jumps
Question 5: A Synonymous Jump
Which instruction produces the same machine code (73h) as JNC?
A: JNC = JNB = JAE: all three test the condition CF = 0. In an unsigned subtraction, if A ≥ B there is no borrow and CF = 0. JNA (CF = 1 or ZF = 1), JGE (SF = OF) and JNS (SF = 0) are different conditions.
Output Question · Comparison
Question 6: JA or JG?
What are BL and BH after the code runs? Why do they come out different? (BX = 0000h at the start.)
MOV AL, 0F0h CMP AL, 10h JA UNSIGNED_ABOVE MOV BL, 0 JMP CONTINUE UNSIGNED_ABOVE: MOV BL, 1 CONTINUE: CMP AL, 10h JG SIGNED_GREATER MOV BH, 0 JMP DONE SIGNED_GREATER: MOV BH, 1 DONE: RET
Show answer
BL = 01h, BH = 00h.
CMP AL, 10h: F0h − 10h = E0h → CF = 0, ZF = 0, SF = 1, OF = 0.
- JA (unsigned; CF = 0 and ZF = 0): 240 > 16 → jumps, BL = 1.
- JG (signed; ZF = 0 and SF = OF): SF ≠ OF → does not jump. Read as signed, F0h = −16 < 16 → BH = 0.
Same bits, different decision: choose the JA/JB family for unsigned data and the JG/JL family for signed data.
Multiple Choice · Comparison
Question 7: 80h versus 1
MOV AL, 80h and CMP AL, 1 are executed. Do JA and JG jump?
B: 80h − 01h = 7Fh: CF = 0, ZF = 0 → JA jumps (unsigned 128 > 1). Read as signed, −128 − 1 = −129 does not fit: OF = 1, and SF = 0 in the result. SF ≠ OF → JG does not jump (−128 < 1). Because overflow flips the sign bit, the decision is made by SF and OF together.
Spot the Bug · Comparison
Question 8: The if / else Bug
The code is meant to set CX = 1 if AX = BX and CX = 2 otherwise. What is CX when AX = BX = 5? Where is the bug, and how is it fixed?
MOV AX, 5 MOV BX, 5 CMP AX, BX JNE not_equal MOV CX, 1 ; block A (equal) not_equal: MOV CX, 2 ; block B (not equal) done: RET
Show answer
CX = 2 (wrong).
After CMP, ZF = 1 and JNE does not jump; block A sets CX = 1. But when the block ends, the processor moves on to the next line, that is block B, and CX becomes 2. A JMP that skips over B is missing at the end of block A:
MOV CX, 1
JMP done ; skip block B
not_equal:
MOV CX, 2
done:Pattern: block A is skipped with the opposite condition (JNE), and block B is skipped with a JMP at the end of block A.
Multiple Choice · Comparison
Question 9: An Out-of-Range Jump
CMP AL, 0 and JE far_away: the assembler reports a "relative jump out of range" error on the JE line (the target is 200 bytes ahead). Which is the correct fix?
B: On the 8086 conditional jumps exist only in the rel8 (−128…+127) form. The condition is inverted: if not equal, a short jump goes to the next line; if equal, the near JMP (E9, anywhere within the segment) takes you to the far target. JE and JZ are the same machine code (A); SHORT does not extend the range (C); LOOPE is rel8 too and changes CX (D). emu8086 and MASM 6 can generate this inverted condition + JMP pair automatically; old MASM and TASM (without JUMPS) treat the line as an error.
Coding Question · Comparison
Question 10: Range Check
AL holds a signed number. Write code that sets BL = 1 if the number is between −10 and 10 (inclusive) and BL = 0 otherwise. Which family of jumps should be chosen?
Show answer
MOV BL, 0 CMP AL, -10 JL outside CMP AL, 10 JG outside MOV BL, 1 outside:
Because the value is signed, JL/JG are used. With JB/JA, −10 = F6h = 246 would be read as unsigned: since 5 < 246, even 5 would be counted as out of range.
Tested: −11 → 0, −10 → 1, 0 → 1, 10 → 1, 11 → 0, −128 → 0, 127 → 0.
Step-by-Step Trace · Loops
Question 11: A Loop with DEC and JNZ
What are AX and CX after the code runs? What if NUMBER DW 6?
JMP START: jumps over the NUMBER data; otherwise the bytes 04h 00h would be taken for instructions.
CX = NUMBER = 4 (counter), AX = 0.
Round 1: AX = 4. DEC CX → 3, ZF = 0 → JNZ jumps back.
Round 2: AX = 8, CX = 2.
Round 3: AX = 0Ch, CX = 1.
Round 4: AX = 10h (4 × 4 = 16). DEC CX → 0, ZF = 1: JNZ does not jump, the loop ends. If NUMBER = 6, then 6 × 6 = 36 = 0024h.
ORG 100h JMP START NUMBER DW 4 START: MOV CX, NUMBER MOV AX, 0 AGAIN: ADD AX, NUMBER DEC CX JNZ AGAIN RET
Multiple Choice · Loops
Question 12: LOOP with CX = 0
How many times does the body of the loop below (INC AX) run?
MOV CX, 0 MOV AX, 0 d: INC AX LOOP d
D: LOOP first decrements CX, then checks whether it is zero. With CX = 0 the first LOOP makes CX FFFFh; the loop continues until CX is 0 again: 65,536 rounds (AX ends up 0000h again). To prevent this, before the loop use JCXZ to skip the CX = 0 case. LOOP always uses CX as its counter.
Output Question · Loops
Question 13: Fibonacci with LOOP
After the code runs, what are AX, BX, CX and DX? (b) What if the first three lines were MOV AX, 1, MOV BX, 2, MOV CX, 4?
MOV AX, 0 MOV BX, 1 MOV CX, 5 AGAIN: MOV DX, AX ADD AX, BX MOV BX, DX LOOP AGAIN RET
Show answer
AX = 0005h, BX = 0003h, CX = 0000h, DX = 0003h.
| Round | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| AX | 1 | 1 | 2 | 3 | 5 |
| BX | 0 | 1 | 1 | 2 | 3 |
DX saves the old AX, AX ← AX + BX, BX ← old AX. LOOP: CX ← CX − 1, jump if CX ≠ 0.
(b) AX = 000Bh, BX = DX = 0007h, CX = 0000h.
Multiple Choice · Loops
Question 14: The EQU Constant and 10 Rounds
What is AX after the code runs?
max_iterations EQU 10 XOR AX, AX MOV BX, 1 MOV CX, max_iterations loop1: MOV DX, AX ADD AX, BX MOV BX, DX LOOP loop1
A: max_iterations is an EQU constant: it takes no space in memory, and the assembler substitutes 10 at every use. XOR AX, AX clears AX (it is shorter than MOV AX, 0). In 10 rounds AX advances through the Fibonacci sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 → 55 = 0037h (BX = 34 = 0022h). 0055h is what you get by mistaking 55 for hexadecimal.
Output Question · Loops
Question 15: LOOP, or SUB and JNZ?
What are total and AX when the program ends? Would the result change if loop l1 were replaced by sub cx, 1 and jnz l1? How do the two forms differ?
mov bx, offset num1 mov cx, 10 mov ax, 0 l1: add ax, [bx] add bx, 2 loop l1 mov [total], ax ret num1: dw 10, 20, 30, 40, 50 dw 10, 20, 30, 40, 50 total: dw 0
Show answer
total = AX = 012Ch (10 + 20 + 30 + 40 + 50 = 150, twice → 300).
BX increases by 2 each round, because the elements are DW (2 bytes).
The result does not change: both forms run 10 rounds. The difference is in the flags: LOOP decrements CX without touching the flags and is 2 bytes; SUB CX, 1 changes the ZF, SF, CF… flags, and JNZ looks at ZF. Also, with SUB/JNZ any register can be used as the counter.
Step-by-Step Trace · Loops
Question 16: Comparing Strings with LOOPE
Two strings are compared with LOOPE. After how many rounds does the loop end? What are BX, CX and DL at the end?
BX = −1 (FFFFh): the first INC makes it 0. At most 4 rounds.
Round 1: 'A' = 'A' → ZF = 1. LOOPE: CX = 3 ≠ 0 and ZF = 1 → continue.
Round 2: 'B' = 'B' → ZF = 1, CX = 2 → continue.
Round 3: 'C' ≠ 'X' → ZF = 0. LOOPE still decrements CX (1) but leaves the loop because ZF = 0.
The LOOP family does not touch the flags: ZF = 0 was preserved, JNE jumps to different. DL = 0, BX = 2 is the first position that differs.
ORG 100h MOV BX, -1 MOV CX, 4 compare: INC BX MOV AL, s1[BX] CMP AL, s2[BX] LOOPE compare JNE different MOV DL, 1 ; equal RET different: MOV DL, 0 ; different RET s1 DB 'ABCD' s2 DB 'ABXD'
Output Question · Example Programs
Question 17: Indexed Addressing and CMP
(a) What is total? (b) What if cmp bx, 20 were replaced by cmp bx, 18? (c) Why is the first line jmp start needed?
jmp start num1: dw 10, 20, 30, 40, 50 dw 10, 20, 30, 40, 50 total: dw 0 start: mov bx, 0 mov ax, 0 l1: add ax, [num1+bx] add bx, 2 cmp bx, 20 jne l1 mov [total], ax ret
Show answer
(a) total = 012Ch (300). BX = 0, 2, …, 18 → 10 elements; when BX = 20, ZF = 1 and JNE does not jump (20 = 10 elements × 2 bytes).
(b) The loop ends when BX = 18, so the last element is not added: 300 − 50 = 250 = 00FAh.
(c) The data comes before the code. Without the JMP, the processor would take the data bytes 0Ah 00h 14h… for instructions and execute them.
[num1+bx]: a fixed address + an index register (indexed addressing).
Step-by-Step Trace · Example Programs
Question 18: Bubble Sort
What is the array when the program ends? How many passes are made, and how many swaps in total? (The steps show the end of each pass.)
Start: 60h 55h 45h 50h 40h. In each pass neighboring bytes are compared; the larger one moves right.
Pass 1: four swaps. The largest, 60h, reached the far right. swapped = 1 → JE start: a new pass.
Pass 2: three swaps (55h moved right).
Pass 3: only 50h and 40h were swapped.
Pass 4: 45h and 40h were swapped. The array is now sorted, but the program does not know it yet.
Pass 5: no swaps at all → swapped = 0, JE does not jump, RET. Result: 40h 45h 50h 55h 60h; 5 passes, 9 swaps, 20 comparisons. JBE is an unsigned comparison.
ORG 100h jmp start arr db 60h, 55h, 45h, 50h, 40h swapped db 0 start: mov bx, 0 mov swapped, 0 scan: mov al, arr[bx] cmp al, arr[bx+1] jbe sorted mov dl, arr[bx+1] mov arr[bx+1], al mov arr[bx], dl mov swapped, 1 sorted: inc bx cmp bx, 4 jne scan cmp swapped, 1 je start ret