HN2new | past | comments | ask | show | jobs | submitlogin

Method 1 (popping PC off the stack) and Method 3 (mov pc,lr) do not work on the earliest ARM processors that support THUMB, as it will not switch to THUMB mode without executing a BX instruction.

Checking reference manuals:

ARMV4T (ARM7TDMI/ARM9TDMI): Does NOT switch to THUMB mode automatically

ARMV5: Does NOT switch to THUMB mode automatically

ARMV7: Does switch to THUMB mode automatically



I thought it switched to thumb based on odd/ evenness.

Plus if you don't want to switch to thumb, this still works?


It is based on whether the low bit of the jump target is 0 or 1, but the first version of Thumb only did that check-and-switch-mode on a small set of jump instructions, not on every way you could alter the program counter. For the others you got the same behaviour you always had for an attempt to jump to an unaligned address, which is to say the low bit was just ignored. The compiler had to generate slightly different code if you wanted your function to support interworking. In the versions of Thumb starting with IIRC Armv5t or maybe v6t2, more instructions did the mode switch check, and codegen got a bit simpler.


On older ARM only bx is allowed to switch Thumb state, even if the address you're giving it is e.g. a Thumb address in ARM mode. You can still use pc as a GPR to jump ARM-ARM or Thumb-Thumb, though.


It does work if you intend to stay in ARM mode only, and will crash if THUMB-mode code calls the function. ARMV7 will do the mode switch automatically and not crash.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: