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

  1. Start with ARM Branch Fundamentals for jumps, calls, PC changes and address formation.
  2. Continue with ARM Conditional Branches and Flags for CMP, signed/unsigned conditions and CBZ/CBNZ.
  3. Finish with ARM Loops and Conditional Execution for loop translations, branch penalties and IT blocks.

Source coverage by PDF page

PagesMaterial
2-3Unconditional branches, calls and infinite loops
4-8LDR/ADR, BX, Thumb-state bit, PC changes and branch range
9-13Flags, conditional branches and comparison exercises
14-15CBZ and CBNZ
16-23while, for, optimized counting and do-while loops
24Pipeline branch penalty
25-28IT, 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 address

Signed and unsigned comparison

CMP r0, r1
BLO unsigned_lower
BLT signed_lower

BLO 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, r0

The two alternatives execute conditionally while the PC continues sequentially, avoiding the taken-branch penalty for this short choice.

Important qualifications

  • SUB normally does not update flags; SUBS does. A conditional branch must use flags produced by the intended comparison.
  • CBZ and CBNZ do not update flags, only use r0-r7, and support short forward branches.
  • A normal B has a limited relative range. B.W extends the range; BX can use a register-held 32-bit destination.
  • IT blocks 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.