Chapter 7: Flow Control
Introduction
Learning Outcomes
Direct program flow with JMP and labels; explain how IP changes and how the displacement (offset) is computed.
State which flag (ZF, CF, SF, OF, PF) each conditional jump tests and name its opposite.
After CMP, choose JA/JB for unsigned numbers and the JG/JL family for signed ones.
Write counted loops with LOOP, LOOPE/LOOPZ, LOOPNE/LOOPNZ and JCXZ; trace the example programs step by step.
Introduction
Program Flow Control
- Normally the CPU executes instructions in memory order: after each instruction, IP (Instruction Pointer) advances by the instruction's length.
- Flow control instructions load a new value into IP and move execution somewhere else.
- This is how decisions (if/else) and loops (for/while) are built at the machine level.
JMP target: always jumps.LOOP, LOOPE/LOOPZ, LOOPNE/LOOPNZ, JCXZ.Unconditional Jump
JMP: Unconditional Jump
JMP targettransfers control to another point in the program unconditionally; it does not change the flags.- It can jump both forward and backward.
- A near jump reaches anywhere in the current code segment (64 KB = 65,536 bytes, offsets 0000h–FFFFh); only IP changes.
- A far jump changes CS and IP together and moves to another segment.
- If the target is close, the assembler can pick the 2-byte short form automatically.
| Form | Code | Bytes | Range |
|---|---|---|---|
JMP SHORT | EB rel8 | 2 | −128…+127 |
JMP NEAR | E9 rel16 | 3 | within segment |
JMP FAR | EA ofs seg | 5 | CS:IP |
JMP reg/mem | FF /4 | 2–4 | indirect |
Unconditional Jump
Labels
- To define a label, write its name followed by
::again: - It may contain letters, digits and characters such as
_, but it cannot start with a digit. - It can stand on its own line or in front of an instruction:
back: jmp stop - A label takes no memory; the assembler turns it into an address (offset).
- Instruction and register names (
mov,ax…) cannot be labels; each label is defined once.
Valid
again:next_char:k1:calc:
Invalid
1start:starts with a digitmov:instruction nametwo words:contains a space
Unconditional Jump · Step through the code
Code Trace: Forward and Backward with JMP
The program starts at address 0100h (org 100h, a .COM program). IP = 0100h.
mov ax, 5 (3 bytes) executed: AX = 0005h, IP = 0103h.
mov bx, 2: BX = 0002h. Next is the jmp calc at 0106h.
jmp calc: IP ← calc = 010Ah. The back: line in between is skipped.
add ax, bx: AX = 5 + 2 = 0007h.
jmp back: a backward jump, IP ← 0108h.
back: jmp stop: forward again, IP ← 010Eh.
ret: IP ← 0000h, the program ends. Result AX = 0007h. Flow: 1 → 2 → 3 → calc → back → stop.
org 100h mov ax, 5 ; ax = 5 mov bx, 2 ; bx = 2 jmp calc ; go to 'calc' back: jmp stop ; go to 'stop' calc: add ax, bx ; ax = ax + bx jmp back ; back to 'back' stop: ret ; return to the OS
Unconditional Jump
Computing the Displacement
Forward jump: the distance is measured from the address after the jump → +2.
At run time the CPU computes IP ← next IP + rel8 = 010Ah + 4 = 010Eh.
Backward jump: the distance is negative and stored in two's complement: −6 → FAh. 010Eh + FFFAh = 0108h (the carry out is dropped).
Conditional Jumps
The Idea of a Conditional Jump
Jcc target: if the condition (cc) holds, IP ← target; otherwise the next instruction runs.- The condition depends on the flags set by an earlier instruction (usually
CMP,SUB,ADD,TEST,DEC). - Jump instructions themselves do not change the flags.
Conditional Jumps
Jumps That Test a Single Flag
| Instruction | Meaning | Condition | Opposite |
|---|---|---|---|
| JZ, JE | Jump if Zero (Equal) | ZF = 1 | JNZ, JNE |
| JC, JB, JNAE | Jump if Carry (Below, Not Above or Equal) | CF = 1 | JNC, JNB, JAE |
| JS | Jump if Sign: result negative | SF = 1 | JNS |
| JO | Jump if Overflow: signed overflow | OF = 1 | JNO |
| JPE, JP | Jump if Parity Even | PF = 1 | JPO, JNP |
| JNZ, JNE | Jump if Not Zero (Not Equal) | ZF = 0 | JZ, JE |
| JNC, JNB, JAE | Jump if Not Carry (Not Below, Above or Equal) | CF = 0 | JC, JB, JNAE |
| JNS | Jump if Not Sign: not negative | SF = 0 | JS |
| JNO | Jump if Not Overflow | OF = 0 | JO |
| JPO, JNP | Jump if Parity Odd (No Parity) | PF = 0 | JPE, JP |
The "C" in JC is the carry flag; "overflow" refers to OF. They are different things.
Conditional Jumps
Encoding and Synonyms
- On the 8086, conditional jumps have a fixed length: 2 bytes = opcode + a 1-byte displacement (rel8).
- rel8 is always signed: relative to the next instruction, a jump can go 128 bytes back or 127 bytes forward.
- Different names for the same condition (synonyms) assemble to the same machine code: JE = JZ, JNE = JNZ, JC = JB = JNAE, JNC = JNB = JAE.
- Conditional jumps with 16-bit displacements (0F 8x) came only with the 80386; the 8086 does not have them.
All are 2 bytes: opcode + rel8. The short JMP opcode is EBh.
Conditional Jumps · Step through the code
Code Trace: JNC = JNB = JAE
All three test the same condition (CF = 0) and produce the same opcode (73h); only rel8 differs, because each sits at a different address.
CF = 0, so jnc a jumps: IP ← 0102h + 07h = 0109h. The jnb, jae and mov ax, 4 in between do not run.
a: mov ax, 5 → AX = 0005h.
ret: IP ← 0000h. Had CF been 1, none of the three would jump, and AX would become 4, then 5.
org 100h jnc a ; 0100h: 73 07 jnb a ; 0102h: 73 05 jae a ; 0104h: 73 03 mov ax, 4 ; 0106h: B8 04 00 a: mov ax, 5 ; 0109h: B8 05 00 ret ; 010Ch: C3
Conditional Jumps
Jump Code Calculator
Enter the address of the jump and the target address (hex) to get the rel8 / rel16 value and the machine code.
displacement = target − (address + instruction length). In the short form the result must be within −128…+127; otherwise the assembler reports "relative jump out of range".Comparison
The CMP Instruction
- Use
CMP dest, srcto compare numbers. - CMP performs a SUB:
dest − src, but it discards the result; it only sets the flags (CF, ZF, SF, OF, PF, AF). - The conditional jump that follows decides based on these flags.
- Operands: register–register, register–memory, memory–register, register/memory–immediate. Two memory operands are not allowed.
JE/JZ jumps.Comparison
How Does CMP AL, BL Set the Flags?
Positive difference: no flag is set except PF (PF = 1 because 03h = 0000 0011 has two 1s).
Equal numbers: the result is 0 → ZF = 1. Equal whether signed or unsigned.
Subtracting a larger number needs a borrow → CF = 1; the result FEh = −2 → SF = 1, no overflow.
127 − (−128) = 255 does not fit in a signed byte → OF = 1. Unsigned, 127 < 128 (CF = 1); signed, 127 > −128 (SF = OF).
FBh is 251 unsigned and −5 signed. Unsigned: 251 > 3 (CF = 0); signed: −5 < 3 (SF ≠ OF). Same bits, different decision!
Comparison
Comparison Calculator
Enter two bytes (decimal −128…255 or hex such as 7Fh) to see the result of CMP A, B, the flags and which jumps are taken.
Comparison
Jumps for Signed Numbers
| Instruction | Meaning | Condition | Opposite |
|---|---|---|---|
| JE, JZ | Jump if Equal (=) · Jump if Zero | ZF = 1 | JNE, JNZ |
| JNE, JNZ | Jump if Not Equal (<>) · Jump if Not Zero | ZF = 0 | JE, JZ |
| JG, JNLE | Jump if Greater (>) · Not Less or Equal | ZF = 0 and SF = OF | JNG, JLE |
| JL, JNGE | Jump if Less (<) · Not Greater or Equal | SF ≠ OF | JNL, JGE |
| JGE, JNL | Jump if Greater or Equal (>=) · Not Less | SF = OF | JNGE, JL |
| JLE, JNG | Jump if Less or Equal (<=) · Not Greater | ZF = 1 or SF ≠ OF | JNLE, JG |
<> means "not equal". Greater / Less assume the numbers are signed, in two's complement.Comparison
Why SF = OF?
- CMP A, B computes A − B. Without overflow the sign of the result is correct: SF = 1 ⇔ A < B.
- With overflow (OF = 1) the result did not fit in 8/16 bits and the sign bit is flipped.
- So the true sign is SF XOR OF: SF ≠ OF ⇔ A < B, SF = OF ⇔ A ≥ B.
| A | B | A − B (8 bit) | SF | OF | Result |
|---|---|---|---|---|---|
| 5 | 7 | FEh (−2) | 1 | 0 | SF ≠ OF → 5 < 7 |
| 127 (7Fh) | −128 (80h) | FFh (−1) ✗ true: +255 | 1 | 1 | SF = OF → 127 > −128 |
| −128 (80h) | 1 | 7Fh (+127) ✗ true: −129 | 0 | 1 | SF ≠ OF → −128 < 1 |
| −5 (FBh) | 3 | F8h (−8) | 1 | 0 | SF ≠ OF → −5 < 3 |
In the overflow rows, SF alone would give the wrong answer; combined with OF the decision is correct.
Comparison
Jumps for Unsigned Numbers
| Instruction | Meaning | Condition | Opposite |
|---|---|---|---|
| JE, JZ | Jump if Equal (=) · Jump if Zero | ZF = 1 | JNE, JNZ |
| JNE, JNZ | Jump if Not Equal (<>) · Jump if Not Zero | ZF = 0 | JE, JZ |
| JA, JNBE | Jump if Above (>) · Not Below or Equal | CF = 0 and ZF = 0 | JNA, JBE |
| JB, JNAE, JC | Jump if Below (<) · Not Above or Equal · Carry | CF = 1 | JNB, JAE, JNC |
| JAE, JNB, JNC | Jump if Above or Equal (>=) · Not Below · Not Carry | CF = 0 | JNAE, JB, JC |
| JBE, JNA | Jump if Below or Equal (<=) · Not Above | CF = 1 or ZF = 1 | JNBE, JA |
Comparison
Above/Below or Greater/Less?
| Comparison | Unsigned (A/B) | Signed (G/L) |
|---|---|---|
| A = B | JE / JZ | JE / JZ |
| A ≠ B | JNE / JNZ | JNE / JNZ |
| A > B | JA / JNBE | JG / JNLE |
| A ≥ B | JAE / JNB | JGE / JNL |
| A < B | JB / JNAE | JL / JNGE |
| A ≤ B | JBE / JNA | JLE / JNG |
JA jumps (251 > 3) but JG does not (−5 < 3). Choose the family that matches the data.Comparison · Step through the code
Code Trace: Same Bits, JA and JG
AL = FBh: unsigned 251, signed −5. BL = 3. CL, CH, DL will mark which jump was not taken.
CMP AL, BL: FBh − 03h = F8h. No borrow → CF = 0; negative result → SF = 1; no overflow → OF = 0.
JA: CF = 0 and ZF = 0 → jumps (251 > 3). The CL = 1 line is skipped.
JG: ZF = 0 but SF ≠ OF → no jump (−5 < 3).
CH = 1: in the signed comparison AL is not greater. One CMP, two different decisions.
AL = 7Fh = 127. Now compare 127 with 80h (−128).
7Fh − 80h = FFh: signed 127 − (−128) = 255 does not fit → OF = 1, the result's sign bit SF = 1. Borrow → CF = 1.
JG: ZF = 0 and SF = OF (1 = 1) → jumps: 127 > −128 is true. SF alone would have said “less”.
DL stays 0. Result: CH = 1 (−5 > 3 is false), DL = 0 (127 > −128).
MOV AL, 0FBh ; 251 or -5 MOV BL, 03h CMP AL, BL ; FBh - 03h JA a1 ; unsigned: 251 > 3 ? MOV CL, 1 ; if JA failed a1: JG g1 ; signed: -5 > 3 ? MOV CH, 1 ; JG not taken g1: MOV AL, 7Fh ; 127 CMP AL, 80h ; 127 - (-128) JG g2 ; SF = OF ? MOV DL, 1 ; if JG failed g2: RET
Comparison · Step through the code
Code Trace: CMP + JE
AX = 0005h.
BX = 0005h.
cmp ax, bx: 5 − 5 = 0 → ZF = 1. AX is unchanged.
ZF = 1, so je jumps.
equal_message: the work for the equal case goes here.
jmp end_program: skips the not_equal_message block. Without this JMP both blocks would run.
End of program. If they were not equal, je would not jump and the jmp on line 5 would go to line 8.
mov ax, 5 mov bx, 5 cmp ax, bx je equal_message jmp not_equal_message equal_message: ; if equal jmp end_program not_equal_message: ; if not equal end_program:
Comparison · Step through the code
Code Trace: CMP + JA
AX = 0005h.
BX = 0007h.
cmp ax, bx: 5 − 7 = FFFEh (−2). Borrow → CF = 1, ZF = 0.
ja needs CF = 0 and ZF = 0. CF = 1 → no jump.
jmp not_jump_above goes to the "less or equal" block.
not_jump_above: the work for AX ≤ BX.
End of program. Using jg instead of ja would not jump either (SF ≠ OF); with positive values both families agree.
mov ax, 5 mov bx, 7 cmp ax, bx ja jump_above jmp not_jump_above jump_above: ; if ax is greater jmp end_program not_jump_above: ; if ax is less/equal end_program:
Comparison · Step through the code
Code Trace: Compare and Print
The character is printed with a DOS service: INT 21h with AH = 02h prints the character in DL.
AL = 25 = 19h.
BL = 10 = 0Ah.
cmp al, bl: 25 − 10 = 15 (0Fh) → ZF = 0.
ZF = 0 → je does not jump.
mov dl, 'n': the character to print is in DL (6Eh).
jmp prt: skips the 'y' line; both branches meet at the prt label.
AH = 02h: DOS character output service.
int 21h: prints n. DOS also leaves the printed character in AL: AL = 6Eh.
A program must end with ret; otherwise the CPU would execute the random bytes that follow as instructions.
org 100h mov al, 25 ; al = 25 mov bl, 10 ; bl = 10 cmp al, bl ; compare al - bl je equal ; jump if al = bl (zf = 1) mov dl, 'n' ; al <> bl: print 'n' jmp prt ; go to the print line equal: mov dl, 'y' ; al = bl: print 'y' prt: mov ah, 2 ; DOS: print char in DL int 21h ret ; return to DOS
Comparison
The if / else Pattern
High Level
if (ax == bx) {
; block A
} else {
; block B
}
while (cx > 0) { ; unsigned
; body
}
Assembly
cmp ax, bx
jne not_eq ; the OPPOSITE test
; block A
jmp after
not_eq:
; block B
after:
top: cmp cx, 0
jbe done ; cx <= 0 → exit
; body
jmp top
done:
jmp after at the end of block A, and block B runs too.Comparison · Step through the code
Code Trace: if / else and while
if (AX == BX) DX = 1; else DX = 2; then, with CX = 2, while (CX > 0) DX += CX. AX = 12, BX = 30.
CMP AX, BX: 12 − 30 → ZF = 0 (not equal), borrow → CF = 1.
JNE not_eq: the opposite condition holds → block A is skipped.
Block B: DX = 2. The next line is after; the JMP after in block A was not needed on this path.
CX = 2. CMP CX, 0: 2 > 0 → CF = 0, ZF = 0; JBE does not jump, the body is entered.
Body: DX = 2 + 2 = 4; CX = 1.
JMP top: the condition is tested again; 1 > 0 → JBE does not jump.
DX = 4 + 1 = 5; CX = 0 → DEC sets ZF = 1.
Back at the top: CX = 0 → ZF = 1, JBE done jumps. Because the condition is tested first, CX = 0 would never enter the loop.
Result DX = 5: block B (2) + 2 + 1.
MOV AX, 12 MOV BX, 30 CMP AX, BX JNE not_eq ; OPPOSITE test MOV DX, 1 ; block A: AX = BX JMP after not_eq: MOV DX, 2 ; block B: AX ≠ BX after: MOV CX, 2 ; while (CX > 0) top: CMP CX, 0 JBE done ; CX ≤ 0 → exit ADD DX, CX ; body DEC CX JMP top done: RET
Comparison
Out-of-Range Conditional Jump
- On the 8086 a conditional jump reaches only −128…+127 bytes.
- If the target is farther, the assembler reports an error. Fix: invert the condition, short-jump over the next instruction, and reach the far target with a JMP (near, ±32 KB).
- emu8086 does this rewrite itself, turning an out-of-range Jcc into an inverted short jump plus a JMP; assemblers such as MASM report an error.
Wrong (target too far)
cmp al, 0 je far_away ; out of range! ; ... 200 bytes of code ... far_away:
Right
cmp al, 0 jne cont ; opposite test, short jmp far_away ; near JMP (E9) cont: ; ... 200 bytes of code ... far_away:
Loops
Loop Instructions
| Instruction | Operation and jump condition |
|---|---|
| LOOP | Decrement CX; jump if CX ≠ 0. |
| LOOPE | Decrement CX; jump if CX ≠ 0 and equal (ZF = 1). |
| LOOPZ | Same as LOOPE: jump if CX ≠ 0 and ZF = 1. |
| LOOPNE | Decrement CX; jump if CX ≠ 0 and not equal (ZF = 0). |
| LOOPNZ | Same as LOOPNE: jump if CX ≠ 0 and ZF = 0. |
| JCXZ | Jump if CX = 0 (CX is not decremented). |
Loops
How Does LOOP Work?
- Loops repeat a block of code depending on a condition.
LOOP target: CX is decremented first; if it is not zero, jump to the label. Same effect asDEC CX+JNZ, but the flags are preserved.LOOPE / LOOPZjumps while CX ≠ 0 and ZF = 1;LOOPNE / LOOPNZwhile CX ≠ 0 and ZF = 0.JCXZ: jumps to the label when CX is zero.- With CX = 0 on entry, the first decrement makes CX FFFFh → the body runs 65,536 times.
Loops · Step through the code
Code Trace: LOOP
CX = 0005h: the body will run 5 times.
Pass 1: the loop body runs.
loop: CX = 0004h ≠ 0 → jump back to again.
Pass 2: the loop body runs.
loop: CX = 0003h ≠ 0 → jump back to again.
Pass 3: the loop body runs.
loop: CX = 0002h ≠ 0 → jump back to again.
Pass 4: the loop body runs.
loop: CX = 0001h ≠ 0 → jump back to again.
Pass 5: the loop body runs.
loop: CX = 0000h → no jump, the loop exits.
jmp quit: quit is already the next line, so this jump is unnecessary but harmless.
ret: the body ran 5 times in total.
org 100h mov cx, 5 ; CX = repeat count again: ; loop body loop again ; if CX ≠ 0 go to again jmp quit ; loop done: go to quit quit: ret
Loops · Step through the code
Code Trace: Nested LOOPs
BX counts the total steps: 0. In a .COM program SP = FFFEh.
The outer loop will run 5 times: CX = 0005h.
Outer pass 1: BX = 1.
AL = '1' (31h), AH = 0Eh → INT 10h teletype prints 1. (Function 0Eh uses BH as the page number; BX < 256, so BH = 0.)
push cx: the inner loop also uses CX, so the outer counter (5) is saved on the stack; SP = FFFCh.
Inner loop counter: CX = 5.
Inner pass 1: BX = 2, prints 2.
loop k2: CX = 4 → back to k2.
4 more inner passes: 5 2s printed in total, BX = 6, CX = 0 → the inner loop ends.
pop cx: the outer counter is back: CX = 5, SP = FFFEh.
loop k1: CX = 4 → back to k1 (outer pass 2).
Outer pass 2 done: BX = 12, CX = 3.
Outer pass 3 done: BX = 18, CX = 2.
Outer pass 4 done: BX = 24, CX = 1.
Outer pass 5 done: CX = 0 → the outer loop exits. BX = 30 = 001Eh (5 + 5×5 steps).
ret. The screen shows "122222" 5 times. Because push/pop are balanced, ret returns to the right address (0000h); SP = 0000h.
org 100h mov bx, 0 ; total steps mov cx, 5 ; outer counter k1: add bx, 1 mov al, '1' mov ah, 0eh int 10h ; print '1' push cx ; save outer mov cx, 5 ; inner counter k2: add bx, 1 mov al, '2' mov ah, 0eh int 10h ; print '2' loop k2 ; inner loop pop cx ; restore outer loop k1 ; outer loop ret
Loops · Step through the code
Code Trace: Searching an Array with LOOPNE
At most 5 elements will be examined.
SI = −1 (FFFFh); the first inc in the loop makes it 0.
SI = 0.
arr[0] = 7; 7 − 3 → ZF = 0.
loopne: CX = 4 ≠ 0 and ZF = 0 → keep searching.
SI = 1.
arr[1] = 1; 1 − 3 → ZF = 0.
loopne: CX = 3 ≠ 0 and ZF = 0 → keep searching.
SI = 2.
arr[2] = 3; 3 − 3 → ZF = 1.
loopne: CX = 2 but ZF = 1 → the loop exits. LOOPNE does not change the flags, so ZF = 1 survives.
ZF = 1 → jne notfound does not jump: the value was found.
AL = 1 (found), SI = 2 gives the position. The loop finished in 3 passes instead of 5.
org 100h mov cx, 5 ; element count mov si, -1 ; first inc makes it 0 scan: inc si ; next element cmp arr[si], 3 ; is it 3? loopne scan ; go on if CX≠0, ZF=0 jne notfound ; ZF=0 → not found mov al, 1 ; found, position in SI ret notfound: mov al, 0 ; not found ret arr db 7, 1, 3, 9, 4
Loops
JCXZ and LOOP / DEC–JNZ
Guarding against a zero-pass loop
mov cx, n ; n known at run time jcxz skip ; skip if CX = 0 again: ; body loop again skip:
Without JCXZ, LOOP with CX = 0 first makes CX FFFFh and the body runs 65,536 times.
LOOP or DEC + JNZ?
| LOOP | DEC CX + JNZ | |
|---|---|---|
| Size | 2 bytes | 1 + 2 = 3 bytes |
| Flags | unchanged | ZF, SF, OF, PF, AF change (CF kept) |
| Counter | CX only | any register |
| Extra condition | LOOPE / LOOPNE | any Jcc can be chosen |
Loops · Step through the code
Code Trace: JCXZ Guard and DEC–JNZ
The pass count n = 0 is in CX. An unguarded LOOP would first make CX FFFFh and run the body 65,536 times.
JCXZ skip: CX = 0 → the loop is never entered. AX stays 0.
Second loop: counter BX = 3 (DEC–JNZ can use any register).
STC: CF = 1. Think of it as information that must survive the loop.
Pass 1: AX = 1. DEC BX → 2, ZF = 0 → JNZ jumps back. CF is still 1.
Pass 2: AX = 2, BX = 1.
Pass 3: AX = 3. DEC BX → 0, ZF = 1. INC/DEC did not touch CF: CF = 1.
JNZ: ZF = 1 → no jump, the loop ends.
Result AX = 3, CF = 1 preserved. LOOP does not change flags either, but works only with CX.
MOV CX, 0 ; n = 0 MOV AX, 0 JCXZ skip ; CX = 0 → skip again: INC AX LOOP again skip: MOV BX, 3 ; n = 3, counter BX STC ; CF = 1 again2: INC AX DEC BX ; CF is kept JNZ again2 RET
Example Programs · Step through the code
Code Trace: Sum of Array Elements
CX = 5 elements, sum AL = 0, index BX = 0.
add al, vector[0]: AL = 0 + 5 = 5 (05h).
BX = 1, CX = 4 → back to next.
add al, vector[1]: AL = 5 + 4 = 9 (09h).
BX = 2, CX = 3 → back to next.
add al, vector[2]: AL = 9 + 5 = 14 (0Eh).
BX = 3, CX = 2 → back to next.
add al, vector[3]: AL = 14 + 2 = 16 (10h).
BX = 4, CX = 1 → back to next.
add al, vector[4]: AL = 16 + 1 = 17 (11h).
BX = 5, CX = 0 → the loop ends.
mov m, al: m = 11h = 17 = 5 + 4 + 5 + 2 + 1.
ret. The data is defined after ret, so it is never executed as code.
org 100h mov cx, 5 ; element count mov al, 0 ; sum (AL) = 0 mov bx, 0 ; BX = index next: add al, vector[bx] ; add element inc bx ; next element loop next ; until CX = 0 mov m, al ; store in m ret vector db 5, 4, 5, 2, 1 m db 0
Example Programs · Step through the code
Code Trace: Printing Hello on the Screen
jmp start: the data (msg) sits between instructions, so it is jumped over; otherwise bytes such as 'H' (48h) would be taken as instructions.
SI = 0: first character.
AL = msg[0] = 'H' = 48h.
48h − 0 ≠ 0 → ZF = 0, je does not jump.
AH = 0Eh, INT 10h → prints H.
SI = 1, jmp next_char goes back to the top.
Next pass: AL = 'e' (65h), ZF = 0 → prints e, SI = 2.
Next pass: AL = 'l' (6Ch), ZF = 0 → prints l, SI = 3.
Next pass: AL = 'l' (6Ch), ZF = 0 → prints l, SI = 4.
Next pass: AL = 'o' (6Fh), ZF = 0 → prints o, SI = 5.
AL = msg[5] = 0: end of the string (null terminator).
0 − 0 = 0 → ZF = 1, je stop jumps.
ret: the screen shows Hello.
org 100h jmp start msg db 'Hello', 0 start: mov si, 0 ; source index = 0 next_char: mov al, msg[si] ; get the char cmp al, 0 ; compare with 0 je stop ; stop if 0 mov ah, 0eh ; teletype function int 10h ; print char in AL inc si ; next character jmp next_char ; loop back stop: ret
Example Programs
Palindrome Check: The Idea
- Palindrome: a string that reads the same forward and backward (
123321,kayak). - DI points to the start of the string and SI to its end; the characters are compared, DI moves forward and SI backward.
- Comparing half the length is enough (
shr cx, 1→ CX / 2). With an odd length the middle character is not compared. str_size = $ - msg:$is the current address;msgandstrare at the same address, so the result is the constant 6, the string length (it takes no memory).- INT 21h / AH = 09h prints the string at DS:DX up to the
'$'character; 0Dh, 0Ah are carriage return and line feed.
Addresses assume org 100h and a 2-byte short jmp start (0100h): str starts at 0102h.
Example Programs · Step through the code
Code Trace: Palindrome (1/2) Setup
jmp start: jumps over the data section.
AH = 09h: DOS string output function.
DX = offset str = 0102h.
INT 21h: prints 123321 and a line break ('$' is not printed).
lea di, str: DI = 0102h (first character).
SI = DI = 0102h.
SI = 0102h + 6 = 0108h: one past the string (0Dh).
dec si: SI = 0107h, the last character '1'.
CX = str_size = 6.
6 − 1 ≠ 0 → ZF = 0, je does not jump.
shr cx, 1: 0110b → 0011b, CX = 3. The bit shifted out is 0 → CF = 0.
org 100h jmp start msg: str db '123321' str_size = $ - msg ; = 6 db 0Dh,0Ah,'$' start: mov ah, 9 ; print up to '$' mov dx, offset str int 21h lea di, str ; DI = address of str mov si, di add si, str_size ; SI = last + 1 dec si ; SI = last char mov cx, str_size cmp cx, 1 ; one character? je is_palindrome ; one char is a palindrome shr cx, 1 ; CX = length / 2
Example Programs · Step through the code
Code Trace: Palindrome (2/2) Comparison
Loop start: DI = 0102h, SI = 0107h, CX = 3.
AL = [DI] = '1', BL = [SI] = '1'.
Equal → ZF = 1, jne does not jump.
DI = 0103h, SI = 0106h (the INC/DEC result is nonzero → ZF = 0), CX = 2 → next_char.
AL = [DI] = '2', BL = [SI] = '2'.
Equal → ZF = 1, jne does not jump.
DI = 0104h, SI = 0105h, CX = 1 → next_char.
AL = [DI] = '3', BL = [SI] = '3'.
Equal → ZF = 1, jne does not jump.
CX = 0 → the loop ends: every pair matched.
AH = 09h, DX = offset msg1 = 0142h.
INT 21h: prints palindrome! (DOS returns from this function with AL = 24h, i.e. '$'.)
jmp stop: skips the "not palindrome!" block.
ret. The data comes right after the stop: label, so without ret the CPU would execute the bytes of "palindrome!$" as instructions.
next_char: mov al, [di] ; char at the start mov bl, [si] ; char at the end cmp al, bl jne not_palindrome ; differ: not one inc di ; start: one forward dec si ; end: one back loop next_char is_palindrome: mov ah, 9 ; print "palindrome!" mov dx, offset msg1 int 21h jmp stop not_palindrome: mov ah, 9 ; "not palindrome!" mov dx, offset msg2 int 21h stop: ret msg1 db "palindrome!$" msg2 db "not palindrome!$"
Example Programs
Palindrome: Different Inputs
| str | Length | CX (after shr) | Comparisons | Output |
|---|---|---|---|---|
'123321' | 6 | 3 | 1=1, 2=2, 3=3 | palindrome! |
'123421' | 6 | 3 | 1=1, 2=2, 3≠4 → jne | not palindrome! |
'12321' | 5 | 2 | 1=1, 2=2 (the middle 3 is skipped) | palindrome! |
'7' | 1 | — | cmp cx,1 → je is_palindrome | palindrome! |
'ab' | 2 | 1 | a≠b → jne | not palindrome! |
cmp cx, 1? With length 1, shr cx, 1 makes CX 0; entering a LOOP-based loop with CX = 0 would run the comparison 65,536 times. Length 0 has the same danger: guard it with jcxz.Lab · 8086 instructions step by step
Lab: Jumps and Loops
Summary
Summary
Summary
Exercises
- The
JNEat address 0120h must jump to address 0100h. What is its machine code? - After
mov al, 80h/cmp al, 1, doJAandJGjump? - How many times does
mov cx, 0/d: loop drun? How do you prevent it? - Write code that sets BX = 1 if the unsigned number in AX is greater than 100, and BX = 0 otherwise.
- In the array sum example, what would AL and CF be if the elements were 200 and 100? Suggest a fix.
- What happens in the Hello example if the
jmp startline is deleted?
Hints / answers
- Next IP = 0122h; 0100h − 0122h = −22h = DEh →
75 DE. - 80h − 01h = 7Fh: CF = 0, ZF = 0 → JA jumps (128 > 1). SF = 0, OF = 1 → SF ≠ OF → JG does not jump (−128 < 1).
- 65,536 times; put a
jcxzbefore it. mov bx, 0/cmp ax, 100/jbe done/mov bx, 1/done:- 200 + 100 = 300 = 12Ch → AL = 2Ch, CF = 1 (carry). Keep the sum in AX and use
adc ah, 0, or add in 16 bits. - The bytes 'H','e','l','l','o',0 (48h 65h 6Ch …) are executed as instructions; the program behaves unpredictably.
Summary
References
- emu8086 documentation: 8086 Assembler Tutorial for Beginners (Part 7: Program Flow Control).
- Intel, The 8086 Family User's Manual, 1979: instruction set and encoding tables.
- Barry B. Brey, The Intel Microprocessors, 8th ed., Pearson, 2009: Chapter 6 "Program Control Instructions".
- Kip R. Irvine, Assembly Language for x86 Processors, 7th ed., Pearson, 2014: conditional processing and loops.