Control flow
Comparisons, condition flags, branches, loops, and readable labels.
The CPU does not have high-level if or while statements. It sets flags while doing arithmetic or comparisons, then a conditional jump chooses the next label.
Test without changing the value
test performs an AND only for its flags; it throws the numeric result away. Testing bit zero is a quick way to distinguish odd and even integers.
mov rcx, 0FFFFh
test rcx, 00001h ; jump if odd
Keep in mind: Use test when you care about zero or a bit mask but want to keep the original operand.
Compare, then choose the right jump
cmp left, right sets flags as if it had calculated left - right. Use ja/jb for unsigned values and jg/jl for signed values. Equality uses je or jne either way.
mov rbx, 100
mov rcx, 200
cmp rcx, rbx
ja above ; jump if lop above rop
mov rdx, 1
above:
mov rcx, 50
cmp rcx, rbx
jb below ; jump if lop below rop
mov rdx, 2
below:
mov rcx, 100
cmp rcx, rbx
jbe equal ; jump if lop below or equal rop
mov rdx, 3
equal:
Keep in mind: Signedness changes how ordering flags are interpreted; it does not change the bits themselves.
A loop is a label plus a branch
There is no hidden loop machinery here. Initialize state, place a label, update the state, compare it with the limit, and jump back while more work remains.
xor rcx, rcx
xor rax, rax
inc rax
acc_entry:
add rax, rax
inc rcx
cmp rcx, 10
je acc_end
jmp acc_entry
acc_end:
Keep in mind: Keep the loop counter, exit condition, and back edge visually close.
Keep these rules
- cmp and test set flags; conditional jumps consume them.
- Choose signed or unsigned ordering jumps deliberately.
- Readable labels are the structure of assembly control flow.