简体   繁体   English

将汇编代码移植到C ++

[英]Porting assembly code to c++

I am porting assembly code to C++ and I have some problems porting a portion of it. 我正在将汇编代码移植到C ++,并且在移植一部分代码时遇到一些问题。

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++: 我有一个内联汇编代码,必须移植到C ++:

__asm { call function }

How to call this pointer in C++? 如何在C ++中调用此指针?

Please note that the arguments lib and fname are of type void* . 请注意,参数libfname的类型为void*

And void* function points to value returned by getfunction() . void* function指向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. __asm块不会将参数压入堆栈。 Therefore the parameter list is empty. 因此,参数列表为空。 And the __asm block does not extract a return value. 并且__asm块不会提取返回值。 So the function has void return type. 因此该函数具有void返回类型。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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