为什么IA-32有一个非直观呼叫者和被呼叫者寄存器保存惯例? [英] Why does IA-32 have a non-intuitive caller and callee register saving convention?

查看:190
本文介绍了为什么IA-32有一个非直观呼叫者和被呼叫者寄存器保存惯例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关IA-32的通用的调用约定说:

The common calling conventions for IA-32 say:

• Callee-save registers
%ebx, %esi, %edi, %ebp, %esp
Callee must not change these.  (Or restore the caller's values before returning.)

• Caller-save registers
%eax, %edx, %ecx, condition flags
Caller saves these if it wants to preserve them.  Callee can freely clobber.

为什么会出现这种奇怪的约定存在吗?为什么不能调用另一个函数之前所有的寄存器保存?或者被调用者保存和 PUSHA / 波帕

推荐答案

为什么要编写code来保存寄存器在每一个您可能不需要的功能?这将增加额外的code和额外的内存写入到每一个函数调用。这似乎并不显著了,但回来时本公约的创建,它可能无论做了80年代。

Why would you want to write code to save registers in every function that you might not need? That would add extra code and extra memory writes to every single function call. It may not seem significant now, but back in the 80's when this convention was created it probably did matter.

和注意,IA-32并没有一个固定的调用约定 - 你所列举的只是一个外部的公约​​ - IA-32不强制执行。如果你正在写自己的code你不过你要使用的寄存器。

And note that ia-32 doesn't have a fixed calling convention - what you list is only an external convention - ia-32 doesn't enforce it. If you're writing your own code you use the registers however you wish.

另见调用约定的讨论记录在老新事物博客。

Also see the discussion History of Calling Conventions at the Old New Thing Blog.

在决定哪些寄存器应为$ P $通过调用pserved
  按惯例,你需要平衡对调用者的需求
  被叫方的需求。主叫方将preFER所有寄存器
  preserved,因为这消除了需要调用者担心
  保存/恢复整个呼叫的价值。被叫方将preFER了
  没有寄存器是preserved,因为这消除了需要拯救
  对进入价值和出口恢复它。

When deciding which registers should be preserved by a calling convention, you need to balance the needs of the caller against the needs of the callee. The caller would prefer that all registers be preserved, since that removes the need for the caller to worry about saving/restoring the value across a call. The callee would prefer that no registers be preserved, since that removes the need to save the value on entry and restore it on exit.

如果您需要太少寄存器是preserved,然后主叫方成为
  充满寄存器保存/恢复code。但是,如果你需要太多的
  注册为preserved,那么被叫成了有义务保存和
  恢复调用者可能没有真正关心的寄存器。
  这是叶函数(即做功能尤为重要
  没有调用任何其他功能)。

If you require too few registers to be preserved, then callers become filled with register save/restore code. But if you require too many registers to be preserved, then callees become obligated to save and restore registers that the caller might not have really cared about. This is particularly important for leaf functions (functions that do not call any other functions).

这篇关于为什么IA-32有一个非直观呼叫者和被呼叫者寄存器保存惯例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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