Re: QUERY : ARM mode inline instructions in C Thumb mode file?
- From: "Wilco Dijkstra" <Wilco.removethisDijkstra@xxxxxxxxxxxx>
- Date: Wed, 27 Aug 2008 10:44:59 +0100
"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
.
- References:
- Prev by Date: Helpful :)
- Next by Date: FPGA/CPLD Design Group on LinkedIn
- Previous by thread: Re: QUERY : ARM mode inline instructions in C Thumb mode file?
- Next by thread: Re: QUERY : ARM mode inline instructions in C Thumb mode file?
- Index(es):
Relevant Pages
|