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, unsigned

Example:

beq x1, x2, target

means:

if x1 == x2:
    jump to target
else:
    continue normally

Taken vs not taken

For:

beq x1, x2, target

If:

x1 == x2

the branch is taken:

PC = target

If:

x1 != x2

the branch is not taken:

PC = PC + 4

Signed vs unsigned

The same 32 bits can be interpreted differently.

Example:

0xFFFFFFFF

as signed RV32:

-1

as unsigned RV32:

4294967295

So:

blt x1, x2, label

uses signed comparison, while:

bltu x1, x2, label

uses unsigned comparison.

Branches in loops

Example:

loop:
    ...
    bne x1, x0, loop

The branch may be taken repeatedly and then become not taken when the loop ends.