-
Notifications
You must be signed in to change notification settings - Fork 0
/
xstigl00.s
340 lines (291 loc) · 10.5 KB
/
xstigl00.s
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
; Autor reseni: Jakub Antonín Štigler xstigl00
; Pocet cyklu k serazeni puvodniho retezce: 635
; Pocet cyklu razeni sestupne serazeneho retezce: 689
; Pocet cyklu razeni vzestupne serazeneho retezce: 213
; Pocet cyklu razeni retezce s vasim loginem: 167
; Implementovany radici algoritmus: Insert sort
; ------------------------------------------------
; DATA SEGMENT
.data
; login: .asciiz "vitejte-v-inp-2023" ; puvodni uvitaci retezec
; login: .asciiz "vvttpnjiiee3220---" ; sestupne serazeny retezec
; login: .asciiz "---0223eeiijnpttvv" ; vzestupne serazeny retezec
login: .asciiz "xstigl00" ; SEM DOPLNTE VLASTNI LOGIN
; A POUZE S TIMTO ODEVZDEJTE
params_sys5: .space 8 ; misto pro ulozeni adresy pocatku
; retezce pro vypis pomoci syscall 5
; (viz nize - "funkce" print_string)
; CODE SEGMENT
.text
main:
; SEM DOPLNTE VASE RESENI
; Insert sort (improoved)
; The basic idea is insert sort, but insert two items at the same time.
;
; Every outer loop two first unsorted items are loaded and sorted into
; registers a0 and a1.
;
; The first inner loop inserts a1, and the second inner loop inserts
; a0. Because I know that a0 is smaller than a1, I can just continue
; and don't have to check the items before.
; List of used registers and their usage/meaning:
; s0, s1: index of first 2 unsorted items
; s2, s3: indexes in the inner loops
; a0, a1: ordered items to insert
; a2, a3: items from login
; t0, t1: temorary (used in conditions)
; v1: 1
; v0: 2
; sort the first 2 items, load the third item to a0 and check if it is
; 0.
lb $a2, login($zero)
daddi $v1, $zero, 1
daddi $v0, $zero, 2
beqz $a2, main_end
lb $a3, login($v1)
daddi $s2, $zero, 1
daddi $s3, $zero, 3
beqz $a3, main_end
sltu $t0, $a3, $a2
daddi $s0, $zero, 2
daddi $s1, $zero, 3
lb $a0, login($v0)
beqz $t0, no_swap
sb $a2, login($v1)
sb $a3, login($zero)
no_swap:
beqz $a0, main_end
outer:
; The outer loop, the next a0 has already been loaded by the previous
; iteration. Load to a1, check for zero, prepare for the inner loops.
lb $a1, login($s1)
lb $a2, login($s2) ; part of inner loop unwrapped
daddi $s2, $s2, -1 ; part of inner loop unwrapped
sltu $t1, $a1, $a0
beqz $a1, single_prep ; When a1 is zero, insert only single item: a0
sltu $t0, $a1, $a2 ; part of inner loop unwrapped
beqz $t1, insert_double ; check if I should swap a0 an a1
lb $a1, login($s0) ; swap by loading in opposite order
lb $a0, login($s1) ; swap by loading in opposite order
insert_double:
; The first inner loop. It finds place for a1 in the sorted part.
; While searching for the place, shift all examined items to the right
; by 2.
; The current version of loop is optimized version of original loop.
; The current version has two iterations of the original version merged
; into a single iteration, it goes only to index 1 to avoid negative
; index because it assumes that there will always be at least 1 next
; item.
;
; When the loop finds place for a1, it jums to the other loop
; (insert_single). When it comes to the last idnex it jumps to a
; special case that correctly inserts the two items and the item at
; index 0 at the indexes 0, 1 and 2.
;
; The two original loops are somehow visible - you can see two symetric
; parts tat just have a2 and a3 swapped.
; part 1:
beqz $t0, insert_single_prep
lb $a3, login($s2)
sb $a2, login($s3)
daddi $s3, $s3, -1
sltu $t0, $a1, $a3
daddi $s2, $s2, -1
beq $s3, $v0, insert_double_last
; part 2:
beqz $t0, insert_single_prep3
lb $a2, login($s2)
sb $a3, login($s3)
daddi $s3, $s3, -1
sltu $t0, $a1, $a2
daddi $s2, $s2, -1
bne $s3, $v0, insert_double
; Correctly insert the a0, a1 and a2/a3 in indexes 0, 1 and 2.
;
; There are two versions, one to jump from the first part of the double
; loop, and one for the second part of the double loop. This is because
; in the first part of the loop, a3 already has the value of the item
; at index 0, and in the secon part of the loop this value is in a2.
; first version of insert_double_last
; insert_double_last:
sltu $t1, $a0, $a2
daddi $s0, $s0, 2
daddi $s1, $s1, 2
beq $t0, $v1, insert_double_last012
beq $t1, $v1, insert_double_last021
; insert_double_last201:
sb $a0, login($v1)
sb $a1, login($v0)
; preparation for next iteration of outer loop
lb $a0, login($s0)
daddi $s2, $s0, -1
daddi $s3, $s1, 0
bnez $a0, outer
j main_end
insert_double_last021:
sb $a0, login($zero)
sb $a2, login($v1)
sb $a1, login($v0)
; preparation for next iteration of outer loop
lb $a0, login($s0)
daddi $s2, $s0, -1
daddi $s3, $s1, 0
bnez $a0, outer
j main_end
insert_double_last012:
sb $a0, login($zero)
sb $a1, login($v1)
sb $a2, login($v0)
; preparation for next iteration of outer loop
lb $a0, login($s0)
daddi $s2, $s0, -1
daddi $s3, $s1, 0
bnez $a0, outer
j main_end
; second version of insert_double_last
insert_double_last:
sltu $t1, $a0, $a3
daddi $s0, $s0, 2
daddi $s1, $s1, 2
beq $t0, $v1, insert_double_last013
beq $t1, $v1, insert_double_last031
; insert_double_last301:
sb $a0, login($v1)
sb $a1, login($v0)
; preparation for next iteration of outer loop
lb $a0, login($s0)
daddi $s2, $s0, -1
daddi $s3, $s1, 0
bnez $a0, outer
j main_end
insert_double_last031:
sb $a0, login($zero)
sb $a3, login($v1)
sb $a1, login($v0)
; preparation for next iteration of outer loop
lb $a0, login($s0)
daddi $s2, $s0, -1
daddi $s3, $s1, 0
bnez $a0, outer
j main_end
insert_double_last013:
sb $a0, login($zero)
sb $a1, login($v1)
sb $a3, login($v0)
; preparation for next iteration of outer loop
lb $a0, login($s0)
daddi $s2, $s0, -1
daddi $s3, $s1, 0
bnez $a0, outer
j main_end
; Now there is preparation for the second inner loop (insert_single).
;
; There are different preparation steps based on from where is jumped
; into this loop.
; Prep 1: jump from the first part of the insert_double loop.
insert_single_prep3:
sb $a1, login($s3)
sltu $t0, $a0, $a3
daddi $s3, $s3, -1
j insert_single2 ; Jump to the second part of the loop, so that I don't
; have to copy a3 to a2
; Prep 2: jump from the start of the outer loop
single_prep:
sltu $t0, $a0, $a2
daddi $s3, $s3, -1
j insert_single
; Prep 3: jump from the second part of the insert_double loop.
insert_single_prep:
sltu $t0, $a0, $a2
sb $a1, login($s3)
daddi $s3, $s3, -1
insert_single:
; The second inner loop. It finds place for a0 in the sorted part.
; While searching for the place, shift all examined items to the right
; by 1.
;
; The loop is optimized in the exact same way as insert_double and so
; it also has the same two parts.
; Part 1:
beqz $t0, inner_single_end
lb $a3, login($s2)
sb $a2, login($s3)
daddi $s3, $s3, -1
sltu $t0, $a0, $a3
daddi $s2, $s2, -1
beq $s3, $v1, insert_single_last
insert_single2:
; Part 2:
beqz $t0, inner_single_end
lb $a2, login($s2)
sb $a3, login($s3)
daddi $s3, $s3, -1
sltu $t0, $a0, $a2
daddi $s2, $s2, -1
bne $s3, $v1, insert_single
; The last iteration of the insert_single loop. It is simpler because
; it only has to sort a0 and a2/a3 at indexes 0 and 1.
;
; It also has the two versions for the same reason.
; Version 1:
; insert_single_last:
daddi $s0, $s0, 2
daddi $s1, $s1, 2
beq $t0, $v1, insert_single_last02
sb $a0, login($v1)
; preparation for next iteration of outer loop
lb $a0, login($s0)
daddi $s2, $s0, -1
daddi $s3, $s1, 0
bnez $a0, outer
j main_end
insert_single_last02:
sb $a0, login($zero)
sb $a2, login($v1)
; preparation for next iteration of outer loop
lb $a0, login($s0)
daddi $s2, $s0, -1
daddi $s3, $s1, 0
bnez $a0, outer
j main_end
; Version 2:
insert_single_last:
beq $t0, $v1, insert_single_last03
sb $a0, login($v1)
; preparation for next iteration of outer loop
lb $a0, login($s0)
daddi $s2, $s0, -1
daddi $s3, $s1, 0
bnez $a0, outer
j main_end
insert_single_last03:
sb $a0, login($zero)
sb $a3, login($v1)
; preparation for next iteration of outer loop
lb $a0, login($s0)
daddi $s2, $s0, -1
daddi $s3, $s1, 0
bnez $a0, outer
j main_end
; When the loop insert_single finds place for a0, it jumps here.
inner_single_end:
daddi $s0, $s0, 2
daddi $s1, $s1, 2
sb $a0, login($s3)
; preparation for next iteration of outer loop
lb $a0, login($s0)
daddi $s2, $s0, -1
daddi $s3, $s1, 0
bnez $a0, outer
main_end:
; all sorted print result and exit
daddi r4, r0, login
jal print_string
syscall 0 ; exit
print_string: ; adresa retezce se ocekava v r4
sw r4, params_sys5(r0)
daddi r14, r0, params_sys5 ; adr pro syscall 5 musi do r14
; nop
; nop
syscall 5 ; systemova procedura - vypis retezce na terminal
jr r31 ; return - r31 je urcen na return address