Lesson 25 of 48 intermediate

Jumps, Loops, and Conditional Branching

Make the 8086 decide, repeat, and branch — turning linear code into smart programs

Open interactive version (quiz + challenge)

Real-world analogy

Jumps are like GPS rerouting. An unconditional jump (JMP) is a forced detour — you always take it. A conditional jump (JZ, JNZ) is like a traffic light: you only go if conditions are right. LOOP is like running laps — the coach (CX) counts down and you keep going until the count hits zero.

What is it?

Jumps and loops give the 8086 the ability to make decisions and repeat actions. JMP is an unconditional transfer of control. Conditional jumps (Jcc) test CPU flags set by CMP, SUB, or TEST to decide whether to branch. LOOP provides a counter-based repeat mechanism using CX. Together, these instructions implement if-else, while, do-while, and for loops in assembly.

Real-world relevance

Every program beyond a trivial calculator needs branching. A keyboard input handler uses conditional jumps to dispatch different key codes. A display driver loops through pixel buffers. An embedded sensor system uses LOOP to average multiple readings. Even boot loaders branch on hardware detection results.

Key points

Code example

; Program: Sum numbers 1 to N where N is in BX
; Result stored in AX
.MODEL SMALL
.STACK 100h
.DATA
  N DW 10          ; sum 1 to 10

.CODE
MAIN PROC
  MOV AX, @DATA
  MOV DS, AX

  MOV CX, [N]      ; CX = N = loop counter
  XOR AX, AX       ; AX = 0 (accumulator)
  JCXZ done        ; guard: skip if N=0

sum_loop:
  ADD AX, CX       ; AX += CX (adds N, N-1, ..., 1)
  LOOP sum_loop    ; CX--, repeat if CX != 0

done:
  ; AX now holds sum = 55 for N=10
  MOV AH, 4Ch
  INT 21h          ; exit to DOS
MAIN ENDP
END MAIN

Line-by-line walkthrough

  1. 1. MOV AX, @DATA / MOV DS, AX — standard setup: point DS to our data segment so we can read variables
  2. 2. MOV CX, [N] — load the value of N (10) into CX, which will serve as both our loop counter and the current number to add
  3. 3. XOR AX, AX — zero out AX (faster than MOV AX, 0). AX is our running sum accumulator
  4. 4. JCXZ done — safety check: if N was 0, skip the entire loop. Without this, LOOP would decrement CX to FFFFh and run 65535 iterations
  5. 5. ADD AX, CX — add the current value of CX to our sum. First iteration adds 10, second adds 9, and so on
  6. 6. LOOP sum_loop — decrement CX by 1. If CX is not zero, jump back to sum_loop. If CX reached 0, fall through
  7. 7. After the loop: AX = 10+9+8+7+6+5+4+3+2+1 = 55
  8. 8. MOV AH, 4Ch / INT 21h — standard DOS exit: terminate the program and return control to DOS

Spot the bug

MOV CX, 5
MOV AX, 0
count_loop:
  ADD AX, 2
  CMP AX, 20
  LOOP count_loop
; Expected: AX = 10 (5 iterations of adding 2)
Need a hint?
Think about what happens if AX reaches 20 before CX reaches 0. Does CMP affect LOOP's behavior?
Show answer
There is no bug in the logic — LOOP only checks CX, ignoring flags from CMP. The CMP is a dead instruction here with no conditional jump following it. AX will correctly be 10. But if the programmer intended to stop early when AX >= 20, they need a conditional jump: replace LOOP with DEC CX / JZ done / CMP AX, 20 / JL count_loop.

Explain like I'm 5

Imagine you are reading a choose-your-own-adventure book. Normally you read page after page (sequential execution). But sometimes the book says 'Jump to page 42' — that is JMP. Other times it says 'If you have the magic sword, go to page 60; otherwise go to page 70' — that is a conditional jump. And sometimes it says 'Re-read this page 5 times' — that is LOOP with CX set to 5.

Fun fact

Conditional jumps on the original 8086 could only reach -128 to +127 bytes. If your branch target was farther away, you had to use the 'reverse-and-jump-over-a-JMP' trick: reverse the condition to skip over an unconditional JMP to the real target. The 386 finally added near conditional jumps.

Hands-on challenge

Write an 8086 assembly program that counts how many elements in a byte array are greater than 50. Store the array at DATA segment with values [30, 75, 12, 88, 45, 99, 50, 51]. Use LOOP and conditional jumps. Store the result count in DL.

More resources

Open interactive version (quiz + challenge) ← Back to course: Microprocessor A–Z