Search code examples
c++assemblyembeddedti-dsp

TI DSP: interfacing C++ and assembly


I posted this Q to TI's 28xx DSP forum but haven't heard a response and figured maybe someone here might know.


I know how to write functions in assembly so that they are C-callable; if the C-callable name is foo() then the assembly function is named _foo().

What if I want to use C++ and optimize a class method in assembly? How do I do that? I assume the only major issues are:

  • naming
  • accessing the "this" pointer
  • accessing class members by somehow knowing offsets

and if I don't want to worry about the last two, then perhaps I would write a static member function and do this:

class MyClass
{
  int x;
  static int _doSomething(int u); // implement this in assembly
public:
  inline void doSomething() { x = _doSomething(x); } 
  // lightweight C++ wrapper to handle the class member / "this" pointer stuff
};

Solution

  • The this pointer gets passed as an additional argument to the function, using the standard calling convention on your platform. On all the platforms I'm familiar with it is passed as the first argument, but I don't do a lot of C++ coding, so I'm not sure if this is guaranteed by the standard. You can always disassemble some C++ code on your platform to confirm.

    The C++ symbol naming is rather more painful than in C, and varies from compiler to compiler. I suppose you could figure out the right symbol name to use by disassembling a compiled function definition, just make sure that: the function is a member of the right class, and has the right number and type of arguments.

    Unless you really need to reproduce a C++ function in situ, I would probably just make a standard C function and do the usual extern "C" { ... } around its declaration.