-
Notifications
You must be signed in to change notification settings - Fork 0
/
sufsort.asm
300 lines (232 loc) · 7.47 KB
/
sufsort.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
%include "asm_io.inc"
SECTION .data
err1: db "incorrect number of command line arguments,", 0x0a,"must have one argument after sufsort",10,0
err2: db "incorrect command line argument, must contain only 0's, 1's and 2's", 0x0a,"and must be between 1 and 30 (inclusive) characters ",10,0
disp: db "sorted suffixes:",10,0
SECTION .bss
N: resd 1
X: resb 30
i: resd 1
j: resd 1
n: resd 1
m: resd 1
k: resd 1
o: resd 1
y: resd 30
i_sufcmp: resd 1
j_sufcmp: resd 1
temp: resd 1
SECTION .text
global asm_main
asm_main:
enter 0,0 ; setup routine
pusha ; save all registers
mov [N], dword 0
mov eax, dword [ebp+8] ; argc
cmp eax, dword 2 ; argc should be 2
jne ERR1
; so we have the right number of arguments
mov eax, dword [ebp+12] ; address of argv[]
mov ebx, dword [eax+4] ; argv[1]
mov eax, ebx
jmp COUNT_LOOP
; loop to check 0,1,2 and length between 1 and 30 inclusive
COUNT_LOOP:
cmp byte [eax], byte '0'
je BYTE_OK
cmp byte [eax], byte '1'
je BYTE_OK
cmp byte [eax], byte '2'
je BYTE_OK
cmp byte [eax], byte 0
je END_LOOP
jmp ERR2
BYTE_OK:
inc eax
add [N], dword 1
cmp [N], dword 30
jg ERR2
jmp COUNT_LOOP
END_LOOP:
mov ecx, X
mov eax, dword [ebp+12] ; address of argv[]
mov ebx, dword [eax+4] ; argv[1]
mov eax, dword [N]
mov [i], dword 0
COPY_LOOP:
cmp [i], eax
jge PRINT_X
mov al, byte [ebx]
mov byte [ecx], al
add ecx, dword 1
add ebx, dword 1
add [i], dword 1
jmp COPY_LOOP
PRINT_X:
mov eax, X
call print_string
call print_nl
jmp APPEND_I
APPEND_I:
mov [i], dword 0
mov edx, dword [N]
mov ecx, y
APPEND_LOOP:
cmp [i], edx
jge BUBBLE_SORT
mov eax, [i]
mov [ecx], eax
add ecx, dword 4
add [i], dword 1
jmp APPEND_LOOP
BUBBLE_SORT:
mov eax, 0
mov ebx, 0
mov ecx, 0
mov edx, 0
mov edx, [N]
mov [i], edx
add [i], dword 1
FOR_I:
sub [i], dword 1
cmp [i], dword 0
jle DISPLAY_SORTED
mov [j], dword 1
sub [j], dword 1
FOR_J:
add [j], dword 1
mov edx, [i]
cmp [j], edx
jge FOR_I
mov edx, [j]
mov eax, dword 4
mul edx
mov edx, eax
sub edx, 4
mov ebx, [y+edx]
mov [i_sufcmp], ebx
mov edx, [j]
mov eax, dword 4
mul edx
mov edx, eax
mov ebx, [y+edx]
mov [j_sufcmp], ebx
call sufcmp
mov [k], eax
cmp [k], dword 0
jle FOR_J
mov eax, [i_sufcmp]
mov [temp], eax
mov edx, [j]
mov eax, dword 4
mul edx
mov edx, eax
sub edx, 4
mov eax, [j_sufcmp]
mov [y+edx], eax
mov edx, [j]
mov eax, dword 4
mul edx
mov edx, eax
mov eax, [temp]
mov [y+edx], eax
jmp FOR_J
DISPLAY_SORTED:
mov [i], dword -1
mov eax, disp
call print_string
DISP_I:
add [i], dword 1
mov eax, [N]
cmp [i], eax
jge asm_main_end
mov edx, [i]
mov eax, dword 4
mul edx
mov edx, eax
mov edx, [y+edx]
jmp DISP_Y
DISP_Y:
cmp edx, [N]
jge PRINT_SUF
mov eax, [X+edx]
call print_char
add edx, 1
jmp DISP_Y
PRINT_SUF:
call print_nl
jmp DISP_I
jmp asm_main_end
sufcmp:
enter 0,0
pusha
mov edx, [N]
mov [n], edx
mov edx, [i_sufcmp]
sub [n], edx
mov edx, [N]
mov [m], edx
mov edx, [j_sufcmp]
sub [m], edx
mov edx, [m]
cmp [n], edx
jle MIN_N ; if n < m
jmp MIN_M ; if m < n
MIN_N:
mov edx, [n]
mov [k], edx
mov [o], dword -1
jmp FOR_LOOP
MIN_M:
mov edx, [m]
mov [k], edx
mov [o], dword -1
jmp FOR_LOOP
FOR_LOOP:
add [o], dword 1
mov edx, [k]
cmp [o], edx
jge FOR_LOOP_END
mov edx, [o]
add edx, [i_sufcmp]
mov eax, X
mov al, byte [eax + edx]
mov [temp], al
mov edx, [o]
add edx, [j_sufcmp]
mov eax, X
mov al, byte [eax + edx]
cmp [temp], al
jl RET_MINUS_ONE
cmp [temp], al
jg RET_PLUS_ONE
jmp FOR_LOOP
RET_MINUS_ONE:
popa
mov eax, dword -1
leave
ret
RET_PLUS_ONE:
popa
mov eax, dword 1
leave
ret
FOR_LOOP_END:
jmp IF_N_M
IF_N_M:
mov eax, [m]
cmp [n], eax
jl RET_MINUS_ONE
jmp RET_PLUS_ONE
ERR1:
mov eax, err1
call print_string
jmp asm_main_end
ERR2:
mov eax, err2
call print_string
jmp asm_main_end
asm_main_end:
popa ; restore all registers
leave
ret