bgtzpseudo

bgtz

pseudo
RISC-V Assembly Reference

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

Description

bgtz takes a branch when the value in a register is positive. The name means branch if greater than zero. The form below jumps to the labeled position when rs is above zero, and falls through to the next instruction otherwise.

bgtz rs, label

The assembler treats it as a pseudo-instruction and rewrites it to the instruction below, which asks whether zero is less than the register.

blt zero, rs, label

RISC-V has no branch for greater than, so the operands are swapped and the less-than branch does the work, the same way bgt is built. The comparison is signed. Zero does not branch, which separates bgtz from bgez. See blt for the underlying branch.

bgtz fits a loop that should continue while something is left to process, such as a count of remaining items that is above zero. It also tests whether a result is above zero.

bgtz a0, found

The line above jumps to found when a0 holds 1 or more. A value of 0 or any negative number continues to the next instruction.

Expands to

blt zero, rs, label

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

Example

li t0, 4
bgtz t0, done   # t0 > 0, so jump to 'done'