gpt4 book ai didi

assembly - 为什么IA-32具有非直观的主叫方和被叫方注册保存约定?

转载 作者:行者123 更新时间:2023-12-03 13:43:57 25 4
gpt4 key购买 nike

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

• 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/ popa保存并恢复所有内容?

最佳答案

为什么要编写代码以将寄存器保存在您可能不需要的每个函数中?这将为每个单个函数调用添加额外的代码和额外的内存写入。现在看来似乎并不重要,但是在80年代创建此约定时,它可能确实很重要。

并请注意,ia-32没有固定的调用约定-您列出的只是外部约定-ia-32不会强制执行。如果您要编写自己的代码,则可以根据需要使用寄存器。

另请参阅“旧新事物博客”上的讨论History of Calling Conventions

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.

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).

关于assembly - 为什么IA-32具有非直观的主叫方和被叫方注册保存约定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8335582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com