19

刚开始学习 x64 汇编,我有一个关于函数、参数和堆栈的问题。据我了解,函数中的前四个参数被传递给 Windows 中的 rcx、rdx、r8 和 r9 寄存器(以及用于浮点数的 xmm0-xmm3)。所以一个有四个参数的简单的加法函数看起来像这样:

add:
   mov r10, rcx
   add r10, rdx
   add r10, r8
   add r10, r9
   mov rax, r10
   ret

但是,我遇到了提到这一点的文档

每个函数至少必须在堆栈上保留 32 个字节(四个 64 位值)。这个空间允许传递给函数的寄存器很容易被复制到一个众所周知的堆栈位置。被调用函数不需要将输入寄存器参数溢出到堆栈,但堆栈空间保留确保它可以在需要时进行。

那么,即使我正在制作的函数采用四个或更少的参数,我是否必须保留堆栈空间,或者这只是一个建议?

4

2 回答 2

14

您的报价来自文档的“调用约定”部分。至少,如果您不从汇编代码中调用其他函数,您不必担心这一点。如果你这样做,那么你必须尊重,除其他外,“红色区域”和堆栈对齐注意事项,你引用的建议旨在确保。

编辑:这篇文章阐明了“红色区域”和“阴影空间”之间的区别。

于 2011-09-12T18:55:04.500 回答
2

在玩了更多并阅读了文档之后,需要为您调用的任何函数保留 32 个字节。如果你的函数和示例一样简单,并且不调用其他函数,则不必保留此空间。但是,您调用的任何函数都可能使用这 32 个字节,因此如果您不保留它们,该函数可能

此外,如果它遵循 ABI,您的函数可能依赖于调用您的函数的堆栈中有 32 个字节可用。通常这 32 字节区域用于保存将在您的函数中更改的寄存器,以便您可以在返回之前恢复它们的值。我认为是出于性能目的,选择 32 字节就足够了,因此大多数叶函数(不调用其他函数的函数)不需要保留任何堆栈空间,并且在堆栈上有临时空间来保存寄存器并在返回之前恢复它们。举个例子:

调用函数:

CallingFunction:
  push rbp
  mov rbp, rsp
  sub rsp, 40  // $20 bytes we want to use at [rbp+30],
               // plus $20 bytes for calling other functions
               // according to windows ABI spec
  mov rcx, [rsi+10]     // parameter 1 (xmm0 if non-int)
  mov rdx, 10           // parameter 2 (xmm1 if non-int)
  movss xmm2, [rsi+28]  // parameter 3 (r8 if int)
  mov r9, [rsi+64]      // parameter 4 (xmm3 if non-int)
  call MyFunction
  // ... do other stuff
  add rsp, 40           // free space we reserved
  pop rbp
  xor rax,rax
  ret

被调用函数

CalledFunction:
  push rbp      // standard
  mov rbp, rsp  // standard

  // should do 'sub rsp, 20' here if calling any functions
  // to give them a free scratch area

  // [rbp] is saved rbp
  // [rbp+8] is return address
  // [rbp+10] to [rbp+2f] are the 0x20 bytes we can
  //     safely modify in this function, this could
  //     be pushed higher if the function had more than 4
  //     parameters and some had to be passed on the stack
  //     or if returning a structure or something that needs
  //     more space.  In these cases the CALLER would have
  //     allocated more space for us

  // the main reason for the 0x20 is so that we can save 
  // registers we want to modify without having to allocate
  // stack space ourselves
  mov [rbp+10], rsi // save rsi in space allocated by caller
  mov [rbp+18], rdi // save rdi in space allocated by caller
  mov rsi, [rcx+20]
  mov rdi, [rsi+48]
  add rdi, [rsi+28]
  mov rax, rdi
  mov rdi, [rbp+18] // restore changed register
  mov rsi, [rbp+10] // restore changed register
  pop rbp
  ret

原始答案

我只是遇到了这个不知道的情况,似乎是这样。例如,GetAsyncKeyState 中的前两条指令会覆盖 0x20 字节区域中返回地址上方的堆栈,您应该为被调用者保留用于参数的区域:

user32.GetAsyncKeyState  - mov [rsp+08],rbx
user32.GetAsyncKeyState+5- mov [rsp+10],rsi
user32.GetAsyncKeyState+A- push rdi
user32.GetAsyncKeyState+B- sub rsp,20
于 2015-03-27T22:59:04.717 回答