-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuicksort.asm
258 lines (243 loc) · 3.85 KB
/
Quicksort.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
PAGE 60,240
TITLE Quick Sort Yapar
sseg SEGMENT PARA STACK 'yigin'
DW 20 DUP(0)
sseg ENDS
dseg SEGMENT PARA 'veri'
count DB 1
dizi DB 100 DUP(0)
CR EQU 13
LF EQU 10
HT EQU 9
mesaj1 DB 'Dizi boyutunu veriniz: ',0
mesaj2 DB '.elemani giriniz: ',0
hata1 DB CR,LF,'Dikkat! Sayi girmediniz!!',CR,LF,0
hata2 DB 'Dikkat! Sayi -128 den kucuk veya 127 den buyuk olamaz!!',CR,LF,0
given_arr DB CR,LF,'Girilen Dizi:',CR,LF,0
sorted_arr DB CR,LF,'Siralanmis Dizi:',CR,LF,0
space DB ' ',0
dseg ENDS
cseg SEGMENT PARA 'kod'
ASSUME DS:dseg, SS:sseg, CS:cseg
MAIN PROC FAR
PUSH DS
XOR AX,AX
PUSH AX
MOV AX,dseg
MOV DS,AX
CALL READ_ARR
MOV AX,OFFSET given_arr
CALL PUT_STR
CALL PRINT_ARR
CALL FRST_Q_SORT
MOV AX,OFFSET sorted_arr
CALL PUT_STR
CALL PRINT_ARR
RETF
MAIN ENDP
FRST_Q_SORT PROC NEAR
XOR SI,SI
MOV DI,CX
DEC DI
CALL Q_SORT
RET
FRST_Q_SORT ENDP
Q_SORT PROC NEAR
PUSH CX
MOV DX,SI
MOV BX,DI
MOV AL,dizi[SI]
LOOP1: CMP SI,DI
JNB LOOP1_END
LOOP2: CMP dizi[DI],AL
JL LOOP2_END1
CMP SI,DI
JNB LOOP2_END2
DEC DI
JMP LOOP2
LOOP2_END1: CMP SI,DI
JE LOOP2_END2
MOV CL,dizi[DI]
MOV dizi[SI],CL
INC SI
LOOP2_END2: CMP dizi[SI],AL
JG LOOP3_END1
CMP SI,DI
JNB LOOP3_END2
INC SI
JMP LOOP2_END2
LOOP3_END1: CMP SI,DI
JE LOOP3_END2
MOV CL,dizi[SI]
MOV dizi[DI],CL
DEC DI
LOOP3_END2: JMP LOOP1
LOOP1_END: MOV dizi[SI],AL
MOV AX,SI
MOV SI,DX
MOV DI,BX
CMP SI,AX
JNB SECOND_IF
PUSH SI
PUSH DI
MOV DI,AX
DEC DI
CALL Q_SORT
POP DI
POP SI
SECOND_IF: CMP DI,AX
JNA ENDING
PUSH SI
PUSH DI
MOV SI,AX
INC SI
CALL Q_SORT
POP DI
POP SI
ENDING: POP CX
RET
Q_SORT ENDP
READ_ARR PROC NEAR
MOV AX,OFFSET mesaj1
CALL PUT_STR
CALL GETN
CBW
MOV CX,AX
PUSH CX
XOR DI,DI
ARR_LOOP: MOV AL,count
CBW
CALL PUTN
MOV AX,OFFSET mesaj2
CALL PUT_STR
CALL GETN
CMP AX,-128
JL ERROR2
CMP AX,127
JG ERROR2
INC count
MOV dizi[DI],AL
INC DI
LOOP ARR_LOOP
JMP FIN_READ_ARR
ERROR2: MOV AX,OFFSET hata2
CALL PUT_STR
JMP ARR_LOOP
FIN_READ_ARR:POP CX
RET
READ_ARR ENDP
PRINT_ARR PROC NEAR
PUSH CX
XOR DI,DI
PUT_ARR: MOV AL,dizi[DI]
CBW
CALL PUTN
MOV AX,OFFSET HT ;HT yerine space,
CALL PUTC ;PUTC yerine PUT_STR yazılırsa; elemanlar arasında horizontal tab yerine bir boşluk bırakılır.
INC DI
LOOP PUT_ARR
POP CX
RET
PRINT_ARR ENDP
GETC PROC NEAR
MOV AH,1H
INT 21H
RET
GETC ENDP
PUTC PROC NEAR
PUSH AX
PUSH DX
MOV DL,AL
MOV AH,2
INT 21H
POP DX
POP AX
RET
PUTC ENDP
GETN PROC NEAR
PUSH BX
PUSH CX
PUSH DX
GETN_START: MOV DX,1
XOR BX,BX
XOR CX,CX
NEW: CALL GETC
CMP AL,CR
JE FIN_READ
CMP AL,'-'
JNE CTRL_NUM
NEGATIVE: MOV DX,-1
JMP NEW
CTRL_NUM: CMP AL,'0'
JB ERROR
CMP AL,'9'
JA ERROR
SUB AL,'0'
MOV BL,AL
MOV AX,10
PUSH DX
MUL CX
POP DX
MOV CX,AX
ADD CX,BX
JMP NEW
ERROR: MOV AX,OFFSET hata1
CALL PUT_STR
MOV AL,count
CBW
CALL PUTN
MOV AX,OFFSET mesaj2
CALL PUT_STR
JMP GETN_START
FIN_READ: MOV AX,CX
CMP DX,1
JE FIN_GETN
NEG AX
FIN_GETN: POP DX
POP CX
POP DX
RET
GETN ENDP
PUTN PROC NEAR
PUSH CX
PUSH DX
XOR DX,DX
PUSH DX
MOV CX,10
CMP AX,0
JGE CALC_DIGITS
NEG AX
PUSH AX
MOV AL,'-'
CALL PUTC
POP AX
CALC_DIGITS:DIV CX
ADD DX,'0'
PUSH DX
XOR DX,DX
CMP AX,0
JNE CALC_DIGITS
DISP_LOOP: POP AX
CMP AX,0
JE END_DISP_LOOP
CALL PUTC
JMP DISP_LOOP
END_DISP_LOOP:POP DX
POP CX
RET
PUTN ENDP
PUT_STR PROC NEAR
PUSH BX
MOV BX,AX
MOV AL,BYTE PTR[BX]
PUT_LOOP: CMP AL,0
JE PUT_FIN
CALL PUTC
INC BX
MOV AL,BYTE PTR[BX]
JMP PUT_LOOP
PUT_FIN: POP BX
RET
PUT_STR ENDP
cseg ENDS
END MAIN