C ++(嵌套)函数调用的指令 - 寄存器 [英] C++ (nested) function call instructions - registers

查看:168
本文介绍了C ++(嵌套)函数调用的指令 - 寄存器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ FAQ

假设一个典型的C ++实现,具有寄存器和一个栈,
  寄存器和参数之前刚刚被写入到堆栈
  来电克(),然后将得到的参数从内克堆栈阅读()
  又一次读恢复寄存器,而克()返回到f()。

Assuming a typical C++ implementation that has registers and a stack, the registers and parameters get written to the stack just before the call to g(), then the parameters get read from the stack inside g() and read again to restore the registers while g() returns to f().

对于嵌套函数调用

void f()
{
  int x = /*...*/;
  int y = /*...*/;
  int z = /*...*/;
  ...code that uses x, y and z...
  g(x, y, z);
  ...more code that uses x, y and z...
}

1 /与寄存器的C ++实现所有和堆栈?这是否意味着:实现依赖于编译器/处理器/计算机体系结构?

1/ Are all implementation of C++ with registers and stack? Does it mean: implementation dependent on compiler/processor/computer architecture?

2 /什么是指令(没有汇编语言,只是大图)序列时我称之为 F()?我对这个话题阅读发散的事情,也是我不记得在哪里登记mentionned,但只有叠加。

2/ What is sequence of instructions (without assembly language, just the big picture) when i call f() ? I have read diverging things on this topic, and also I don't remember that registers where mentionned, but only stack.

3 /什么是额外特异性/分的时候你处理嵌套函数强调?

3/ what are additional specificities/points to underline when you deal with nested functions?

感谢

推荐答案

有关编号 2 这取决于很多事情,包括编译器和平台。基本上传递和返回参数的功能,以不同的方式被称为调用约定的。文章在x86平台上调用约定进入的一些细节操作顺序,你可以看到如何丑陋和复杂它与平台和编译器这是最有可能只是这个小组合得到,为什么你听到各种不同的情况,的的根在函数调用约定。涵盖了更广泛的方案,包括 64位的平台,但难以阅读。它会变得更加复杂,因为 GCC 实际上可能没有弹出堆栈,但直接操作堆栈指针,我们可以看到这样一个例子,尽管在装配的此处。这是很难概括调用约定,如果参数的数量是足够小很多调用约定,可避免使用所有并使用寄存器完全

For number 2 this depends on many things including the compiler and the platform. Basically the different ways of passing and returning arguments to functions are called calling conventions. the article Calling conventions on the x86 platform goes into some detail on sequence of operations and you can see how ugly and complicated it gets with just this small combination of platforms and compilers which is most likely why you have heard all sorts of different scenarios, The gen on function calling conventions. covers a wider set of scenarios including 64 bit platforms but is harder to read. It gets even more complicated because gcc may not actually push and pop the stack but directly manipulate the stack pointer, we can see an example of this, albeit in assembly here. It is hard to generalize about calling conventions, if the number of arguments is small enough many calling conventions can avoid using the stack at all and will use registers exclusively.

至于数 3 ,嵌套函数不会改变任何东西,它只是重复上述步骤重新为下一个函数调用。

As to number 3, nested functions does not change anything, it will just repeat the procedure over again for the next function call.

至于数 1 肖恩指出编译成字节code。与执行它所有的栈上的操作。在公共中间语言有一个很好的例子。

As to number 1 As Sean pointed out .Net compiles to byte code with performs all it's operations on the stack. The Wikipedia page on Common Intermediate Language has a good example.

的x86-64 ABI 文件是,如果你想了解一个特定的调用约定具体是如何工作的另一个伟大的文档。 图3.5和3.6 整洁,因为他们给一个函数的一个很好的例子有很多参数以及每个参数使用通用寄存器组合传递浮点寄存器。看着覆盖调用约定文档时,这种漂亮的图是一个罕见的发现。

The x86-64 ABI document is another great document if you want to understand how one specific calling convention works in detail. Figure 3.5 and 3.6 are neat since they give a nice example of a function with many parameters and how each parameter is passed using a combination of general purpose registers, floating point registers and the stack. This sort of nice diagram is a rare find when looking at documents that cover calling conventions.

这篇关于C ++(嵌套)函数调用的指令 - 寄存器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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