024 函数的值调用

/*******************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


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值