Skip to main content

IAR Embedded Workbench for RL78 5.20

Calling assembler routines from C++

In this section:

The C calling convention does not apply to C++ functions. Most importantly, a function name is not sufficient to identify a C++ function. The scope and the type of the function are also required to guarantee type-safe linkage, and to resolve overloading.

Another difference is that non-static member functions get an extra, hidden argument, the this pointer.

However, when using C linkage, the calling convention conforms to the C calling convention. An assembler routine can therefore be called from C++ when declared in this manner:

extern "C" 
{
  int MyRoutine(int);
}

The following example shows how to achieve the equivalent to a non-static member function, which means that the implicit this pointer must be made explicit. It is also possible to “wrap” the call to the assembler routine in a member function. Use an inline member function to remove the overhead of the extra call—this assumes that function inlining is enabled:

class MyClass;

extern "C" 
{
  void DoIt(MyClass *ptr, int arg);
}

class MyClass 
{
public:
  inline void DoIt(int arg) 
  { 
    ::DoIt(this, arg); 
  }
};