bne
lyszt.net covers bne and other RISC-V instructions in more detail.
Encoding format
Description
bne means branch if not equal, and it is the counterpart of beq. It compares two registers and jumps to the labeled position when they differ. When they match, execution continues with the next instruction. The form is shown below. Branches, labels and jumping on a condition are explained on the beq page.
bne rs1, rs2, label
Together, beq and bne handle every equal and not equal decision. Assembly often states a condition in the inverted form. To run a block of code only when two values are equal, branch around that block with bne. The code skips the block when the values differ and enters it when they match.
bne also keeps a loop going while a condition holds. A loop that runs until a value reaches a target ends with a bne that jumps back to the top. Text reading follows this pattern, where the loop continues while the current character is not the zero byte that marks the end.
The RISC-V manual says beq and bne take the branch when registers rs1 and rs2 are equal or unequal. The offset is a 12-bit value counted in multiples of 2 bytes, so a branch reaches about 4 KiB in either direction, where a KiB is 1024 bytes.
bnez is the shorthand for comparing with zero, which asks whether a value is not zero. The comparison in bne, as in beq, is plain equality of bits, so signed and unsigned readings do not matter.
Example
li t0, 1 li t1, 2 bne t0, t1, done # t0 != t1, so jump to 'done'
