编写AMD64 SysV程序集时,哪个寄存器可用作临时寄存器? [英] Which registers to use as temporaries when writing AMD64 SysV assembly?

查看:136
本文介绍了编写AMD64 SysV程序集时,哪个寄存器可用作临时寄存器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在根据AMD64 SysV ABI在组装中使用cpuid实现功能.我需要在函数本身中使用2个临时寄存器:第一个用于累加返回值的寄存器,第二个作为计数器.

I'm implementing a function using cpuid in assembly according to AMD64 SysV ABI. I need 2 temporary registers to be used in the function itself: the first one to accumulate return value and the second one as a counter.

我的函数当前显示为:

;zero argument function
some_cpuid_fun:
  push rbx

  xor r10d, r10d ;counter initial value
  xor r11d, r11d ;return value accumulator initial value
  some_cpuid_fun_loop:
  ;...
  cpuid
  ;update r10d and r11d according to the result


  mov eax, r11d
  pop rbx
  ret

由于cpuid Clobbers eaxebxecxedx我无法在不同的cpuid执行中使用它们.如 AMD64 SysV ABI :

Since cpuid clobbers eax, ebx, ecx, edx I cannot use them across different cpuid executions. As documented in the AMD64 SysV ABI:

r10    temporary register, used for passing a function’s
       static chain pointer

r11    temporary register

只有一个严格的临时寄存器r11r10似乎有不同的用途(并且它作为循环计数器的用途不是一个,我显然没有传递任何静态链指针).

There is only one strictly temporary register r11, r10 seems to have a different purpose (and its usage as a loop counter is not one, I obviously did not pass any static chain pointers).

问题: some_cpuid_fun函数实现AMD64 SysV ABI是否兼容?如果没有,该如何重写以保持与ABI的兼容性?

QUESTION: Is the some_cpuid_fun function implementation AMD64 SysV ABI compatible? If no how to rewrite it to stay compatible with the ABI?

推荐答案

在确定了用于参数的所有寄存器之后,在这种情况下,没有一个要注意的是,寄存器是否是易失性的(不在所有寄存器中保留)电话).

After you have identified all the registers used for the arguments, none in this case, all you have to care about is if a register is volatile (not preserved across calls) or not.

简而言之,请看图3.4的最后一列(r10的用法来自哪里):未保存,因此您可以使用它而无需恢复它.
用法列仅告诉您在期望的地方寻找参数以及在何处放置返回值.
如果您在输入中未使用静态链指针,则可以在需要时立即覆盖r10.

In short, looks at the last column of the Figure 3.4 (where the usage for r10 come from): It is not preserved, so you can use it without restoring it.
The Usage column only tells you where to look for the arguments if expected and where to put the return value.
If you don't take a static chain pointer in input, you can overwrite r10 as soon as needed.

是的,使用r10作为临时寄存器是ABI兼容的.

So yes, using r10 as a temporary register is ABI compatible.

供参考:

本小节讨论每个寄存器的用法.注册%rbp,%rbx和 %r12至%r15属于"调用函数,并且被调用函数为 保持其价值所必需的.换句话说,一个被调用的函数必须保留 这些寄存器为其调用者提供值.其余寄存器属于"被叫 功能. 5如果调用函数想要在一个寄存器中保留这样的寄存器值 函数调用时,必须将值保存在其本地堆栈帧中.

This subsection discusses usage of each register. Registers %rbp, %rbx and %r12 through %r15 "belong" to the calling function and the called function is required to preserve their values. In other words, a called function must preserve these registers’ values for its caller. Remaining registers "belong" to the called function. 5 If a calling function wants to preserve such a register value across a function call, it must save the value in its local stack frame.

这篇关于编写AMD64 SysV程序集时,哪个寄存器可用作临时寄存器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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