bltzpseudo

bltz

pseudo
RISC-V Assembly Reference

lyszt.net covers bltz and other RISC-V instructions in more detail.

Description

bltz branches when a register holds a negative number. The name means branch if less than zero. The form below jumps to the labeled position if rs is below zero, and continues to the next instruction otherwise.

bltz rs, label

The assembler rewrites this pseudo-instruction into a real instruction. It becomes the instruction below, which compares the register with zero, the register that always holds 0.

blt rs, zero, label

The comparison is signed, so the 32 bits in the register are read as a number that can be negative. A branch compares two registers and cannot take a constant written in the instruction, which is why the 0 comes from the zero register. See blt for branches that compare two values and beq for how labels work.

To check whether a value is less than zero, put the value in a register and use bltz on it. The usual cases are testing the result of a subtraction to see whether it went below zero, and testing a function result that reports an error as a negative number.

sub t0, t1, t2
bltz t0, smaller

The lines above jump to smaller when the subtraction result is below zero. A result of exactly 0 is not negative and continues to the next instruction. The opposite branch, which also takes zero, is bgez.

Expands to

blt rs, zero, label

The assembler replaces this pseudo-instruction with the real instruction or instructions listed above.

Example

li t0, -3
bltz t0, done   # t0 < 0, so jump to 'done'