Re: QUERY : ARM mode inline instructions in C Thumb mode file?




"gdisirio" <herpes@xxxxxx> wrote in message news:hKidnZFpiODXnCjVnZ2dnUVZ_uadnZ2d@xxxxxxxxxxxxxxx
hello:

one small question regarding use of ARM inline assembly code in a
C file that has been compiled for Thumb mode.

is it possible to use ARM assembly code from within a C file that
has been compiled for Thumb and Thumb interworking?

how this is done is described on this page:
http://www.devrs.com/gba/files/asmc.txt

i have a C file that has been compiled for Thumb mode. in it, i am
using ARM inline assembly code. apparently, GCC issues no error
message but forcibly converts the ARM code into Thumb code.

i think that GCC requires an entire file to be in either Thumb mode
or ARM mode, but i am not sure. is that true?

the C code that is compiled into Thumb is:

void function_f( void )
{
asm volatile
(
".align \n\t"
".arm \n\t"
"mrs r0, cpsr \n\t"
"msr cpsr_c, r0 \n\t"
"pop { r0 }"
);
}

the C code is compiled with:
arm-elf-gcc -mlittle-endian -mcpu=arm7tdmi -march=armv4t \
-mthumb-interwork -mthumb -mno-tpcs-frame .....

here is the generated code:
00008194 <function_f>:
8194: b580 push {r7, lr}
8196: af02 add r7, sp, #8
8198: 0000 lsls r0, r0, #0
819a: e10f b.n 83bc <wrap+0x10>
819c: f000 e121 blx 4083e0 <_stack+0x3883e0>
81a0: 0001 lsls r1, r0, #0
81a2: e8bd 46bd ldmia.w sp!, {r0, r2, r3, r4, r5, r7, r9,
sl, lr}
81a6: b082 sub sp, #8
81a8: bc80 pop {r7}
81aa: bc01 pop {r0}
81ac: 4700 bx r0


in the inline assembly code, if i do not use ".arm", i get compiler
errors.

is it possible to use ARM assembly code from within a C file that
has been compiled for Thumb and Thumb interworking?

Aaron

Makefile
--------

CC = arm-elf-gcc
TARGET_ARCH = -mlittle-endian -mcpu=arm7tdmi -march=armv4t
TARGET_ARCH += -mthumb -mno-tpcs-frame -mthumb-interwork

all: test
arm-elf-objdump -S -D test > test.lst

clean:
rm -f test test.o function.o


C CODE
------

void function_f( void )
{
asm volatile
(
".align \n\t"
".arm \n\t"
"mrs r0, cpsr \n\t"
"msr cpsr_c, r0 \n\t"
"pop { r0 }"
);
}

int main( void )
{
function_f();
return 0;
}

Try this:

void function_f( void )
{
asm volatile
(
".code 32 \n\t"
"mrs r0, cpsr \n\t"
"msr cpsr_c, r0 \n\t"
".code 16 \n\t"
"pop { r0 }"
);
}

It generates:

function_f:
.code 32
mrs r0, cpsr
msr cpsr_c, r0
.code 16
pop { r0 }
bx lr

That is still incorrect as it doesn't switch to ARM, nor switch back to Thumb...
You can't do this unless you:

A. Switch to ARM mode for the whole function - many compilers can switch
between ARM and Thumb on a per function basis. I don't know whether GCC
supports this yet. The key is that the function_f symbol is marked as ARM,
not Thumb, or it will be incorrectly called.

B. Use a Thumb function, like above, and explictly switch to ARM yourself,
do your ARM sequence and BX lr to switch back if needed.

You should use a naked assembler function to stop the compiler from
generating its own entry/exit sequences.

Btw why corrupt the stack using pop {r0}?

Wilco


.



Relevant Pages

  • Re: QUERY : ARM mode inline instructions in C Thumb mode file?
    ... one small question regarding use of ARM inline assembly code in a ... C file that has been compiled for Thumb mode. ... in the inline assembly code, if i do not use ".arm", i get compiler ...
    (comp.arch.embedded)
  • Re: How to compile a project in Thumb mode
    ... Use thumb compiler for .C files instead of arm compiler with enabled ... As far as converting/compiling .s files in thumb mode is ...
    (comp.sys.arm)
  • Re: Barring and guitar neck shape
    ... Hello Steve-I've been playing for 35 years and I use barred positions ... The thumb supplies leverage for whatever position your LH ... Use of gravity or arm weight ... To get the "feel" of using arm weight for barring, ...
    (rec.music.makers.guitar.jazz)
  • Re: interworking flag enabled for the Linker
    ... I find that there is a interworking flag that has to be enabled to ... Thumb code is that it is generally more compact, while the advantage of ARM ... make is which functions to compile to ARM to get optimal performance ...
    (comp.sys.arm)
  • Re: exercise question
    ... forearm outside of simply playing? ... Try playing without the left thumb on the back of the neck. ... Can you feel the right forearm countering the pull of the left ... ask them to lift their right arm ...
    (rec.music.classical.guitar)