是必要的功能少于四个参数预留堆栈空间?堆栈、必要、参数、功能

2023-09-07 22:20:49 作者:小帅气、

刚开始学习的x64汇编,我有一个关于函数,参数,堆栈的问题。据我了解,在函数的第四个参数得到传递给RCX,RDX,R8和R9寄存器(和XMM0-XMM3的花车)在Windows中。因此,与将四个参数一个微不足道的新增功能如下:

Just started learning x64 assembly and I have a question about functions, arguments, and the stack. As far as I understand it, the first four arguments in a function get passed to rcx, rdx, r8, and r9 registers (and xmm0-xmm3 for floats) in Windows. So a trivial addition function with four parameters would looks like this:

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

不过,我遇到了提到了这一点 文档:

However, I've come across documentation that mentions this:

至少,每个功能必须保留32个字节(4个64位值)在栈上。:此空间允许寄存器传递给函数可以很容易地复制到一个众所周知的栈位置。的被叫功能不需要溅出输入寄存器PARAMS到堆栈,但堆栈空间保留确保它可以在需要

At a minimum, each function must reserve 32 bytes (four 64-bit values) on the stack. This space allows registers passed into the function to be easily copied to a well-known stack location. The callee function isn't required to spill the input register params to the stack, but the stack space reservation ensures that it can if needed.

所以,我必须要保留的堆栈空间,即使我做了功能需要四个参数或更小,或者是它只是一个建议?

So, do I have to reserve stack space even if the functions I'm making take four parameters or less, or is it just a recommendation?

推荐答案

您的报价是从文档的调用约定的一部分。最起码,你不必担心这个,如果你不从你的程序集code调用等功能。如果你这样做,那么你就必须尊重,除其他事项外,红番区和栈对齐的考虑,你引用的建议的目的是确保。

Your quote is from the "calling convention" part of the documentation. At the very least, you do not have to worry about this if you do not call other functions from your assembly code. If you do, then you must respect, among other things, "red zone" and stack alignment considerations, that the recommendation you quote is intended to ensure.

编辑:这个帖子明确的区别红番区和影子空间。

this post clarifies the difference between "red zone" and "shadow space".