The loop shape in Assembly
A label, a compare, a conditional jump back: every loop ever.
; A loop is a label, a compare, and a conditional jump back.
global _start
section .text
_start:
xor rax, rax ; sum
mov rcx, 1 ; counter
.top:
add rax, rcx
inc rcx
cmp rcx, 10
jle .top ; while rcx <= 10
mov rdi, rax ; 1+2+...+10 = 55
mov rax, 60 ; exit code 55
syscall
How it works
.top:is a local label under_start.cmpsets flags;jlejumps while rcx <= 10.- The Gauss sum 1..10 = 55 leaves as the exit code.
Keywords and builtins used here
addcmpglobalincjlemovraxrcxrdisectionsyscallxor
The run, in numbers
- Lines
- 16
- Characters to type
- 332
- Tokens
- 41
- Three-star pace
- 60 tpm
At the three-star pace of 60 tokens a minute, this run takes about 41 seconds.
Step 2 of 4 in Control flow, step 12 of 31 in Language basics.