Thread: inline assembly

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    27

    inline assembly

    Hi,

    Need urgent help with the following problem
    trying to call a class member function from a class using inline assembly

    is this possible at all?

    Code:
      __asm { 
            __asm push edx                  
            __asm push eax                  
            __asm call MyFunction
            __asm pop ecx                  
            __asm pop ecx                   
    }
    MyFunction is the memberfunction

    the error is:
    error C2420: 'MyFunction' : illegal symbol in first operand
    error C2415: improper operand type

    I'm using MSVC++6.0

    I guess this has something to do with the name mangling of the member function, or?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    __asm { 
            __asm push edx                  
            //...
    }
    I don't think you need those __asm's in there.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Oh, I know . . . in C I think it would be
    Code:
    call _MyFunction
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    it is technicall possible, but you have to know an awful lot about how your compiler mangles function names. Set you compiler to generate an assembly listing of your program then look to see how the compiler did it. It's a lot easier (and safer) to just let your compiler generate the assembly instructions to call class methods.

    Here is a simple sample where CMyClass::foo() is calling a class function is_int() with one parameter.
    Code:
    	push	252					; 000000fcH
    	mov	ecx, DWORD PTR _this$[ebp]
    	call	?is_int@CMyClass@@QAE_NH@Z		; CMyClass::is_int
    	mov	BYTE PTR _b$[ebp], al
    Last edited by Ancient Dragon; 09-29-2005 at 01:18 PM.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    27
    I get a whole lot of errors when doing this. I fetch the mangled name from the map file. But when i use this, I get:

    error C2018: unknown character '0x40'

    Maybe there's someway of escaping these characters?


    according to msdn:
    An __asm block can call only global C++ functions that are not overloaded. If you call an overloaded global C++ function or a C++ member function, the compiler issues an error.

    You can also call any functions declared with extern "C" linkage. This allows an __asm block within a C++ program to call the C library functions, because all the standard header files declare the library functions to have extern "C" linkage.

    (couldn't send a link to the page so....)

    /S

  6. #6
    ---
    Join Date
    May 2004
    Posts
    1,379
    What a waste of time. Just use C++.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > trying to call a class member function from a class using inline assembly
    Why?
    Just to see if you can?

    I can't see any practical reason for this - nevermind the complete loss of portability in the process.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    27
    it's an interpreter for a certain instruction set, which has already been implemented in inline assembly. I want to use this in another work using classes, but I don't want to rewrite the all those instructions again

  9. #9
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Code:
    #include <cstdio>
    
    class Foobar
    {
    public:
    	void CallMe(int x)
    	{
    		printf("%d\n",x);
    	}
    };
    
    int main(int argc, char* argv[])
    {
    	Foobar foo;
    	__asm
    	{
    		MOV EAX,10
    		PUSH EAX
    		MOV ECX, DWORD PTR [foo]
    		CALL Foobar::CallMe
    	}
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help using inline assembly for keyboard input
    By sleventeen in forum C Programming
    Replies: 7
    Last Post: 05-10-2009, 01:31 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Arrays In Inline x86 Assembly
    By saxman in forum C Programming
    Replies: 17
    Last Post: 07-07-2004, 02:38 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  5. inline assembly question
    By DavidP in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2002, 06:14 AM