简体   繁体   中英

Porting assembly code to c++

I am porting assembly code to C++ and I have some problems porting a portion of it.

I have a pointer declared as:

void* function;

and it is pointing to a function call:

void* function = getfunction(lib, fname);

I have a inline assembly code which I have to migrate to C++:

__asm { call function }

How to call this pointer in C++?

Please note that the arguments lib and fname are of type void* .

And void* function points to value returned by getfunction() .

In what follows, I am assuming that the entire function call is encapsulated by

__asm { call function }

In which case this is a function pointer that can be declared like this:

void (*function)(void);

The __asm block does not push parameters onto the stack. Therefore the parameter list is empty. And the __asm block does not extract a return value. So the function has void return type.

You assign to the function pointer in just the same way:

function = getfunction(lib, fname);

And you call it like this:

function();

Look at the following example :

// ptrFunc is the name of the pointer to the getFunction
void* (*ptrFunc)(void*, void*) = &getfunction;
// ... Declaring whatever lib and fname are
// Now call the actual function by using a pointer to it
ptrFunc(lib, fname);
// Another form of how to call getFunc might be :
(*ptrFunc)(lib, fname);

Also, you can pass function pointers to another function such as :

void *getFunction(void* lib, void* fname)
{
   // Whatever
}

void myFunction( void* (*ptrFunc)(void*, void*) )
{
   // void *lib = something;
   // void *fname = something else;
   ptrFunc(lib, fname);
}

int main()
{
    void* (*ptrFunc)(void*, void*) = &getfunction;
    // Passing the actual function pointer to another function
    myFunction(ptrfunc);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM