Loops

The LOOP instruction

LOOP decrements RCX and jumps to a label while RCX is not zero. It is one instruction, but modern CPUs handle the explicit DEC and JNZ pair faster, so expect to see both styles:

mov rcx, 10 print_loop: ; body here loop print_loop ; rcx = rcx - 1, jump if not zero

The register pattern

The explicit pattern gives you control over the counter and the exit condition:

mov rcx, 10 print_loop: ; body here dec rcx jnz print_loop

Nested loops

Save the outer counter before entering the inner loop, on the stack or in a spare register, and restore it after. RCX is the classic counter, so nested loops must protect it.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which register does the LOOP instruction use as its counter?

    Answer

    RCX

    LOOP decrements RCX and jumps while it is not zero.

  2. What does LOOP do when RCX is 1?

    Answer

    It decrements RCX to 0 and falls through

    The decrement happens first, and the jump is taken only when the result is not zero.

  3. Which pair is the explicit form of LOOP?

    Answer

    DEC and JNZ

    LOOP is a decrement followed by a jump when the counter is still non-zero.

  4. Why must a nested loop save the outer counter?

    Answer

    The inner loop uses the same RCX

    Both loops count in RCX, so the outer count must go to the stack or a spare register.

  5. What does this program print? default rel section .data digit db "0", 10 section .text global _start _start: mov rcx, 3 xor rbx, rbx count: inc rbx dec rcx jnz count add bl, "0" mov [digit], bl mov rax, 1 mov rdi, 1 mov rsi, digit mov rdx, 2 syscall mov rax, 60 xor rdi, rdi syscall

    Answer

    3

    The loop increments rbx three times before rcx reaches zero, and 3 becomes the digit 3.

  6. What does this program print? default rel section .data digit db "0", 10 section .text global _start _start: mov rcx, 5 xor rbx, rbx count: inc rbx loop count add bl, "0" mov [digit], bl mov rax, 1 mov rdi, 1 mov rsi, digit mov rdx, 2 syscall mov rax, 60 xor rdi, rdi syscall

    Answer

    5

    LOOP runs the body five times, so rbx ends at 5.

  7. What is the exit status of the first loop program on this page?

    Answer

    0

    rdi is cleared before sys_exit, so the status is 0.

  8. Which jump keeps a loop going while a counter is not zero?

    Answer

    JNZ

    JNZ jumps when the zero flag is clear, so it repeats until the decrement reaches zero.

  9. What does dec rcx do to the flags?

    Answer

    It sets ZF when the result is zero

    DEC updates ZF, SF and OF, which is why JNZ can follow it directly.

  10. How many times does a loop body run with RCX = 10 and a LOOP at the end?

    Answer

    10

    The body runs once per count, and the tenth LOOP leaves RCX at zero and falls through.

The snippets on this page are fragments, not whole programs: they show the two loop forms. They were assembled with NASM 3.01 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev