Class with methods written in asm (Linux)

B

bluejolts

Is it possible to make a class with some of its member functions
written in assembly (in a separate file)? For example:

class C {
public:
int method (void);
}

====ASM file====

..global method

method:
XOR EAX,EAX
RET

=========

how do i get these files to link properly?
 
R

red floyd

Is it possible to make a class with some of its member functions
written in assembly (in a separate file)? For example:

class C {
public:
int method (void);
}

====ASM file====

.global method

method:
XOR EAX,EAX
RET

=========


class C
{
public:
int method();
};

extern "C" int asm_method();

inline int C::method()
{
return asm_method();
}

Then write asm_method. That way you don't have to worry about name
mangling.
 
I

Ian Collins

Is it possible to make a class with some of its member functions
written in assembly (in a separate file)?

Not in standard C++. Member functions have C++ linkage with its
associated name mangling, so unless you give your assembly functions the
matching mangled name, they won't match.

Why don't you just use a C++ member function to wrap your assembly
function (probably declared extern "C") or inline code?
 
J

James Kanze

Is it possible to make a class with some of its member functions
written in assembly (in a separate file)? For example:
class C {
public:
int method (void);
}
====ASM file====

.global method

method:
XOR EAX,EAX
RET

=========

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++.
 
L

Laurent D.A.M. MENTEN

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
 
I

Ian Collins

Laurent D.A.M. MENTEN wrote:

Please don't snip attributions.
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.

file: MyClass.cxx
-----------------

#include <stdio.h>

class MyClassclass MyClass

What?

int myAsmFunc( int a ) __asm__( "asmName" );

That's a gcc extension.
 
J

James Kanze

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.

Except that there is no such thing in standard C++. My
experience with calling assembler from C++ has mainly been from
Sun CC, which doesn't support it.

And does the attribute also handle things like class layout?
The layout of struct is usually defined by the system API; the
layout of a class, on the other hand, can get very hairy, and
isn't always well documented.
Just keep in mind that non-static function have a hidden
parameter being the 'this' pointer.

Which may be passed by some special means, rather than by the
usual parameter passing mechanism. (On a Sparc, it's passed in
o0, which is where the first argument would normally go anyway,
but on a register poor machine like an Intel, there would be a
definite advantage in passing it in a register, rather than on
the stack like everything else.)
 
L

Laurent D.A.M. MENTEN

Except that there is no such thing in standard C++.

Damn right, but I doubt there will ever be some standard for asm/c++
interfacing. Is this subject even covered by any c++ book?
And does the attribute also handle things like class layout?
The layout of struct is usually defined by the system API; the
layout of a class, on the other hand, can get very hairy, and
isn't always well documented.

Good question... Since my need for asm in c++ was motivated by
embedded system programming and wrapping of low level primitives
this I have not investigated.
Which may be passed by some special means, rather than by the
usual parameter passing mechanism. (On a Sparc, it's passed in
o0, which is where the first argument would normally go anyway,
but on a register poor machine like an Intel, there would be a
definite advantage in passing it in a register, rather than on
the stack like everything else.)

That is also true, anyway I believe the one who need to mix asm
and c++ knows his job and is assumed to be able to face these
problems...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top