数组排序
1. 题目: 将一个数组的所有元素排序后输出
2.要求:给定一个数组,数组包含10个整型元素,将其按照从小到大的顺序排列后输出,要求排序的算法用子程序来实现。
例如,输入的数组元素为1,3,-9,5,12,0,-3,-12,24,34,那么输出是:-12,-9,-3,0,1,3,5,12,24,34。
1 ; Example assembly language program --
2 ; Author: karllen
3 ; Date: revised 5/2014
4
5 .386
6 .MODEL FLAT
7
8 ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD
9
10 INCLUDE io.h ; header file for input/output
11
12 cr EQU 0dh ; carriage return character
13 Lf EQU 0ah ; line feed
14
15 .STACK 4096 ; reserve 4096-byte stack
16
17 .DATA
18 i DWORD ?
19 j DWORD ?
20 temp DWORD ?
21 promot1 BYTE "Please Enter ten numbers to sort from min to max",cr,Lf,0
22 array DWORD 10 DUP(?)
23 promot2 BYTE "The numbers that are sorted is",cr,Lf,0
24 value BYTE 11 DUP(?)
25 BYTE cr,Lf,0
26
27 .CODE
28
29 sortArray PROC NEAR32
30 push ebp ;建立堆栈
31 mov ebp,esp
32
33 mov i,0
34 mov edx,10
35 ; mov ebx,[ebp+8] ;取得数组地址
36 sortFirst:
37 mov ebx,[ebp+8]
38 inc i
39 cmp i,9
40 jg endsortFirst ;大于9则跳转,
41
42 sub edx,i ;求edx-i
43 mov j,0
44 sortSecond:
45 inc j
46 cmp j,edx
47 jg endsortSecond ;大于10-i则转移
48 mov eax,[ebx]
49 mov ecx,[ebx+4]
50
51 cmp eax,ecx ; cmp [ebx],[ebx+4]
52 jl endCMP ;[ebx]<[ebx+4]则转移
53
54 mov edx,eax
55 mov [ebx],ecx
56 mov [ebx+4],edx
57
58
59 ;swap
60 endCMP:
61 add ebx,4
62 mov edx,10
63 jmp sortSecond
64
65 endSortSecond:
66 jmp sortFirst
67 endsortFirst:
68
69 pop ebp
70 ret
71
72 sortArray ENDP
73
74 _start:
75
76 output promot1
77 mov ecx,0
78 lea ebx,array
79
80 doFirstWhile:
81 inc ecx
82 cmp ecx,10
83 jg endFirstWhile ;大于10则结束
84
85 input value,11
86 atod value
87 mov [ebx],eax
88 add ebx,4
89 jmp doFirstWhile
90 endFirstWhile:
91
92 lea eax,array
93 push eax
94 call sortArray
95 add esp,4
96
97 output promot2
98 mov ecx,0
99 lea ebx,array
100
101 doSecondWhile:
102 inc ecx
103 cmp ecx,10
104 jg endSecondWhile
105 dtoa value,[ebx]
106 output value
107 add ebx,4
108 jmp doSecondWhile
109
110 endSecondWhile:
111
112 INVOKE ExitProcess, 0 ; exit with return code 0
113
114 PUBLIC _start ; make entry point public
115
116 END ; end of source code