019 一维数组 选择法对十个整数进行排序

/***********************019 一维数组***********************
 * 使用选择法对十个整数进行排序。
 * C语言精彩编程百例第19
 */

#include<stdio.h>
void main()
{
	int i,j,min,temp;
	int array[10];

	printf("please input ten integer:\n");
	for(i=0;i<10;i++)
	{
		printf("array[%d]=",i);
		scanf("%d",&array[i]);
	}
	printf("The array is:");
	for(i=0;i<9;i++)
	{
		min=i;
		for(j=i;j<10;j++)
			if(array[min]>array[j]) min =j;
		temp=array[i];
		array[i]=array[min];
		array[min]=temp;	
	}
	printf("\nThe result:\n");
	for(i=0;i<10;i++)
		printf("%d",array[i]);
	printf("\n");
}


对应的汇编内容:

	.file	"019.c"
	.def	___main;	.scl	2;	.type	32;	.endef
	.text
LC0:
	.ascii "please input ten integer:\12\0"
LC1:
	.ascii "array[%d]=\0"
LC2:
	.ascii "%d \0"
LC3:
	.ascii "The array is:\0"
LC4:
	.ascii "\12The result:\12\0"
LC5:
	.ascii "\12\0"
	.align 2
.globl _main
	.def	_main;	.scl	2;	.type	32;	.endef
_main:
	pushl	%ebp
	movl	%esp, %ebp
	subl	$88, %esp
	andl	$-16, %esp
	movl	$0, %eax
	movl	%eax, -76(%ebp)
	movl	-76(%ebp), %eax
	call	__alloca
	call	___main
	subl	$12, %esp         # printf("please input ten integer:\n") 
	pushl	$LC0
	call	_printf
	addl	$16, %esp
	movl	$0, -12(%ebp)     # i=0
L4:
	cmpl	$9, -12(%ebp)     # i<10
	jle	L7
	jmp	L5
L7:
	subl	$8, %esp         # printf("array[%d]=",i);
	pushl	-12(%ebp)
	pushl	$LC1
	call	_printf
	addl	$16, %esp
	subl	$8, %esp          # scanf("%d",&array[i]);
	leal	-72(%ebp), %edx   # edx = &array[0] 
	movl	-12(%ebp), %eax   # eax = i
	sall	$2, %eax	  # eax= eax*4
	leal	(%eax,%edx), %eax # eax= &array[0]+i*4
	pushl	%eax              # scanf("%d",&array[i]);
	pushl	$LC2
	call	_scanf
	addl	$16, %esp
	leal	-12(%ebp), %eax   # &i
	incl	(%eax)            # i++
	jmp	L4                # 下次循环
L5:
	subl	$12, %esp         # printf("The array is:");   
	pushl	$LC3
	call	_printf
	addl	$16, %esp
	movl	$0, -12(%ebp)     # i=0
L8:
	cmpl	$8, -12(%ebp)     # i 和 9-1比较
	jle	L11               # i<9
	jmp	L9
L11:
	movl	-12(%ebp), %eax   
	movl	%eax, -20(%ebp)   # min =i
	movl	-12(%ebp), %eax
	movl	%eax, -16(%ebp)   # j =i
L12:
	cmpl	$9, -16(%ebp)     # j 和 10-1 比
	jle	L15
	jmp	L13
L15:
	movl	-20(%ebp), %eax         # eax =min
	movl	-16(%ebp), %edx         # edx =j
	movl	-72(%ebp,%eax,4), %eax  # eax =[ebp+eax*4-72]=arrar[min]
	cmpl	-72(%ebp,%edx,4), %eax  # array[min] 和 array[j] 比较
	jle	L14                     # 小于等于跳
	movl	-16(%ebp), %eax         # eax = j
	movl	%eax, -20(%ebp)         # min =j
L14:
	leal	-16(%ebp), %eax         # j++
	incl	(%eax)
	jmp	L12
L13:
	movl	-12(%ebp), %eax           # eax =i
	movl	-72(%ebp,%eax,4), %eax    # eax = array[i]
	movl	%eax, -24(%ebp)           # temp=eax
	movl	-12(%ebp), %edx           # edx=i
	movl	-20(%ebp), %eax           # eax=min
	movl	-72(%ebp,%eax,4), %eax    # eax=array[min]
	movl	%eax, -72(%ebp,%edx,4)    # array[i]=eax
	movl	-20(%ebp), %edx           # edx=min
	movl	-24(%ebp), %eax           # eax=temp
	movl	%eax, -72(%ebp,%edx,4)    # array[min]=eax
	leal	-12(%ebp), %eax           # i++
	incl	(%eax)
	jmp	L8
L9:
	subl	$12, %esp                 # printf("\nThe result:\n");
	pushl	$LC4
	call	_printf
	addl	$16, %esp
	movl	$0, -12(%ebp)             # i=0
L17:
	cmpl	$9, -12(%ebp)             # i<10
	jle	L20
	jmp	L18
L20:
	subl	$8, %esp                  # printf("%d",array[i]);
	movl	-12(%ebp), %eax
	pushl	-72(%ebp,%eax,4)
	pushl	$LC2
	call	_printf
	addl	$16, %esp
	leal	-12(%ebp), %eax          # i++
	incl	(%eax)
	jmp	L17                      # 下次循环
L18:
	subl	$12, %esp
	pushl	$LC5
	call	_printf
	addl	$16, %esp
	leave
	ret
	.def	_scanf;	.scl	2;	.type	32;	.endef
	.def	_printf;	.scl	2;	.type	32;	.endef


按照程序内容来看

 int i,j,min,temp;  的内存位置 -12(%epb)~-24(%epb)

int array[10];  的内存位置 [%ebp+0*4-72] ~ [%ebp+10*4-72]

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值