Search code examples
armthumb

Jump between Thumb and ARM


I am interested in the ARM and Thumb2 commands: LDR and LDR.W, PC, =ADDR for absolute jumping to a certain address.

For example, when I jump from ARM code to ARM, the command LDR PC, =ADDR is performed. But what happens in the other scenarios?

from ARM to Thumb2

from Thumb2 to Thumb2

from Thumb2 to ARM

when is +1 needed to be added to the address? and why?


Solution

  • The rule is actually quite simple:

    • If bit 0 of the address is 0, the CPU will execute the code as ARM code after the next branch
    • If bit 0 of the address is 1, the CPU will execute the code as Thumb after the next branch
    • Of course if there is a mismatch, the CPU will certainly get a fault (After executing random code) because it has no way to check if the code is ARM or Thumb.

    This is what explains the +1.

    Note that depending on the compiler, and depending on the label used, bit 0 of the address may be automatically set by the compiler.