dec and jnz in Assembly
The tightest countdown: decrement until the zero flag.
; dec + jnz is the tightest countdown: the zero flag ends it.
global _start
section .data
tick: db "tick", 10
TLEN: equ $ - tick
section .text
_start:
mov r12, 3 ; r12 survives syscalls
.again:
mov rax, 1
mov rdi, 1
mov rsi, tick
mov rdx, TLEN
syscall
dec r12
jnz .again
mov rax, 60
xor rdi, rdi
syscall
How it works
r12survives syscalls — the callee-saved registers matter.decsets the zero flag;jnzloops on it.- Three ticks print, then a clean exit.
Keywords and builtins used here
dbdecequglobaljnzmovr12raxrdirdxrsisectionsyscallxor
The run, in numbers
- Lines
- 23
- Characters to type
- 324
- Tokens
- 54
- Three-star pace
- 55 tpm
At the three-star pace of 55 tokens a minute, this run takes about 59 seconds.
Step 3 of 4 in Control flow, step 13 of 31 in Language basics.