...call an assembly-coded function from C or C++?
How do I go about writing a function in assembly code and calling it from C or C++?
The main thing to be aware of is the ARM's procedure call standard. This is described in detail at http://infocenter.arm.com/help/topic/com.arm.doc.ihi0042c/IHI0042C_aapcs.pdf
For the assembly code function's entry point to be visible to other modules you should use the .global directive
For example, here is a very simple function written in assembly code, it takes one parameter which it doubles and returns:
.text 32
global func
func:
add r0, r0, r0
bx lr
From C, you would call this function as follows:
unsigned func(unsigned n);
int main(void) {
unsigned n = func(10);
return 0;
}
To call this function from C++, the only difference is that you would need to declare the function as an external C function:
extern "C" unsigned func(unsigned n);
int main(void) {
unsigned n = func(10);
return 0;
}
Please sign in to leave a comment.
Comments
0 comments