typestar

test and setcc in Assembly

Flag questions become byte answers.

; test ands without storing; setcc turns a flag into a byte.
        global _start

        section .text
_start:
        mov rax, 0b10100        ; 20

        test rax, 1             ; lowest bit set?
        setz bl                 ; bl = 1 if even
        movzx rbx, bl

        test rax, rax           ; the idiomatic "is it zero?"
        setnz cl                ; cl = 1 if nonzero
        movzx rcx, cl

        lea rdi, [rbx + rcx + 40]  ; 1 + 1 + 40
        mov rax, 60             ; exit code 42
        syscall

How it works

  1. test rax, 1 asks about the low bit without storing.
  2. setz and setnz write 0 or 1 into a byte register.
  3. test rax, rax is the idiomatic zero check.

Keywords and builtins used here

The run, in numbers

Lines
18
Characters to type
425
Tokens
53
Three-star pace
55 tpm

At the three-star pace of 55 tokens a minute, this run takes about 58 seconds.

Type this snippet

Step 3 of 4 in Bits & flags, step 17 of 31 in Language basics.

← Previous Next →