Click here to Skip to main content
15,886,661 members
Articles / Programming Languages / ASM

How to invoke C++ member operations from inline-assembler code segments

Rate me:
Please Sign up or sign in to vote.
4.74/5 (15 votes)
19 Sep 20057 min read 50.4K   443   42  
Calling C++ operations from assembler code using member function pointers.
// ASM2CPPInvoker.cpp : Definiert den Einsprungpunkt f�r die Konsolenanwendung.
//

#include "stdafx.h"

class ServiceA
{
	char c;
public:
	void sub(int a, int b)
	{
		printf("ServiceA: %d - %d = %d\n", a, b, a-b);
	}
	virtual void add(int a, int b)
	{
		printf("ServiceA: %d + %d = %d\n", a, b, a+b);
	}
	virtual void mul(int a, int b)
	{
		printf("ServiceA: %d * %d = %d\n", a, b, a*b);
	}
};
class ServiceB : public ServiceA
{
public:
	void sub(int a, int b)
	{
		printf("ServiceB: %d - %d = %d\n", a, b, a-b);
	}
	virtual void add(int a, int b)
	{
		printf("ServiceB: %d + %d = %d\n", a, b, a+b);
	}
};



typedef void (ServiceA::*TypeAPtr)(int, int);

int main(int argc, char* argv[])
{
	ServiceA serviceA; ServiceB serviceB;
	ServiceA *pSA = &serviceB;
	TypeAPtr _add = &ServiceA::add;


	pSA->add(10,50);
	pSA->mul(102,50);
	pSA->sub(20,5);

	(pSA->*_add)(10,20);
	_asm
	{
		lea ecx, serviceB;
		push 60;
		push 40;
		call _add;
	}

	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Chief Technology Officer W3L
Germany Germany
-Since 1th August 2007: Chief Technology Officer of W3L
-2002/08/01-2007/07/31: PhD student
-1997/10/15-2002/07/31: Studied Electrical Engineering and Computer Science

Comments and Discussions