Course: Computer Architectures
Source: R. Ferrero, Politecnico di Torino, 7_branches(1).pdf, 28 pages. The deck has no lecture number or date, so the filename and PDF page order are preserved.
Why branches matter
A processor normally fetches the next instruction sequentially. A branch changes the program counter (PC), allowing the program to choose another instruction. This is needed for decisions, loops, function calls and program termination.
ARMv7-M uses Thumb/Thumb-2 instructions and makes conditional decisions through the flags in APSR. This differs from the RISC-V branch instructions in Branches, which compare registers directly.
Learning route
- Start with ARM Branch Fundamentals for jumps, calls, PC changes and address formation.
- Continue with ARM Conditional Branches and Flags for
CMP, signed/unsigned conditions andCBZ/CBNZ. - Finish with ARM Loops and Conditional Execution for loop translations, branch penalties and
ITblocks.
Source coverage by PDF page
| Pages | Material |
|---|---|
| 2-3 | Unconditional branches, calls and infinite loops |
| 4-8 | LDR/ADR, BX, Thumb-state bit, PC changes and branch range |
| 9-13 | Flags, conditional branches and comparison exercises |
| 14-15 | CBZ and CBNZ |
| 16-23 | while, for, optimized counting and do-while loops |
| 24 | Pipeline branch penalty |
| 25-28 | IT, absolute-value example and IT syntax |
Key source examples
Function call
BL calculate ; saves the return address in LR and jumps
...
calculate:
ADD r0, r0, r1
BX lr ; returns to the saved addressSigned and unsigned comparison
CMP r0, r1
BLO unsigned_lower
BLT signed_lowerBLO interprets the comparison as unsigned; BLT interprets it as signed. With 0xFF and 1 treated as 8-bit values, unsigned comparison sees 255 < 1 as false, while signed comparison sees -1 < 1 as true. In real ARM registers, make sure the value has been sign-extended if you intend the signed interpretation.
Absolute value without a branch
CMP r0, r1
ITE GE
SUBGE r2, r0, r1
SUBLT r2, r1, r0The two alternatives execute conditionally while the PC continues sequentially, avoiding the taken-branch penalty for this short choice.
Important qualifications
SUBnormally does not update flags;SUBSdoes. A conditional branch must use flags produced by the intended comparison.CBZandCBNZdo not update flags, only user0-r7, and support short forward branches.- A normal
Bhas a limited relative range.B.Wextends the range;BXcan use a register-held 32-bit destination. ITblocks are limited: the first instruction is the true case, up to four instructions are covered, false conditions are inverse conditions, and a branch may appear only as the last instruction.- The pipeline diagrams illustrate why a taken branch can discard already fetched instructions; the exact penalty depends on the processor implementation.
Review route
Try the retrieval questions in each concept note without looking at the answers. Then compare ARM’s flag-based conditions with Branches and connect the pipeline consequence to control hazards.