013 for语句

/************************013 for语句******************************
 * C 语言编程百例的第13个例子:
 * 使用嵌套的for语句,来在屏幕上输出一个菱形,菱形由星号(*)组成,
 * 共九行九列。*/

#include <stdio.h>

void main()
{
	int i,j,k;
	//变量i从0到4,表示所画菱形图的第一至第5行
	for (i=0;i<=4;i++)
	{
		//当行数为i时,空格数是i的函数,为4-i个
		for(j=0;j<=3-i;j++)
			printf(" ");
		//星号数也是i的函数,为2i+1个
		for(k=0;k<=2*i;k++)
			printf("*");
		printf("\n");
	}
	//变量i从0到3,表示所画菱形图的第六至第九行
	for(i=0;i<=3;i++)
	{
		//当行数为i时,空格是i的函数,此时为i个
		for(j=0;j<=i;j++)
			printf(" ");
		//星号个数也是i的函数,此时为7-2i个
		for(k=0;k<=6-2*i;k++)
			printf("*");
		printf("\n");
	}
}

对应的汇编:

 

	.file	"013.c"
	.def	___main;	.scl	2;	.type	32;	.endef
	.text
LC0:
	.ascii " \0"
LC1:
	.ascii "*\0"
LC2:
	.ascii "\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, -16(%ebp)
	movl	-16(%ebp), %eax
	call	__alloca
	call	___main
	movl	$0, -4(%ebp) # 赋值 i=0
L4:
	cmpl	$4, -4(%ebp) # 判断 i和4
	jle	L7           # i<=4成立,跳
	jmp	L5           # 退出 for 循环
L7:
	movl	$0, -8(%ebp) # 赋值 j=0 
L8:
	movl	$3, %eax
	subl	-4(%ebp), %eax # eax = 3-i
	cmpl	%eax, -8(%ebp) # j 和 3-i 比较
	jle	L11            # j <= 3-i成立,跳
	jmp	L9             # 退出循环
L11:
	subl	$12, %esp      # 调用printf
	pushl	$LC0
	call	_printf
	addl	$16, %esp
	leal	-8(%ebp), %eax # eax =& j
	incl	(%eax)         # j++
	jmp	L8             # 下一次循环
L9:
	movl	$0, -12(%ebp)  # k=0
L12:
	movl	-4(%ebp), %eax # eax=i
	addl	%eax, %eax     # eax=eax*2
	cmpl	%eax, -12(%ebp)# 比较k和eax*2
	jle	L15            # k<=eax*2 跳
	jmp	L13            # 退出循环
L15:
	subl	$12, %esp      # print f
	pushl	$LC1
	call	_printf
	addl	$16, %esp
	leal	-12(%ebp), %eax # eax =&k
	incl	(%eax)          # k++
	jmp	L12
L13:
	subl	$12, %esp       # printf
	pushl	$LC2
	call	_printf
	addl	$16, %esp
	leal	-4(%ebp), %eax  # eax = &i
	incl	(%eax)          # i++
	jmp	L4
L5:
	movl	$0, -4(%ebp)    # i = 0
L16:
	cmpl	$3, -4(%ebp)    # i 和三比较
	jle	L19             # 小于等于跳
	jmp	L3              # 退出循环
L19:
	movl	$0, -8(%ebp)    # j = 0
L20:
	movl	-8(%ebp), %eax  # eax = j
	cmpl	-4(%ebp), %eax  # 比较 j 和 i
	jle	L23             # 小于等于跳
	jmp	L21             # 退出循环
L23:
	subl	$12, %esp       # printf
	pushl	$LC0
	call	_printf
	addl	$16, %esp
	leal	-8(%ebp), %eax # eax = &j
	incl	(%eax)         # j++
	jmp	L20            # 下一次循环
L21:
	movl	$0, -12(%ebp)  # k= 0
L24:
	movl	-4(%ebp), %eax # eax=i
	leal	(%eax,%eax), %edx # edx= eax *2
	movl	$6, %eax          # eax =6
	subl	%edx, %eax        # eax=eax-edx
	cmpl	%eax, -12(%ebp)   # 比较 k 和6-2*i
	jle	L27               # 小于等于跳
	jmp	L25               # 退出循环
L27:
	subl	$12, %esp         # printf
	pushl	$LC1
	call	_printf
	addl	$16, %esp
	leal	-12(%ebp), %eax   # eax =&k
	incl	(%eax)            # k++
	jmp	L24               # 下一次循环
L25:
	subl	$12, %esp         # printf 
	pushl	$LC2
	call	_printf
	addl	$16, %esp
	leal	-4(%ebp), %eax    # eax=&i
	incl	(%eax)            # i++
	jmp	L16
L3:
	leave
	ret
	.def	_printf;	.scl	2;	.type	32;	.endef



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值