C数组的反汇编产生了奇怪的结果


int square() {
char test[50];
}

上述代码产生

square():
push    rbp
mov     rbp, rsp

当我把代码稍微改成时

int square() {
char test[150];
}

生成的程序集是

square():
push    rbp
mov     rbp, rsp
sub     rsp, 40

这仍然很奇怪,因为我不明白为什么它没有分配给以前的创作。我以-O0运行,所以gcc不会对其进行优化。为什么gcc为错误大小的数组创建代码?

int square() {
char a[50];
char b[50];
}
square():
push    rbp
mov     rbp, rsp
sub     rsp, 8

与x86 类似

int square() {
char a[500];
}

用-m32编译得到:

square():
push    ebp
mov     ebp, esp
sub     esp, 512

这个额外的12字节是从哪里来的?为什么-m32有一个用于字符测试[50]的子指令,而x86_64没有?

GCC在变量的堆栈指针下方使用x86-64 System V ABI的128字节红区,仅在不足时保留一些额外的堆栈空间

对于最后一个示例,GCCsub使用512来保持堆栈(和数组(对齐。

i386 System V ABI没有红色区域,因此必须为整个阵列保留空间(Windows x64也没有(。

最新更新