Topic : Trying to intermix C and assembly coding

Forum : ARM

Original Post
Post Information Post
August 15, 2010 - 6:50pm
Guest

Hello
I have been trying hard to mix C and assembly coding .I was able to do it with regular gcc tools but RIDE somehow is not supporting it. I have included both the assembly and the c files and both of them build but during debug session they donot work the branch and link to the function fun() somehow branches to location 0 .

ASSEMBLY CODE
.text
.thumb
.syntax unified
.global fun
fun:
mov r0,#1
mov pc,lr

C Code
extern int fun(void);
int main (void)
{
int x;
x=fun();
while(1);
return 0;
}

Thanks in advance

Replies
Post Information Post
+1
0
-1
August 16, 2010 - 11:46am
Raisonance Support Team

Hi,

I don't see what you mean by 'regular gcc tools', but Ride uses the standard GCC executables, so this can probably be considered as 'regular gcc tools'.

Your problem is probably in the project options, which could generate wrong command lines. You can check the command lines that Ride launches, in the build log window of Ride. Compare them with the command lines that work.

I hope it helps.

Best Regards,

Vincent

+1
0
-1
August 16, 2010 - 12:47pm
Guest

Hi
'regular gcc tools'- I regularly use MinGw and gcc arm tools. Using a makefile I am able to link the object codes of assembly and C.

As you suggested I have been viewing the project options . I have enabled the assembly file to be debugged and linked.

Is there any project option that I have to enable so that the assembly files's object code links to the C code ?

The following is the build log that I am getting when I build the project. Both the assembly and C files have already built properly and list,object files for them are also generated . Test_app is the name of the application and its located on my desktop.

Running: LD Linker
"C:\Program Files\Raisonance\Ride\arm-gcc\bin\arm-none-eabi-gcc.exe" -mcpu=cortex-m3 -mthumb -Wl,-T -Xlinker "C:\Users\Lokesh\Desktop\testing\test_appl.elf.ld" -u _start -Wl,-static -Wl,--gc-sections -nostartfiles -Wl,-Map -Xlinker "C:\Users\Lokesh\Desktop\testing\test_appl.map" -Wl,--warn-once
"C:\Program Files\Raisonance\Ride\arm-gcc\bin\arm-none-eabi-objcopy.exe" "C:\Users\Lokesh\Desktop\testing\test_appl.elf" --target=ihex "C:\Users\Lokesh\Desktop\testing\test_appl.hex"
"C:\Program Files\Raisonance\Ride\Bin\rexrdr.exe" "C:\Users\Lokesh\Desktop\testing\test_appl.elf.sizetmp" 0 "C:\Program Files\Raisonance\Ride\arm-gcc\bin\arm-none-eabi-size.exe" "C:\Users\Lokesh\Desktop\testing\test_appl.elf"
"C:\Program Files\Raisonance\Ride\Bin\dwf2xml.exe" "C:\Users\Lokesh\Desktop\testing\test_appl.dbi" "C:\Users\Lokesh\Desktop\testing\test_appl-globals.Standard.xml" "C:\Users\Lokesh\Desktop\testing\test_appl.Standard.xml"

Regards
Lokesh

+1
0
-1
August 16, 2010 - 12:50pm
Guest

Also the bl instruction to fun() during the debug session is replaced by UNDEF in case I use assembly coding . But a C code of the same function works correctly.

+1
0
-1
August 16, 2010 - 5:16pm
Raisonance Support Team

Hi,

You must add another ".thumb_func" assembler directive for telling the linker that the code is a thumb function:

.text
.thumb
.syntax unified
.global fun
.thumb_func
fun:
mov r0,#1
mov pc,lr

I checked on the files that you sent by email that this solves the problem.

In general, the procedure for writing an assembler function to be called by C is this:

1. Write a C file that contains just a basic function with the same name and prototype as the assembler function you want to write. (it should read the parameters and return something)

2. Select the "Save Temp Files" compiler options and compile your C file. The compiler will then generate a ".s" file in the folder where your C file is.

3. Open the .s file generated by the compiler, copy it and use it as basis for your assembler file. It includes all the required directives and declarations for your function, and examples about how to access the parameters and return a value. You just need to remove references to the original c file, in order to allow debugging.

This is how I found out the missing directive in your example: I compiled this C code and looked at the generated asm:

int func(void)
{
return 1;
}

Best Regards,

Vincent

+1
0
-1
August 16, 2010 - 6:32pm
Guest

Thank you for such a wonderful support. I had been in been facing trouble for the last few days on this issue of C and Assembly inter-networking.
Keep up the good work.

Regards
Lokesh Siddhu