Home > OS >  Call C function from Assembly, passing args and getting the return value in the ARM calling conventi
Call C function from Assembly, passing args and getting the return value in the ARM calling conventi

Time:08-06

I want to call a C function, say:

int foo(int a, int b) {return 2;}

inside an assembly (ARM) code. I read that I need to mention

import foo

in my assembly code, for assembler to search for foo in C file. But, I am stuck at passing arguments a and b from assembly and retrieving an integer (here 2) again back in assembly. Could someone could explain me how to do this, with a mini example?

CodePudding user response:

I would write a function that calls your function and look on https://godbolt.org/ what assembly gets created for that function call, especially how the parameters get set. Then recreate that in your own code.

This is probably not the correct way, but I think that should work as well.

CodePudding user response:

What you need to do is place the arguments in the correct registers (or on the stack) as required. All the details on how to do this are what is known as the calling convention and forms a very important part of the Application Binary Interface(ABI).

Details on the ARM (Armv7) calling convention can be found at: https://developer.arm.com/documentation/den0013/d/Application-Binary-Interfaces/Procedure-Call-Standard

  • Related