简体   繁体   English

为什么VS2013将一个函数调用编译成两个指令而不是一个?

[英]Why does VS2013 compile a function-call into two instructions instead of one?

Here is a simple program: 这是一个简单的程序:

void func()
{
    printf("hello");
}

int main()
{
    printf("%p",func);
    func();
    return 0;
}

Stepping over the line printf("%p",func) , I get 00F811AE printed on the console. 跨过printf("%p",func) ,我在控制台上打印了00F811AE

Disassembling the line func() , gives me call _func (0F811AEh) - so far so good. 反汇编func() ,给我call _func (0F811AEh) -到目前为止很好。

But disassembling the contents of func , the first instruction appears at address 00F813C0 . 但是反汇编func的内容,第一条指令出现在地址00F813C0

So I "went to see" what's on address 00F811AE , and there I found jmp func (0F813C0h) . 因此,我“去看看”地址00F811AE ,然后在其中找到了jmp func (0F813C0h)

To summarize this, it appears that the function-call is compiled as two instructions: 概括起来,似乎函数调用被编译为两条指令:

call _func (0F811AEh)
jmp   func (0F813C0h)

Why does the VS2013 compiler use two instructions instead of just one? 为什么VS2013编译器使用两条指令而不是仅一条指令?

It appears that a single jmp would do the the job. 看来一个jmp就能完成这项工作。 I am asking even this because I have a feeling that the other compilers do it in a similar manner (depending on the underlying HW architecture of course). 我之所以这样问,是因为我感觉其他编译器也以类似的方式进行此操作(当然,这取决于底层的硬件架构)。

Thanks 谢谢

Learn about "thunking": http://en.wikipedia.org/wiki/Thunk 了解有关“ thunking”的信息: http : //en.wikipedia.org/wiki/Thunk

One benefit with "thunking" in your example is that the rest of your code will always call func , but any function performing the same role could be injected into the call at address 0x00F811AE. 在您的示例中,“ thunking”的一个好处是,其余代码将始终调用func ,但是任何执行相同角色的函数都可以注入到地址为0x00F811AE的调用中。

Try making func a static one and find out if anything changes. 尝试将func设为 静态,并找出是否有任何变化。

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

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