Glitch City Laboratories Archives

Glitch City Laboratories closed on 1 September 2020 (announcement). This is an archived copy of a thread from Glitch City Laboratories Forums.

You can join Glitch City Research Institute to ask questions or discuss current developments.

You may also download the archive of this forum in .tar.gz, .sql.gz, or .sqlite.gz formats.

Programming/Scripting/Development/Web Design

Questions in regards to ARM assembly: Swapping between ARM and Thumb - Page 1

Questions in regards to ARM assembly: Swapping between ARM and Thumb

Posted by: Couldntthinkofaname
Date: 2019-01-27 08:58:34
Good morning,


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

Re: Questions in regards to ARM assembly: Swapping between ARM and Thumb

Posted by: ISSOtm
Date: 2019-01-27 13:06:22
Instructions are always aligned, so bit 0 of the address is always 0. That's why it's instead used as a mode bit - you'd use `bx ARMCode` and `bx ThumbCode | 1`.