6

在 GCCcdecl调用约定中,我可以依靠我压入堆栈的参数在调用返回后保持不变吗?即使在混合 ASM 和 C 并-O2启用优化 ( ) 时?

4

1 回答 1

6

一句话:没有。

考虑这段代码:

__cdecl int foo(int a, int b)
{
   a = 5;
   b = 6;
   return a + b;
}

int main()
{
   return foo(1, 2);
}

这产生了这个 asm 输出(使用 -O0 编译):

movl    $5, 8(%ebp)
movl    $6, 12(%ebp)
movl    8(%ebp), %edx
movl    12(%ebp), %eax
addl    %edx, %eax
popl    %ebp
ret

因此,__cdecl 函数很有可能踩到堆栈值。

这甚至还没有计算内联或其他优化魔法的可能性,其中事情可能不会首先出现在堆栈上。

于 2016-08-09T07:15:18.053 回答