473,386 Members | 1,943 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Class with methods written in asm (Linux)

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?

Oct 5 '07 #1
6 2069
bl*******@gmail.com wrote:
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.
Oct 5 '07 #2
bl*******@gmail.com wrote:
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?

--
Ian Collins.
Oct 5 '07 #3
On Oct 5, 2:01 am, bluejo...@gmail.com wrote:
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++.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 5 '07 #4
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
Oct 5 '07 #5
Laurent D.A.M. MENTEN wrote:

Please don't snip attributions.
James Kanze wrote:
>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.

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

#include <stdio.h>

class MyClassclass MyClass
What?

<snip>
>
int myAsmFunc( int a ) __asm__( "asmName" );
That's a gcc extension.

--
Ian Collins.
Oct 5 '07 #6
>class MyClassclass MyClass
>
What?
Oops... typed twice....
> int myAsmFunc( int a ) __asm__( "asmName" );

That's a gcc extension.
Yes it is... anyway asm and c++ mix is highly compiler dependent.
Oct 6 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Dave | last post by:
Hi, I have done some research, trying to Clear The Screen in java code. The first option was the obv: system.out.print("\n\n\n\n\n\n\n\n\n\n\n\n"); then i heard about this method:...
6
by: Sridhar R | last post by:
I am looking for a class browser that has these features. 1. Given a symbol (class, method or function) it should giveback the lineno n source code 2. It should be efficient and quick. I...
3
by: Sibylle Koczian | last post by:
I want to try out several applications, all doing the same thing but using different GUI libraries (Tkinter, Qt, wxWindows). Using an example I found in a book I wrote a class containing the GUI...
4
by: Neil Zanella | last post by:
Hello, I would like to know whether it is possible to define static class methods and data members in Python (similar to the way it can be done in C++ or Java). These do not seem to be mentioned...
9
by: Piotre Ugrumov | last post by:
I would have to implement a class hierarchy. The base class is the class Quadrilateral. The discendents arre th classes Trapezoid, Parallelogram, Rectangle, Square. Quadrilateral |_ Trapezoid...
11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
10
by: Bonzol | last post by:
vb.net Hey there, could someone just tell me what the differnce is between classes and modules and when each one would be used compared to the other? Any help would be great Thanx in...
49
by: Ben Voigt [C++ MVP] | last post by:
I'm trying to construct a compelling example of the need for a language feature, with full support for generics, to introduce all static members and nested classes of another type into the current...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.