/*******************024 函数的值调用*********************
* C语言精彩编程百例第24*/
#include<stdio.h>
int square(int x);
int cube(int y);
int main(void)
{
int m=12;
int n=4;
printf("%d %d\n",square(m),m);
printf("%d %d\n",cube(n),n);
return 0;
}
int square(int x)
{
x=x*x;
return x;
}
int cube(int y)
{
y=y*y*y;
return y;
}
.file "024.c"
.def ___main; .scl 2; .type 32; .endef
.text
LC0:
.ascii "%d %d\12\0"
.align 2
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
andl $-16, %esp
movl $0, %eax
movl %eax, -12(%ebp)
movl -12(%ebp), %eax
call __alloca
call ___main
movl $12, -4(%ebp) # m=12
movl $4, -8(%ebp) # n=4
subl $4, %esp # 为调用printf做堆栈调整
pushl -4(%ebp) # m作为printf的参数入栈
subl $4, %esp # 为调用square做堆栈调整
pushl -4(%ebp) # m作为的square参数入栈
call _square # 调用函数
addl $8, %esp # 平衡堆栈
pushl %eax # 函数返回结果入栈
pushl $LC0 # "%d %d\n"
call _printf # 调用printf
addl $16, %esp # 平衡堆栈
subl $4, %esp # 为调用printf做堆栈调整
pushl -8(%ebp) # n作为printf的参数入栈
subl $4, %esp # 为调用cube做堆栈调整
pushl -8(%ebp) # n作为cube的参数入栈
call _cube # 调用cube
addl $8, %esp # 平衡堆栈
pushl %eax # 函数返回结果入栈
pushl $LC0 # "%d %d\n"
call _printf # 调用printf
addl $16, %esp # 平衡堆栈
movl $0, %eax # main函数返回值设定
leave # 释放程序堆栈
ret
.align 2
.globl _square
.def _square; .scl 2; .type 32; .endef
_square:
pushl %ebp # 保存ebp
movl %esp, %ebp # ebp=esp
movl 8(%ebp), %eax # 8(%ebp)是调用_square时push进来的最后一个参数,4(%ebp)是call指令保存的IP
imull 8(%ebp), %eax # eax=x*x
movl %eax, 8(%ebp) # 8(%ebp)=x*x
movl 8(%ebp), %eax # eax=8(%ebp)
popl %ebp
ret # pop IP
.align 2
.globl _cube
.def _cube; .scl 2; .type 32; .endef
_cube:
pushl %ebp # 保存ebp
movl %esp, %ebp # ebp=esp
movl 8(%ebp), %eax # eax=x
movl %eax, %edx # edx =x
imull 8(%ebp), %edx # edx=x*x
movl 8(%ebp), %eax # eax = x
imull %edx, %eax # eax =x*x*x
movl %eax, 8(%ebp) # x=x*x*x
movl 8(%ebp), %eax # eax=x 为函数准备返回值
popl %ebp # 恢复ebp
ret # 恢复IP
.def _cube; .scl 2; .type 32; .endef
.def _square; .scl 2; .type 32; .endef
.def _printf; .scl 2; .type 32; .endef