branch-instructions

Search IconIcon to open search

Source: samek-embedded

Branch instructions

  • B.N (Branch) instruction modifies the PC register so that it skips to a different instruction
  • BLT.N conditional branching
    • only modifies PC if the N bit in the APSR is set.
    • the instruction to jump to is encoded within the instruction itself: 0xFC = -4, so jump back 4 instructions, from 0x8e to 0x8a
  • Branching results in pipeline delays –> solution e.g. loop unrolling
  • BL saves the address of the next instruction into the LR link register .
    • The previous value of LR (i.e. previous function return address) must also be saved somehow.
    • The previous return address is saved to the stack .
  • Example of a main function executing (branching off into) the delay subfunction:
    1
    2
    3
    4
    5
    
    int main(){
        GPIO_PORTF_DATA_BITS_R[LED_RED] = LED_RED;
        delay();	
        GPIO_PORTF_DATA_BITS_R[LED_RED] = 0;
    }
    
    1
    
    
    
DisassemblyRegister
Before BL
After BL

Notes:

  • After branching off into the subfunction delay, the stack is expanded by 4 memory units, as can be seen in the SUB SP, SP, #0X4 instruction.
    1
    
    0xF0 - 0x4 = 0xEC
    
  • At the end of delay(), the reverse occurs. The stack is shrunk by 4 memory units: ADD SP, SP, #0X4 and points to 0xF0 again, just like before delay() was executed.
  • The return of the delay() function is given by the BX branch instruction .
  • It would be expected that, right after BL, LR stores the instruction address 0x94, but instead 0x95 is stored.
    • This is also at odds with the fact that the ARM instructions must be aligned at even addresses.
    • This is a legacy behaviour explained in the BX section .
    • At the end of the subfunction (after executing BX), the PC register is indeed updated to the correct instruction 0x94.

BX (branch and exchange)

  • Performed upon return of a function.
  • Sets PC to the value of LR.
    • However, not all bits of LR are transferred to PC.
    • The least significant bit of PC is always set to zero, as the return address must be even.
    • Therefore, the least significant bit of LR is not used for addressing.
    • Instead, it is used as the instruction set exchange bit.
      • If 1: processor switches to the THUMB instruction set.
      • If 0: processor switches to the ARM instruction set.
    • This behavour, however, moot in Cortex-M (no possibility to switch to ARM instructions), and is therefore just a legacy behaviour.