how do we implement control structures like for and while loops?
Branch instructions are used to alter control flow.
B – BranchBL – Branch with LinkB – BranchBL – Branch with LinkHow do we return from the subroutine which BL invoked?
MOV pc, r14
or
BX r14 (on ARMv4T or later)
What does the following code do?
... ; some code here B fwd ; jump to label 'fwd' ... ; more code here fwd
Branching forward, to skip over some code:
... ; some code here B fwd ; jump to label 'fwd' ... ; more code here fwd
What does this code do?
back ... ; more code here B back ; jump to label 'back'
Branching backwards, creating a loop:
back ... ; more code here B back ; jump to label 'back'
What does this code do?
... ... 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
Using 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
Are Branches PC-relative?
Branches are PC-relative. +/-32M range (24 bits × 4 bytes).
ARM’s branch instructions are PC-______ the code produced is position ___________ — it can execute from any address in memory. Certain systems such as BREW use this.
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