How to call C function from assembly ?

opcode_x64
Posts: 47
Joined: Sun Jan 13, 2019 5:39 pm

How to call C function from assembly ?

Postby opcode_x64 » Wed Apr 22, 2020 7:45 pm

Hello,

can you please provide an example for calling a C function in main.c from assembly file ? I just tried following code:

main.c

Code: Select all

extern void Test(int a){
   printf("Test was called!\n");
}

void app_main(){
..
..
..}
assembly file

Code: Select all

..
..
.section .iram1,"ax"
 .global     xt_highint5
 .type       xt_highint5,@function
 .align      4
.extern _Test

..
..
call0 _Test
I am getting error message: undefined reference .... while compiling. I could not find any example for xtensa assembly to call a C function in main.c with the function name.

I appreciate every help !

Best regards,
opcode_x64

ESP_Sprite
Posts: 8921
Joined: Thu Nov 26, 2015 4:08 am

Re: How to call C function from assembly ?

Postby ESP_Sprite » Wed Apr 22, 2020 8:59 pm

From memory:
- The 'extern' is not needed.
- You need to add a '.global _test' to your assembly code
- You need to call using a call4, call8 or call12 instruction, dependent on which local registers you want to keep.
However:
- You can't call C functions from high-level interrupts, as the stack is not guaranteed to be in a sane state.
- You can't call blocking functions, like printf, from _any_ interrupt.

opcode_x64
Posts: 47
Joined: Sun Jan 13, 2019 5:39 pm

Re: How to call C function from assembly ?

Postby opcode_x64 » Wed Apr 22, 2020 9:36 pm

Hey ESP_Sprite,

thanks for your help :) . Actually I want to call a function in C which uses with Inline Assembler the s32c1i function for lock and unlock operation of synchronized variables between the high level interrupt on core1 (Producer) and a consumer Task on core0. The C functions for lock and unlock is working with structure object and because I dont know if its possible to address an structure object variable in Assembler as one can do with a normal variable, the idea was to stay in C and call IT from Assembler... But it may be easier to use directly Assembler Code for lock and unlock inside the Assembly File instead of using C function...

Who is online

Users browsing this forum: No registered users and 152 guests