| |||
| Home > Mixing C, C++, and Assembly Language > Calls to C++ from assembly language | |||
To be able to call a C++ function from assembly, use the extern
"C" declaration:
Example 23. Defining the function to be called in C++
struct S { // has no base classes or virtual functions
S(int s) : i(s) { }
int i;
}; extern "C" void cppfunc(S * p) {
// Definition of the C++ function to be called from ASM.
// The body is C++, only the linkage is C.
p->i += 5;
}
In ARM assembly language, import the name of the C++ function
and use a Branch with Link (BL)
instruction to call it:
Example 24. Defining assembly language function
AREA Asm, CODE
IMPORT cppfunc ; import the name of the C++
; function to be called from Asm
EXPORT f
f
STMFD sp!,{lr}
MOV R0,#2
STR R0,[sp,#-4]! ; initialize struct
MOV R0,sp ; argument is pointer to struct
BL cppfunc ; call 'cppfunc' so it can change the struct
LDR R0, [sp], #4
ADD R0, R0, R0,LSL #1
LDMFD sp!,{pc}
END