blepseudo

ble

pseudo
RISC-V Assembly Reference

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

Description

ble branches when one value is less than or equal to another. The name stands for branch if less than or equal. The form below jumps to the label when rs1 is at most rs2, using a signed comparison.

ble rs1, rs2, label

ble is a pseudo-instruction that relies on swapping operands, as bgt does. Asking whether a is less than or equal to b matches asking whether b is greater than or equal to a, so the assembler rewrites ble as bge with the operands reversed. No dedicated machine instruction is needed. See bge for the underlying branch.

ble suits loops and ranges that include their endpoint. A loop that runs while a counter is at most n, with n included, reads clearly with ble. The same loop with the strict blt needs n plus one. Range checks use it alongside the other branches. Branch away if a value is below the minimum, branch away if it is above the maximum, and what remains has passed.

The RISC-V manual states that bgt, bgtu, ble and bleu can be synthesized by reversing the operands to blt, bltu, bge and bgeu.

Equal values do branch with ble, which makes it the opposite of bgt on the same operands. For any pair, one of them branches. The comparison is signed, so values may be negative. blt, bge, bgt and ble together give all four signed ordering directions.

Expands to

bge rs2, rs1, label

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

Example

li t0, 2
li t1, 5
ble t0, t1, done   # 2 <= 5, so jump to 'done'