It's possible, but it's a lot of work. You have to specify the
mangled name in the assembler, and perhaps deal with special
ways the this argument may be passed. In general, if I had to
do this, I'd define a C-style struct, and extern "C" functions
which manipulated it, and then wrap that in a class for C++.
The C API is generally well defined and stable, while the same
isn't necessarily true for C++.
Not that much of work indeed! Have a look at the following skeleton. The
asm function simply returns its argument added to the member i. The
whole magic is the __asm__ attribute which allow you to give an
arbitrary name to a function... with no name mangling.
Just keep in mind that non-static function have a hidden parameter being
the 'this' pointer.
file: MyClass.cxx
-----------------
#include <stdio.h>
class MyClassclass MyClass
{
private:
int i;
public:
MyClass( int = 0 );
int myAsmFunc( int a ) __asm__( "asmName" );
};
MyClass::MyClass( int _i )
{
this->i = _i;
}
int main( int nArgC, char* pszArgV[] )
{
MyClass* a = new MyClass( 1 );
int r = a->myAsmFunc( 2 );
printf( "result : %d\n", r );
return 0;
}
file: MyClass.S
---------------
.file "MyClass_asm.S"
.globl asmName
.text
asmName:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %eax /* get this pointer */
movl 0(%eax), %eax /* get this->i (offset 0) */
addl 12(%ebp), %eax /* add parameter */
leave
ret
.type asmName, @function
.size asmName, . - asmName