3.14 Calls to C++ from assembly language

You can call a C++ function from assembly code by defining it as extern "C" and using the IMPORT directive.

Defining the C++ function to be called

To be able to call a C++ function from assembly code, use the extern "C" declaration:

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;
}

Defining the calling function in assembly language

In ARM assembly language, import the name of the C++ function using the IMPORT directive and use a BLX instruction to call it:

    PRESERVE8
    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
    BLX    cppfunc         ; call 'cppfunc' so it can change the struct
    LDR    R0, [sp], #4
    ADD    R0, R0, R0,LSL #1
    LDMFD  sp!,{pc}
    END
Non-ConfidentialPDF file icon PDF versionARM DUI0471M
Copyright © 2010-2016 ARM Limited or its affiliates. All rights reserved.