函数返回值的存放地址
(2014-05-30 22:21:13)
标签:
返回值eaxraxstruct栈it |
分类: C |
from http://nxlhero.blog.51cto.com/962631/703953
$ make test
cc test.c
-o test
$ ./test
$ echo $?
100
函数f把返回值放到eax了,main函数什么都没做,所以返回值还是100。
$make haha
cc haha.c
-o haha
$ ./haha
Segmentation fault (core dumped)
$ echo $?
139
-
//示例1:返回值为char
-
-
char f()
{
char a = 'a';
return a;
}
int main()
{
char b = f();
return 0;
}
-
- .file "char.c"
- .text
- .globl f
- f:
- pushl �p
- movl %esp, �p
- subl $16, %esp
-
movb $97, -1(�p)
//我的显示的为 movb $0x61,-0x1(%rbp) with objdump or the same with gcc -S - movsbl -1(�p),�x //符号扩展,我自个的显示为movzbl -0x1(%rbp),�x
- leave
- ret
- .globl main
- main:
- leal 4(%esp), �x
-
andl $-16, %esp
- pushl -4(�x)
- pushl �p
- movl %esp, �p
- pushl �x
-
subl $16, %esp
// sub $0x10,%rsp or subq $16,%rsp - call f
- movb %al, -5(�p) //movb %al, -1(%rbp)
- movl $0, �x
- addl $16, %esp
- popl �x
- popl �p
- leal -4(�x), %esp
- ret
-
-
-
long
long f() -
{
-
longlong a = 5; -
returna; -
}
-
int
main() -
{
-
longlong b; -
b=f(); -
return0; -
}
-
-
"longint.c".file -
.text -
.globl
f -
f:
-
pushl �p -
movl %esp, �p -
subl $16, %esp -
movl $5, -8(�p) -
movl $0, -4(�p) -
movl -8(�p), �x -
movl -4(�p), �x -
leave -
ret -
.globl
main -
main:
-
leal 4(%esp), �x -
andl $-16, %esp -
pushl -4(�x) -
pushl �p -
movl %esp, �p -
pushl �x -
subl $20, %esp -
call f -
movl �x, -16(�p) -
movl �x, -12(�p) -
movl $0, �x -
addl $20, %esp -
popl �x -
popl �p -
leal -4(�x), %esp -
ret
-
-
float
f() -
{
-
return 0.1; -
}
-
int
main() -
{
-
afloat = f(); -
return 0; -
}
-
-
.file "float.c" -
.text -
.globl
f -
f:
-
pushl �p -
movl %esp, �p -
subl $4, %esp -
movl $0x3dcccccd, �x -
movl �x, -4(�p) -
flds -4(�p) //把结果压到浮点寄存器栈顶 //我的使用的是movss -0x4(%rbp),%xmm0 -
leave -
ret -
.globl
main -
main:
-
leal 4(%esp), �x -
andl $-16, %esp -
pushl -4(�x) -
pushl �p -
movl %esp, �p -
pushl �x -
subl $16, %esp -
call f -
//我的使用的是movssfstps -8(�p) //从浮点寄存器栈顶取数 %xmm0,-0x4(%rbp) -
movl $0, �x -
addl $16, %esp -
popl �x -
popl �p -
leal -4(�x), %esp -
ret
-
-
-
int
f() -
{
-
return5; -
}
-
int
(*whatisthis()) () //这个函数的返回类型是函数指针 -
{
-
returnf; -
}
-
int
main() -
{
-
int(*a) (); -
intb; -
a = whatisthis(); -
b = a(); -
"%d\n",b);printf( -
return0; -
}
-
-
"ret_fun.c".file -
.text -
.globl
f -
f:
-
pushl �p -
movl %esp, �p -
movl $5, �x -
popl �p -
ret -
-
.globl
whatisthis -
whatisthis:
-
pushl �p -
movl %esp, �p -
movl $f, �x -
popl �p -
ret -
-
.LC0:
-
"%d\n".string -
.text -
-
.globl
main -
main:
-
leal 4(%esp), �x -
andl $-16, %esp -
pushl -4(�x) -
pushl �p -
movl %esp, �p -
pushl �x -
subl $36, %esp -
call whatisthis -
movl �x, -12(�p) //我的显示为movq %rax,-16(%rbp) -
movl -12(�p), �x //我的显示movq -16(%rbp),%rdx -
call *�x //我的显示 call *�x -
movl �x, -8(�p) //把返回结果5 放入�x,从而main函数从�x读取返回值5到-8(%rbp) -
movl -8(�p), �x -
movl �x, 4(%esp) -
movl $.LC0, (%esp) -
call printf -
movl $0, �x -
addl $36, %esp -
popl �x -
popl �p -
leal -4(�x), %esp -
ret
-
-
-
struct
xxx{ -
chara; -
};
-
struct
xxx f() -
{
-
structxxx x; -
'9';x.a = -
returnx; -
}
-
int
main() -
{
-
structxxx y = f(); -
return0; -
}
-
-
"struct_char.c".file -
.text -
.globl
f -
f:
-
pushl �p -
movl %esp, �p -
subl $16, %esp -
movl8(�p), �x //取出地址,放入edx -
movb $57, -1(�p) // movb $0x39, -0x1(%rbp) -
movzbl-1(�p), �x //'9'放到 al -
movb%al, (�x) //将al内容写到edx指向的地址 我的无此步骤 -
movl �x, �x -
leave -
ret $4 -
-
.globl
main -
main:
-
leal 4(%esp), �x -
andl $-16, %esp -
pushl -4(�x) -
pushl �p -
movl %esp, �p -
pushl �x -
subl $24, %esp -
leal-21(�p), �x //地址放到eax -
movl�x, (%esp) //地址压入栈中 -
call f -
subl$4, %esp //没有取返回值的指令了 -
//因为已经写到目的地址了movzbl -21(�p), �x 我的main中使用mov %al, -0x1(%rbp)来取得地址 -
movb %al, -5(�p) -
movl $0, �x -
movl -4(�p), �x -
leave -
leal -4(�x), %esp -
ret
-
-
-
struct
xxx { -
chara[10]; -
};
-
struct
xxx intf( a) -
{
-
structxxx t; -
t.a[9] = 1; -
returnt; -
}
-
int
main() -
{
-
structxxx m=f(1); -
return0; -
}
-
-
"struct.c".file -
.text -
.globl
f -
f:
-
pushl �p -
movl %esp, �p -
subl $16, %esp -
movl 8(�p), �x //取地址 我的显示的是是movl �i, -36(%rbp) 读取传入参数,并复制到栈内 -
movb $1, -1(�p) -
movl -10(�p), �x -
movl �x, (�x) -
movl -6(�p), �x -
movl �x, 4(�x) -
movzwl -2(�p), �x -
movw %ax, 8(�x) -
movl �x, �x -
leave -
ret $4 -
-
.globl
main -
main:
-
leal 4(%esp), �x -
andl $-16, %esp -
pushl -4(�x) -
pushl �p -
movl %esp, �p -
pushl �x -
subl $24, %esp -
leal -14(�p), �x -
movl $1, 4(%esp) //先压入参数 我的显示 movl $1,�i -
movl �x, (%esp) //再压入返回值地址 我的显示没有此项 -
call f -
subl $4, %esp -
movl $0, �x -
movl -4(�p), �x -
leave -
leal -4(�x), %esp -
ret
-
struct
xxx { -
chara[10]; -
};
-
struct
xxx intf( a) -
{
-
structxxx t; -
t.a[9] = 1; -
returnt; -
}
-
int
main() -
{
-
f(1); -
return0; -
}
-
int
main() -
{
-
inta; -
longlong a1; -
doublea2; -
inta = { b "xxx\n");;};= 5; printf( -
inta1 = { b int= 5; c = 2; 3-4;b-c;}; -
inta2 = { b int= 5; c = 2; 10-8;}; -
"%d\n",a);printf( -
"%ld\n",a1);printf( -
"%lf\n",a2);printf( -
return0; -
}
前一篇:gdb查看内存地址和栈中的值

加载中…