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
test rax, 1asks about the low bit without storing.setzandsetnzwrite 0 or 1 into a byte register.test rax, raxis the idiomatic zero check.
Keywords and builtins used here
blclgloballeamovmovzxraxrbxrcxrdisectionsetnzsetzsyscalltest
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.
Step 3 of 4 in Bits & flags, step 17 of 31 in Language basics.