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