typestar

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

  1. r12 survives syscalls — the callee-saved registers matter.
  2. dec sets the zero flag; jnz loops on it.
  3. Three ticks print, then a clean exit.

Keywords and builtins used here

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.

Type this snippet

Step 3 of 4 in Control flow, step 13 of 31 in Language basics.

← Previous Next →