【发布时间】:2026-01-05 04:00:02
【问题描述】:
我正在阅读一本教科书,其中显示了基于 C 代码的汇编代码:
C 代码:
void echo()
{
char buf[8];
otherFunction(buf);
}
汇编代码:
echo:
subq $24, %rsp //Allocate 24 bytes on stack, but why allocate 24 instead of 8 bytes?
movq %rsp, %rdi //Compute buf as %rsp
call otherFunction
我不明白为什么堆栈指针 %rsp 会减少 24 个字节。我只分配了 8 个字节的缓冲区为char buf[8];,并且没有被调用者保存的寄存器可以压入堆栈,该指令不应该是
subq $8, %rsp
【问题讨论】:
-
我认为堆栈帧大小被四舍五入到 32 字节对齐
-
@Barmar:不,gcc 只维护 16 字节对齐,这是 x86-64 ABI 所需的最小值。如果您执行了
alignas(32) buf[8],您会看到额外的代码来过度对齐堆栈。
标签: c assembly gcc x86-64 stack-memory