Lecture 03 - Branches and Control Flow
Course: Computer Architectures
Topics
Branches
A branch changes control flow only if its condition is satisfied.
Example:
beq x1, x2, targetIf:
x1 == x2the branch is taken and execution goes to target.
If:
x1 != x2the branch is not taken and execution continues with the next sequential instruction.
Jumps
A jump changes the PC unconditionally.
jal
jal ra, labeljumps and saves a return address.
j
j labeljumps without keeping a useful return address.
jalr
jalr rd, offset(rs1)uses a register-derived destination.
ret
retreturns through ra.
Loop pattern
A loop may use a conditional branch such as:
loop:
...
addi x1, x1, -1
bne x1, x0, loopThe branch is taken while x1 != 0, and not taken when x1 == 0.
Why branches matter to pipelines
A branch can change the PC, so the processor may not know which instruction should be fetched next until the branch condition is resolved.
This leads directly to Pipelining > 11. Control hazards.
Questions to review
- What does “taken” actually mean?
- What is the difference between branch and jump?
- Why does
jalsave a return address? - Why do branches create a pipeline problem?