Quiz Entry - updated: 2026.07.14
How do you recognize a loop in assembly?
Look for a backward conditional jump: a label, a block of body instructions, then a cmp/test + conditional jump that targets that earlier label.
* The tell-tale sign is a backward conditional jump. do-while jumps back from the bottom; while adds a guard jump that skips the body; for adds an init before the loop. *
.L_loop:
# body instructions
addl $1, %ecx
cmpl %edi, %ecx
jl .L_loop
Types of loops by structure:
| Loop type | Pattern |
|---|---|
| do-while | Body first, conditional jump back |
| while | Initial conditional jump to skip, then body + jump back |
| for | Init before loop, body + update + test + jump back |
while loop has a guard jump before the loop:
cmp ...
je .L_done
.L_loop:
# body
cmp ...
jne .L_loop
.L_done:
Identifying the loop variable: Look for a register that gets incremented (add $1) or decremented (sub $1) near the conditional jump. The cmp before the jump is the loop condition.
Go deeper:
Control flow — loops (Wikipedia) — Defines while, do-while, and for loops, matching the three structural patterns in the answer.
Jcc conditional jumps (felixcloutier) — Reference for the conditional jump that forms the loop back-edge.