函数返回值的存放地址
(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() -
{
-
long a = 5; -
a; -
}
-
int
main() -
{
-
long b; -
b=f(); -
0; -
}
-
-
.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() -
{
-
float = 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 -
fstps -8(�p) //从浮点寄存器栈顶取数 %xmm0,-0x4(%rbp) -
movl $0, �x -
addl $16, %esp -
popl �x -
popl �p -
leal -4(�x), %esp -
ret
-
-
-
int
f() -
{
-
5; -
}
-
int
(*whatisthis()) () //这个函数的返回类型是函数指针 -
{
-
f; -
}
-
int
main() -
{
-
(*a) (); -
b; -
a = whatisthis(); -
b = a(); -
printf( -
0; -
}
-
-
.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:
-
.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{ -
a; -
};
-
struct
xxx f() -
{
-
xxx x; -
x.a = -
x; -
}
-
int
main() -
{
-
xxx y = f(); -
0; -
}
-
-
.file -
.text -
.globl
f -
f:
-
pushl �p -
movl %esp, �p -
subl $16, %esp -
8(�p), �x //取出地址,放入edx -
movb $57, -1(�p) // movb $0x39, -0x1(%rbp) -
-1(�p), �x //'9'放到 al -
%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 -
-21(�p), �x //地址放到eax -
�x, (%esp) //地址压入栈中 -
call f -
$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 { -
a[10]; -
};
-
struct
xxx intf( a) -
{
-
xxx t; -
t.a[9] = 1; -
t; -
}
-
int
main() -
{
-
xxx m=f(1); -
0; -
}
-
-
.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 { -
a[10]; -
};
-
struct
xxx intf( a) -
{
-
xxx t; -
t.a[9] = 1; -
t; -
}
-
int
main() -
{
-
f(1); -
0; -
}
-
int
main() -
{
-
a; -
long a1; -
a2; -
a = { b "xxx\n");;};= 5; printf( -
a1 = { b int= 5; c = 2; 3-4;b-c;}; -
a2 = { b int= 5; c = 2; 10-8;}; -
printf( -
printf( -
printf( -
0; -
}
前一篇:gdb查看内存地址和栈中的值