从内联汇编调用参数时如何将参数传递给C ++函数 [英] How do I pass arguments to C++ functions when I call them from inline assembly

查看:261
本文介绍了从内联汇编调用参数时如何将参数传递给C ++函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我希望能够从c ++ dll调用函数。
由于某些原因,我想从C ++代码中的__asm块调用它们。
我的问题是这样的:我知道在调用函数之前,必须按照函数调用约定指定的顺序将其参数推入堆栈,但是我可以简单地执行以下操作吗:

So, I would like to be able to call functions from a c++ dll. For certain reasons, I would like to call them from an __asm block in my C++ code. My question is this: I know that before I call the function, I have to push its arguments on the stack in the order specified by the function's calling convention.However, can i simply do something like this:

int a=5;   
double b = 5.0;  
__asm{  
       push b 
       push a  
       call functionAddress  
}

让我担心的事实是,我似乎还记得汇编语言中标准字的大小是2个字节,而C ++中int的大小通常是4个字节,而double则是8个字节。在上面的示例中,我真的要推送每个变量的完整值,还是仅仅是前几个字节?如果上面的代码不正确,正确的方法是什么?另外,如果我们正在调用的函数返回一个double,则此值存储在哪里?我假设它不能在寄存器中,因为它只能存储32bits(4bytes)。对此混乱的任何帮助将不胜感激:)

What worries me is the fact that I seem to remember that the standard word size in assembly is 2bytes, while the size of an int in C++ is usually 4bytes, and 8 bytes for a double.So , in the example above, am I really pushing the full value of each variable, or just the first couple of bytes? If the code above is not correct, what would be the right way to do it? Also, if the function we are calling returns a double, where is this value stored? I'm assuming it can't be in a register, because it can only store 32bits ( 4bytes ).Any help with this mess would be greatly appreciated :)

推荐答案

要推送8字节值(例如double),您将无法使用常规的 PUSH 指令。您也不会将浮点参数(或双精度)压入浮点堆栈。您需要手工将这些胖参数放在堆栈上。例如,将π作为函数f的参数:

To push 8-byte values such as doubles, you won't be able to use a regular PUSH instruction. And neither do you push floating-point parameters (or doubles) on to the floating-point stack. You need to put these fat parameters on the stack 'by hand'. For example, to push π as a parameter to a function f:

  __asm {
    FLDPI                    // load pi onto FP stack
    SUB ESP,8                // make room for double on processor stack
    FSTP QWORD PTR [ESP]     // store pi in proc stack slot (and pop from FP stack)
    CALL f
    ADD ESP,8                // clean up stack (assuming f is _cdecl)
  }

这篇关于从内联汇编调用参数时如何将参数传递给C ++函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆