从汇编代码里看的应该清楚
#include
#include
struct aa {
int b;
int c;
};
struct aa fun(){
struct aa b;
return b;
}
void fun2(){
struct aa c = fun();
}
int main(){
fun2();
}
gcc -S后
.file "returnstruct.c"
.text
.globl fun
.type fun, @function
fun:
pushl %ebp
movl %esp, %ebp
subl $16, %esp
movl 8(%ebp), %ecx
movl -8(%ebp), %eax
movl -4(%ebp), %edx
movl %eax, (%ecx)
movl %edx, 4(%ecx)
movl %ecx, %eax
leave
ret $4
.size fun, .-fun
.globl fun2
.type fun2, @function
fun2:
pushl %ebp
movl %esp, %ebp
subl $28, %esp
leal -24(%ebp), %eax
movl %eax, (%esp)
call fun
subl $4, %esp
movl -24(%ebp), %eax
movl -20(%ebp), %edx
movl %eax, -8(%ebp)
movl %edx, -4(%ebp)
leave
ret
.size fun2, .-fun2
.globl main
.type main, @function
main:
leal 4(%esp), %ecx
andl $-16, %esp
pushl -4(%ecx)
pushl %ebp
movl %esp, %ebp
pushl %ecx
subl $4, %esp
call fun2
addl $4, %esp
popl %ecx
popl %ebp
leal -4(%ecx), %esp
ret
.size main, .-main
.ident "GCC: (GNU) 4.1.2 20070925 (Red Hat 4.1.2-33)"
.section .note.GNU-stack,"",@progbits
注意看fun函数里的几句代理:
movl 8(%ebp), %ecx
movl -8(%ebp), %eax
movl -4(%ebp), %edx
movl %eax, (%ecx)
movl %edx, 4(%ecx)
movl %ecx, %eax
以及 fun2的代码
可以很明显的看出这个简单的结构体的返回利用了堆栈
阅读(1466) | 评论(0) | 转发(0) |