IM只是好奇下面的例子C - 分号的返回值是什么?
#include<stdio.h>
int test();
int test(){
// int a = 5;
// int b = a+1;
return ;
}
int main(){
printf("%u\n",test());
return 0;
}
我与“GCC -Wall -o分号semicolon.c”编译它来创建可执行 和“GCC -Wall -S semicolon.c”,让汇编代码是:
.file "semicolon.c"
.text
.globl test
.type test, @function
test:
pushl %ebp
movl %esp, %ebp
subl $4, %esp
leave
ret
.size test, .-test
.section .rodata
.LC0:
.string "%u\n"
.text
.globl main
.type main, @function
main:
leal 4(%esp), %ecx
andl $-16, %esp
pushl -4(%ecx)
pushl %ebp
movl %esp, %ebp
pushl %ecx
subl $20, %esp
call test
movl %eax, 4(%esp)
movl $.LC0, (%esp)
call printf
movl $0, %eax
addl $20, %esp
popl %ecx
popl %ebp
leal -4(%ecx), %esp
ret
.size main, .-main
.ident "GCC: (Ubuntu 4.3.3-5ubuntu4) 4.3.3"
.section .note.GNU-stack,"",@progbits
,因为我不是这样的汇编亲,我只知道printf的打印什么是EAX ,但我不完全理解“MOVL%EAX,4(%ESP)”的意思是我假设在调用测试 之前填充eax,但那么值是多少?什么意思是4(%esp),esp的意思是什么?
如果我取消测试()行的printf打印6 - 这是写在EAX ^^
很好的解释。添加一些关于这个主题的更多信息,似乎eax及其在通过堆栈传递值时的用法与简单数据类型(int,double,char,void *,...)类似,但是当它进入更多的结构时编译器的内存管理发挥作用。即时通讯还不确定在堆栈上发生什么情况,以便理解空函数返回结构的空函数,但这是一个更复杂的问题/答案。无论如何 - thx – 2010-01-15 12:17:42
@John:几乎同样的事情发生在像'int'和'float'一样的结构中。它被称为_stack垃圾_。无论内存中调用者为返回值保留的是什么,返回_returned_。 – 2010-01-15 12:52:58