LOGBOOK

HELP

Quiz Entry - updated: 2026.07.14

How do you recognize a for(i = 0; i < n; i++) loop with its counter in assembly?

Look for a counter that's zeroed before the loop, incremented by 1 each pass, and compared against a limit at the bottom with a backward conditional jump.

for loop: init i=0, body, i++, cmp i<n with backward jl to the body

* The four for-loop parts in assembly: zero the counter, body, add $1, then a bottom cmp + backward conditional jump. *

    # i = 0
    xor  %ecx, %ecx
.L_loop:
    # Body using %ecx as index
    movl (%rdi,%rcx,4), %eax
    ...
    # i++
    add  $1, %ecx
    # i < n
    cmp  %esi, %ecx
    jl   .L_loop

Identifying the parts:

C code Assembly clue
i = 0 xor %ecx, %ecx or mov $0, %ecx before the loop
i < n cmp %esi, %ecx + jl (signed) or jb (unsigned) at the end
i++ add $1, %ecx (or inc %ecx) near the end
a[i] (%rdi,%rcx,4) — index register used with scale

Counting down (for(i = n-1; i >= 0; i--)):

    mov  %esi, %ecx
.L_loop:
    sub  $1, %ecx
    ...
    test %ecx, %ecx
    jns  .L_loop

jns = "jump if not sign" = i >= 0.

Go deeper:

From Quiz: REVE1 / Assembly Patterns & GDB | Updated: Jul 14, 2026