Course: Computer Architectures
Source lesson: ARM Branches and Control Flow (PDF pages 2-8)
1. Direct and indirect branches
B label changes the PC to a nearby label. BX Rn changes the PC to the address stored in a register. BL label and BLX Rn also save the address of the next instruction in LR (r14), so they call subroutines.
BL calculate
; execution returns here
calculate:
ADD r0, r0, r1
BX lrB is a jump without a return protocol. BL is a call because it preserves where execution should continue.
2. Infinite loops
A bare-metal program should not fall through past its final instruction. A common safe endpoint is:
stop:
B stop3. Address-loading choices
LDR r0, =target
BX r0loads a target address, often through a literal pool. ADR computes a nearby PC-relative address:
ADR r0, target
BX r0LDR =label and ADR are not interchangeable in every situation: ARM’s Thumb state and the least-significant address bit matter when BX forms the destination. The source examples show that LDR can set bit 0 for code labels, whereas ADR does not automatically do so.
4. Range and PC changes
A normal Thumb B has about �2 KB of relative range. B.W extends this to about �16 MB. BX can jump using a full 32-bit register value. MOV PC, Rn and LDR PC, =label also change control flow, but the source warns that these forms force the last PC bit to zero.
Retrieval checks
Q: Why does BL need LR?
A: It stores the return address so BX lr can resume after the call.
Q: When is BX preferable to B?
A: When the destination is held in a register or a direct relative branch cannot reach it.
Q: Why is B stop useful at the end of a bare-metal program?
A: It prevents execution from continuing into an unintended memory region.