Questions in regards to ARM assembly: Swapping between ARM and Thumb
Posted by: Couldntthinkofaname
Date: 2019-01-27 08:58:34
I am beginning to learn ARM assembly, and there's one concept i'm not sure I quite grasp.
From ARM's docs, it would seem that if I `bx <reg>|<val>`, the value at <reg> or <val> respectively needs to have bit 0 set if it is switching from ARM >>> Thumb, and reset if it's going from Thumb >>> ARM.
Would that entail an alignment of the subroutines, to ensure that the bits are set/reset respectively?
Example (GAS Assembler):
.arm
.align 2
EpsisAmazingSubroutine: @The alignment of this subroutine means that bit 0 is reset, so bx from a thumb subr will switch back to ARM
stmdb sp!,{lr,fp}
add fp,sp,#0
sub sp,sp,#4
@ var at fp-4 is EpsisAmaingInteger
mov r3,#3
str r3,[fp,#-4]
@ EpsisAmazingInteger is now 3
sub r0,fp,#4 @ Nab ptr to EpsisAmazingInteger
bxl add3toInt @ Swap to thumb and call subr
@ EpsisAmazingInteger should now be 6
@ Close stack frame and leave
add sp,fp,#0
ldm sp!,{fp,lr} @ ARMv4 doesn't change state on pop {pc}
bx lr
.thumb
.align 2
.byte 0x69
add3toInt: @ The alignment, coupled by the byte, should set bit 0 of this address
@ We don't need a stack frame here
ldr r3,[r0]
add r3,r3,#3
str r3,[r0]
bx lr @ (Hopefully) swap back to ARM and branch to link reg
Note: Assume CPU is ARM7TDMI