Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Assembly (http://www.programmingforums.org/forum20.html)
-   -   Implement C++ functions using MASM (http://www.programmingforums.org/showthread.php?t=11101)

Klarre Aug 17th, 2006 6:54 AM

Implement C++ functions using MASM
 
I have a class with a member function that I wish to implement using MASM.
:

class Foo
{
public:
    Foo() {}
    ~Foo() {}

    int adder(int, int); // <- This should be implemented using MASM
};


:

;ASM source
.486
.model flat, stdcall
option casemap :none

.code

adder proc arg1:dword, arg2:dword
        mov eax, arg1
        add eax, arg2

        ret

adder endp

end

I know how to implement C function, but now C++. I have read this http://www.codeproject.com/cpp/Using...nesVisualC.asp but with no success. How, and where, do I declare the function? Something like
:

extern "C" int __stdcall adder(int, int);
or similiar should be somewhere I guess...
:

int main()
{
    Foo foo = Foo();
    foo.adder(1, 2);
}

This does not work, cause I don't know how to declare the function. Anyone who can help me out with this?

/Thanks for your help!

/Klarre

Wizard1988 Aug 17th, 2006 7:24 AM

Try declaring it right after including headers.

Klarre Aug 17th, 2006 7:34 AM

Tried this
:

#include "foo.h"
extern "C" int __stdcall adder(int, int);
#include <iostream>

int main()
{
        Foo foo = Foo();
        std::cout << foo.adder(4, 5) << std::endl;

        system("pause");

        return 0;
}

Visual Studio gave me this compiler error:
:

error C2039: 'adder' : is not a member of 'Foo'

DaWei Aug 17th, 2006 9:02 AM

Where are you doing anything to associate the definition with the class? :confused:

Eoin Aug 17th, 2006 9:05 AM

This is a bit tough and won't be portable, but its not impossible. First you need to understand what exactly a member function is compiled to, then you emulate that with your ASM function. Important things to know are how the name mangling works or not forgetting that VC passes an implicit this pointer to member functions.

I think your best bet is to write the function in C++, compile into debug code, open that up in a debugger to see what exactly VC expects the function to be called and copy that together with the calling convention in your ASM code.

Also check out the Agner Fog's Manuals.

DaWei Aug 17th, 2006 9:20 AM

Quote:

won't be portable
LOL!!

Eoin Aug 17th, 2006 10:18 AM

Are you laughing at the fact that Assembly is not portable? If so, very fair point :) . I ment though that a solution wouldn't be portable across different C++ compilers, even on identical architectures.

Actually Klarre, forgot to ask you an important question, why do you want to be able to write meber functions in ASM? Its easier to write stdcall or ccall ASM functions, build them into an object file or library, link that into your C++ app and then call those functions from inside member, or indeed any, functions.

DaWei Aug 17th, 2006 10:26 AM

Yeah, no offense, it just tickled my funnybone when I read it.

Klarre Aug 17th, 2006 10:39 AM

Quote:

Originally Posted by Eoin
Actually Klarre, forgot to ask you an important question, why do you want to be able to write meber functions in ASM? Its easier to write stdcall or ccall ASM functions, build them into an object file or library, link that into your C++ app and then call those functions from inside member, or indeed any, functions.

I am just curious about how it works!

Klarre Aug 17th, 2006 12:03 PM

I finally got it linked correctly.
:

class Foo
{
public:
        Foo();
        ~Foo();

        int bar();
};

int main()
{
        Foo foo = Foo();
        int x = foo.bar();

        return 0;
}


:

.386P

; ----------------------------------------------------------------------------
; Foo::Foo()
; ----------------------------------------------------------------------------
_TEXT SEGMENT
??0Foo@@QAE@XZ PROC NEAR
        mov eax, ecx
        ret 0

??0Foo@@QAE@XZ ENDP
_TEXT ENDS

; ----------------------------------------------------------------------------
; Foo::~Foo()
; ----------------------------------------------------------------------------
_TEXT SEGMENT
??1Foo@@QAE@XZ PROC NEAR
        ret 0

??1Foo@@QAE@XZ ENDP
_TEXT ENDS

; ----------------------------------------------------------------------------
; int Foo::bar()
; ----------------------------------------------------------------------------
_TEXT SEGMENT
?bar@Foo@@QAEHXZ PROC NEAR
        mov eax, 4
        ret 0

?bar@Foo@@QAEHXZ ENDP
_TEXT ENDS

END

So now is the next step to play around with variables and other complex stuff.

Thanks alot for your help! See you soon again! ;)

/Klarre


All times are GMT -5. The time now is 12:51 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC