-
Notifications
You must be signed in to change notification settings - Fork 0
/
quicksort.asm
533 lines (365 loc) · 7.38 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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
# This MIPS program should sort a set of numbers using the quicksort algorithm
# The program should use MMIO
.data
welcomeStatement: .asciiz "Welcome to QuickSort. Enter an Array and a Command\n"
sortedStatement: .asciiz "The sorted array is: \n"
reInitializedStatement: .asciiz "The Array is re-initialized\n"
array: .space 600
wordArray: .space 600
.align 4
storageWordArray: .space 2400
arrayToPrint: .space 600
print: .space 600
intermediate: .space 600
.text
.globl main
main:
#printing first welcome line, prompting for an array and command
la $a1, welcomeStatement
jal Print
start: #loading inputed array into array space
la $a1, array
jal Read
#print inputed array
la $a1, array
jal Print
#command = s, we need to convert, sort, ask for another prompt
start2: beq $s3, 115, CommandS
#command = c, we need to clear both and ask for another prompt
beq $s3, 99, CommandC
li $v0, 10 # ending the program
syscall
################
Print:
addi $sp, $sp, -4 # create space on stack
sw $ra, 0($sp) # store return address to main on stack
PrintLoop:
lb $a0, 0($a1) #extract char we want to print and put into a0
beq $a0, $0, PrintDone #if at end of string we want to end, otherwise print
jal Write # call Write with valid character in a0
addi $a1, $a1, 1 # increment pointer
j PrintLoop
Write: lui $t0, 0xffff # ffff0000
WriteLoop:
lw $t1, 8($t0) # control
andi $t1,$t1,0x0001
beq $t1,$0, WriteLoop
sb $a0, 12($t0) # char to print is in a0
jr $ra
#restore stack and end, returning to main
PrintDone:
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
#######################
Read: # read user text into buffer
lui $t0, 0xffff # ffff0000
ReadLoop:
lw $t1, 0($t0) # control
andi $t1,$t1,0x0001
beq $t1,$zero,ReadLoop
lb $v0, 4($t0) # load character inputed by user is stored in v0
#3 possibilities for letters
add $t6, $v0, $zero
beq $t6, 99, store
beq $t6, 115, store
beq $t6, 'q', done
#otherwise store char
sb $v0, 0($a1) # store character into buffer
addi $a1, $a1, 1 # increment buffer pointer
j ReadLoop
#storing command in s3
store:
add $s3, $t6, $0
j ReadFin
ReadFin:
sb $zero, 0($a1) #end string with null
jr $ra
####################
Convert:
loop:
lb $t0, 0($a1) #extract first value
beq $t0, $0, fin
sub $t0, $t0, 48
add $a1, $a1, 1
blt $t0, 0, loop
lb $t1, 0($a1)
sub $t1, $t1, 48
blt $t1, 0, storeDigit
li $t7, 10
mul $t3, $t7, $t0
add $t0, $t3, $t1
sb $t0, 0($a2)
add $a2, $a2, 1
add $a1, $a1, 1
j loop
storeDigit:
sb $t0, 0($a2)
add $a2, $a2, 1
add $a1, $a1, 1
j loop
fin:
#storing null char at last pos, 1 after the one we're at now WTFFFi guess SO
sb $0, 0($a2)
jr $ra
#######################################
ConvertToWord:
toWordLoop:
lb $t0, 0($a1)
beq $t0, $0, toWordFin
sb $t0, 0($a2)
add $a1, $a1, 1
add $a2, $a2, 4
j toWordLoop
toWordFin:
sb $0, 0($a2)
jr $ra
#######################################
ArraySize:
li $t6, 0
sizeLoop:
lb $t0, 0($a1)
beq $t0, $0, arrayFin
add $t6, $t6, 1
add $a1, $a1, 1
j sizeLoop
arrayFin:
jr $ra
#############################
####################
CommandS:
la $a1, array
la $a2, wordArray
jal Convert
la $a1, wordArray
jal ArraySize
la $a1, wordArray
la $a2, storageWordArray
jal ConvertToWord
la $a0, storageWordArray
li $a1, 0
move $t0, $t6 #store number of elements
addi $t0, $t0, -1
move $a2, $t0
jal quicksortMethod
la $a1, sortedStatement
jal Print
la $a0, storageWordArray
la $a2, arrayToPrint
j okMaybeThisWorks
j done
#########################
okMaybeThisWorks:
loopF:
lb $t2, 0($a0)
beq $t2, $0, umOK
sb $t2, 0($a2)
addi $a0, $a0, 4
addi $a2, $a2, 1
j loopF
umOK:
sb $0, 0($a2)
la $a0, arrayToPrint
la $a1, print
j Print2
#####################
Print2:
lb $t0, 0($a0)
bgt $t0, 9, doubleDig
beq $t0, $0, okNOW
add $t0, $t0, 48
sb $t0, 0($a1)
li $t5, 32
sb $t5, 1($a1)
add $a0, $a0, 1
add $a1, $a1, 2
j Print2
doubleDig:
li $t7, 10
div $t0, $t7
mflo $t6
mfhi $t7
addi $t6, $t6, 48
addi $t7, $t7, 48
sb $t6, 0($a1)
sb $t7, 1($a1)
li $t5, 32
sb $t5, 2($a1)
add $a0, $a0, 1
add $a1, $a1, 3
j Print2
okNOW:
la $a1, print
jal Print
j startNew
############
CommandC:
la $a1, array
la $a2, wordArray
la $a3, storageWordArray
la $a0, intermediate
la $t7, arrayToPrint
la $t8, pPrint
loopC:
lb $t9, 0($a1)
beq $t9, $0, loopC2
sb $0, 0($a1)
addi $a1, $a1, 1
j loopC
loopC2:
lb $t9, 0($a2)
beq $t9, $0, loopC3
sb $0, 0($a2)
addi $a2, $a2, 1
j loopC2
loopC3:
lb $t9, 0($a3)
beq $t9, $0, loopC4
sb $0, 0($a3)
addi $a3, $a3, 4
j loopC3
loopC4:
lb $t9, 0($a0)
beq $t9, $0, loopC5
sb $0, 0($a0)
addi $a0, $a0, 1
j loopC4
loopC5:
lb $t9, 0($t7)
beq $t9, $0, loopC6
sb $0, 0($t7)
addi $t7, $t7, 1
j loopC5
loopC6:
lb $t9, 0($t8)
beq $t9, $0, CStartOver
sb $0, 0($t8)
addi $t8, $t8, 1
j loopC6
CStartOver:
la $a1, reInitializedStatement
jal Print
j start
########################
startNew:
la $a1, intermediate
jal Read
la $a2, intermediate
##########
#if $a0 isnt empty we go thru this loop to end at the end of now filled intermediate ($a2) array
la $a0, array
combineLoop:
lb $t1, 0($a0)
beq $t1, $0, combine
add $a0, $a0, 1
j combineLoop
#then, we combine them by starting at the end of $a0 and beginning of $a2, stopping when $a0 is empty
combine:
lb $t0, 0($a2)
beq $t0, $0, thisFin
sb $t0, 0($a0)
add $a0, $a0, 1
add $a2, $a2, 1
j combine
thisFin:
sb $0, 0($a0)
j start2
##################
quicksortMethod:
addi $sp, $sp, -24
sw $s0, 0($sp)
sw $s1, 4($sp)
sw $s2, 8($sp)
sw $a1, 12($sp)
sw $a2, 16($sp)
sw $ra, 20($sp)
move $s0, $a1 # s0=l
move $s1, $a2 # s1=r
move $s2, $a1 # s2=p
# while (l < r)
BigLoop:
bge $s0, $s1, BigLoopDone
# while (val(l) <= val(p) AND l < r)
Loop1:
li $t7, 4
mult $s0, $t7
mflo $t0
add $t0, $t0, $a0 # t0 = val(l)
lw $t0, 0($t0)
mult $s2, $t7
mflo $t1
add $t1, $t1, $a0 #t1 = val(p)
lw $t1, 0($t1)
bgt $t0, $t1, Loop1Done #make sure val(l)<val(p)
bge $s0, $a2, Loop1Done # make sure l < right
addi $s0, $s0, 1
j Loop1
Loop1Done:
Loop2:
li $t7, 4
mult $s1, $t7
mflo $t0
add $t0, $t0, $a0
lw $t0, 0($t0)
mult $s2, $t7
mflo $t1
add $t1, $t1, $a0 # t1 = val(p)
lw $t1, 0($t1)
blt $t0, $t1, Loop2Done # make sure val(r) >= val(p)
ble $s1, $a1, Loop2Done # make sure r > left
addi $s1, $s1, -1 # r--
j Loop2
#(l >= r)
Loop2Done:
blt $s0, $s1, swap
li $t7, 4
mult $s2, $t7
mflo $t6
add $t0, $t6, $a0
mult $s1, $t7
mflo $t6
add $t1, $t6, $a0
lw $t2, 0($t0)
lw $t3, 0($t1)
sw $t3, 0($t0)
sw $t2, 0($t1)
move $a2, $s1
addi $a2, $a2, -1
jal quicksortMethod
lw $a1, 12($sp)
lw $a2, 16($sp)
lw $ra, 20($sp)
# quick(arr, r + 1, right)
move $a1, $s1
addi $a1, $a1, 1 # a1 = r = r + 1
jal quicksortMethod
lw $a1, 12($sp)
lw $a2, 16($sp)
lw $ra, 20($sp)
lw $s0, 0($sp)
lw $s1, 4($sp)
lw $s2, 8($sp)
addi $sp, $sp, 24
jr $ra
swap:
li $t7, 4
mult $s0, $t7
mflo $t6
add $t0, $t6, $a0
mult $s1, $t7
mflo $t6
add $t1, $t6, $a0
lw $t2, 0($t0)
lw $t3, 0($t1)
sw $t3, 0($t0)
sw $t2, 0($t1)
j BigLoop
BigLoopDone:
lw $s0, 0($sp)
lw $s1, 4($sp)
lw $s2, 8($sp)
addi $sp, $sp, 24
jr $ra
done:
la $v0, 10
syscall