Conditional branches¶
Problem¶
Run different code paths based on a value.
Code¶
stl.startup_and_init_all
bit.if 8, x, x_is_zero, x_is_nonzero
x_is_zero:
stl.output "x is zero\n"
;done
x_is_nonzero:
stl.output "x is nonzero\n"
;done
done:
stl.loop
x: bit.vec 8, 42
Walkthrough¶
bit.if 8, x, l0, l1— check the 8-bit valuex: jump tol0if all bits are0, otherwise jump tol1. Three-way variants exist (bit.cmp lt, eq, gt); a one-armed variantbit.if0jumps only when zero and falls through otherwise.The labels
x_is_zero,x_is_nonzero,doneare local to the file; the;donebare-jump (no flip) is the FlipJump idiom for “fall through”.
Variations¶
Three-way comparison (<, ==, >):
bit.cmp 8, a, b, lt, eq, gt
lt: stl.output "a<b\n"; ;done
eq: stl.output "a==b\n"; ;done
gt: stl.output "a>b\n"; ;done
done: stl.loop
Hex-level branches when you’re already using hex.*:
hex.if 8, x, x_is_zero, x_is_nonzero