少于四个参数的函数是否需要保留堆栈空间? [英] Is reserving stack space necessary for functions less than four arguments?

查看:17
本文介绍了少于四个参数的函数是否需要保留堆栈空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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?

推荐答案

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

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

这篇关于少于四个参数的函数是否需要保留堆栈空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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