Branches
Course: Computer Architectures
Type: Concept
Definition
A branch is a conditional jump.
It changes the program counter only when its condition is true.
Common branch instructions
beq -> branch if equal
bne -> branch if not equal
blt -> branch if less than, signed
bge -> branch if greater/equal, signed
bltu -> branch if less than, unsigned
bgeu -> branch if greater/equal, unsignedExample:
beq x1, x2, targetmeans:
if x1 == x2:
jump to target
else:
continue normallyTaken vs not taken
For:
beq x1, x2, targetIf:
x1 == x2the branch is taken:
PC = targetIf:
x1 != x2the branch is not taken:
PC = PC + 4Signed vs unsigned
The same 32 bits can be interpreted differently.
Example:
0xFFFFFFFFas signed RV32:
-1as unsigned RV32:
4294967295So:
blt x1, x2, labeluses signed comparison, while:
bltu x1, x2, labeluses unsigned comparison.
Branches in loops
Example:
loop:
...
bne x1, x0, loopThe branch may be taken repeatedly and then become not taken when the loop ends.