Understanding Branches
Branches allow your program to make decisions and execute different code based on conditions. MIPS provides conditional branches that compare registers and jump to a label if the condition is true. The program counter (PC) is updated to the target address, changing the flow of execution.
Key Points:
- Branches change program flow based on conditions
- beq branches if two registers are equal
- bne branches if two registers are not equal
- Labels mark target locations
Example Code
# Branch if equal
li $t0, 5
li $t1, 5
beq $t0, $t1, equal_label # Branch if $t0 == $t1
# Code here runs if not equal
j end_label
equal_label:
# Code here runs if equal
end_label:
# Continue executionTry It Yourself
1
2
3
4
5
6
7
8
9
10
11
12