![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 291
Rep Power: 4
![]() |
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 extern "C" int __stdcall adder(int, int); int main()
{
Foo foo = Foo();
foo.adder(1, 2);
}/Thanks for your help! /Klarre
__________________
http://www.klarre.se |
|
|
|
|
|
#2 |
|
Professional Programmer
|
Try declaring it right after including headers.
__________________
JG-Webdesign |
|
|
|
|
|
#3 |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 291
Rep Power: 4
![]() |
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;
}error C2039: 'adder' : is not a member of 'Foo'
__________________
http://www.klarre.se |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3
![]() |
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. |
|
|
|
|
|
#6 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
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 |
|
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3
![]() |
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. |
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#9 | |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 291
Rep Power: 4
![]() |
Quote:
__________________
http://www.klarre.se |
|
|
|
|
|
|
#10 |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 291
Rep Power: 4
![]() |
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 Thanks alot for your help! See you soon again! ![]() /Klarre
__________________
http://www.klarre.se |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |