Lesson 10 of 15

Loops

Loops in Assembly

There is no for or while keyword in assembly. Loops are built from comparisons and jumps.

While Loop Pattern

loop_start:
	cmp rcx, 0
	je loop_end
	; ... loop body ...
	dec rcx
	jmp loop_start
loop_end:

Counting Up

	xor rbx, rbx         ; counter = 0
loop_top:
	cmp rbx, 5
	je loop_done
	; ... do work ...
	inc rbx
	jmp loop_top
loop_done:

Preserving Registers Across Syscalls

The syscall instruction clobbers rcx and r11. If you need a loop counter to survive across a syscall, use a callee-saved register like rbx, r12-r15, or save/restore with push/pop.

Your Task

Write a program that uses a loop to print the digits 1 through 5, each followed by a newline.

Print: "1\n2\n3\n4\n5\n"

x86_64 runtime loading...
Loading...
Click "Run" to execute your code.