So how do we implement control structures like
while loops? Branch instructions are used to alter control flow.
- PC := <address>
BL– Branch with Link
- R14 := address of next instruction, PC := <address>
How do we return from the subroutine which
MOV pc, r14
BX r14 (on ARMv4T or later)
Branching forward, to skip over some code:
... ; some code here B fwd ; jump to label 'fwd' ... ; more code here fwd
Branching backwards, creating a loop:
back ... ; more code here B back ; jump to label 'back'
BL to call a subroutine:
... ... BL calc ; call 'calc' ... ; returns to here ... calc ; function body ADD r0, r1, r2 ; do some work here MOV pc, r14 ; PC = R14 to return
Branches are PC-relative. +/-32M range (24 bits × 4 bytes).
Since ARM’s branch instructions are PC-relative the code produced is position independent — it can execute from any address in memory. Certain systems such as BREW use this.
How can we perform longer branches which access the full 32-bit address space?
You can set up the LR manually if needed, then load into PC:
MOV lr,pc LDR pc,=dest