User guide

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
Passing a reference between C and C++
Example 4-21 and Example 4-22 show how to pass a reference between C and C++.
Example 4-21 C++ function
extern "C" int cfunc(const int&);
// Declaration of the C function to be called from C++
extern "C" int cppfunc(const int& r) {
// Definition of the C++ to be called from C.
return 7 * r;
}
int f() {
int i = 3;
return cfunc(i); // passes a pointer to 'i'
}
Example 4-22 Defining the C function
extern int cppfunc(const int*);
/* declaration of the C++ to be called from C */
int cfunc(const int* p) {
/* definition of the C function to be called from C++ */
int k = *p + 4;
return cppfunc(&k);
}
Calling C++ from C or assembly language
The code in Example 4-23, Example 4-24 and Example 4-25 demonstrates how to call a non-static, non-virtual C++
member function from C or assembly language. Use the assembler output from the compiler to locate the mangled
name of the function.
Example 4-23 Calling a C++ member function
struct T {
T(int i) : t(i) { }
int t;
int f(int i);
};
int T::f(int i) { return i + t; }
// Definition of the C++ function to be called from C.
extern "C" int cfunc(T*);
// declaration of the C function to be called from C++
int f() {
T t(5); // create an object of type T
return cfunc(&t);
}
Example 4-24 Defining the C function
struct T;
extern int f__1TFi(struct T*, int);
/* the mangled name of the C++ */
/* function to be called */
int cfunc(struct T* t) {
Mixing C, C++, and Assembly Language
Copyright ?1999 2001 ARM Limited 4-15