ARM: Introduction to ARM: Branch Instructions

by David Thomas on

Branch Instructions

So how do we implement control structures like for and while loops? Branch instructions are used to alter control flow.

<operation>{cond} <address>

  • BBranch
    • PC := <address>
  • BLBranch with Link
    • R14 := address of next instruction, PC := <address>

How do we return from the subroutine which BL invoked?

MOV pc, r14

or

BX r14 (on ARMv4T or later)

Examples

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'

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

Remarks

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

Navigate