Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 17th, 2006, 6:54 AM   #1
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 301
Rep Power: 4 Klarre is on a distinguished road
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
__________________
http://www.klarre.se
Klarre is online now   Reply With Quote
Old Aug 17th, 2006, 7:24 AM   #2
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 417
Rep Power: 4 Wizard1988 is on a distinguished road
Send a message via AIM to Wizard1988
Try declaring it right after including headers.
__________________
JG-Webdesign
Wizard1988 is offline   Reply With Quote
Old Aug 17th, 2006, 7:34 AM   #3
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 301
Rep Power: 4 Klarre is on a distinguished road
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'
__________________
http://www.klarre.se
Klarre is online now   Reply With Quote
Old Aug 17th, 2006, 9:02 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Where are you doing anything to associate the definition with the class?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Aug 17th, 2006, 9:05 AM   #5
Eoin
Hobbyist Programmer
 
Eoin's Avatar
 
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3 Eoin is on a distinguished road
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.
__________________
Visit my website BinaryNotions.
Eoin is offline   Reply With Quote
Old Aug 17th, 2006, 9:20 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
won't be portable
LOL!!
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Aug 17th, 2006, 10:18 AM   #7
Eoin
Hobbyist Programmer
 
Eoin's Avatar
 
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3 Eoin is on a distinguished road
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.
__________________
Visit my website BinaryNotions.
Eoin is offline   Reply With Quote
Old Aug 17th, 2006, 10:26 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Yeah, no offense, it just tickled my funnybone when I read it.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Aug 17th, 2006, 10:39 AM   #9
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 301
Rep Power: 4 Klarre is on a distinguished road
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!
__________________
http://www.klarre.se
Klarre is online now   Reply With Quote
Old Aug 17th, 2006, 12:03 PM   #10
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 301
Rep Power: 4 Klarre is on a distinguished road
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
__________________
http://www.klarre.se
Klarre is online now   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C# corruption!!! Kilo C++ 32 May 21st, 2006 8:44 PM
binding between ordinary member functions & polymorphic member functions ASH C++ 2 May 11th, 2006 4:36 AM
Implement "Whats This" using Win32 hemanth.balaji C++ 1 May 29th, 2005 7:03 AM
Exporting Functions victorsk Visual Basic 1 May 18th, 2005 2:32 PM
User-defined creatNode and deleteNode functions for a doubly-linked list jgs C 2 Apr 28th, 2005 8:53 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:19 AM.

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