-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMips Project Sorting
500 lines (401 loc) · 7.57 KB
/
Mips Project Sorting
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
.data
space: .asciiz " " # a space string.
line: .asciiz "\n" # a newline string.
arrayi: .word 0 :100 # an array of word, for storing values.
size: .word 10 # actual count of the elements in the array.
ending1: .asciiz "========================================================\n"
starting1: .asciiz " \n\t\tCOMPUTER ARCITECTURE AND LOGIC DESIGN\n"
welcome: .asciiz "\t\t WELCOME TO MIPS ASSEMBLY LANGUAGE"
welcome1: .asciiz "\n \t\t\tSORTING OF ARRAYS \n"
starting2: .asciiz "\n \t\t\t GROUP MEMBERS\n"
starting3: .asciiz "\n \t\t\t MUTAYYAB IMRAN\n \t\t\t(02-131212-063)\n\n \t\t\t MUTAYYAB IMRAN\n \t\t\t(02-131212-063)\n\n\t\t\tMUTAYYAB IMRAN\n \t\t\t(02-131212-063)\n"
prompt5: .asciiz " \n-------------------------------------------------------\n\t\tSORTING ALGORITHMS\n-------------------------------------------------------\n"
prompt6:.asciiz"\nWHICH SORTING YOU WANT TO APPLY?\nPlease enter \n \n 1) FOR INSERTION SORT \n 2) FOR SLECTION SORT \n 3) FOR BUBBLE SORT \n"
pr1: .asciiz"---------------------INSERTION SORT--------------------\n\nHow many elements you want to store in array?\n"
Length: .asciiz "-----------------------BUBBLE SORT---------------------\n\nHow many elements you want to store in array ? \n"
str1: .asciiz "---------------------SELECTION SORT--------------------\n\n How many elements you want to store in array ? \n"
str2: .asciiz "Insert the array elements:\n"
str3: .asciiz "The sorted array is:\n"
str5: .asciiz "\n"
prompt3: .asciiz ""
nl: .asciiz "\n"
t2: .asciiz "\n"
terminate: .asciiz "\n\t\t\tSorting Complete\n"
pcorr: .asciiz "\nINVALID INPUT !!!\n"
retry: .asciiz "Please enter valid input from1-3!!!\n\n"
promptlll: .asciiz "\nDO YOU WANT TO DO SORTING AGAIN?? press 1 \n for YES \n Any key for NO \n"
exiting: .asciiz " \n\t\t\tTHANKYOU\n "
.align 2
array: .space 100
.text
.globl main
main:
li $v0,4
la $a0,ending1
syscall
li $v0,4
la $a0,welcome
syscall
li $v0,4
la $a0,starting1
syscall
li $v0,4
la $a0,starting2
syscall
li $v0,4
la $a0,starting3
syscall
li $v0,4
la $a0,ending1
syscall
li $v0,4
la $a0,welcome1
syscall
li $v0,4
la $a0,prompt5
syscall
correct:
li $v0,4
la $a0,prompt6
syscall
li $v0,5
syscall
move $t0,$v0
li $t1,1
li $t2,3
li $t3,2
beq $t0,$t1,insertion
beq $t0,$t2,bubble
beq $t0,$t3,selectionsort
li $v0,4
la $a0,pcorr
syscall
li $v0,4
la $a0,retry
syscall
b correct
Methods:
insertion:
params_info:
li $v0, 4
la $a0, pr1
syscall
li $v0, 5
syscall
la $t0, size
sw $v0, ($t0)
receive_values_loop_info:
li $v0, 4
la $a0, str2
syscall
li $v0, 4
la $a0, line
syscall
receive_values_loop_prep:
la $t0, arrayi
lw $t1, size
li $t2, 0
receive_values_loop:
bge $t2, $t1, receive_values_end
li $v0, 5
syscall
sw $v0, 0($t0)
addi $t0, $t0, 4
addi $t2, $t2, 1
j receive_values_loop
receive_values_end:
sort_prep:
la $t0, arrayi
lw $t1, size
li $t2, 1
sort_xloop:
la $t0, arrayi
bge $t2, $t1, sort_xloop_end
move $t3, $t2
sort_iloop:
la $t0, arrayi
mul $t5, $t3, 4
add $t0, $t0, $t5
ble $t3, $zero, sort_iloop_end
lw $t7, 0($t0)
lw $t6, -4($t0)
bge $t7, $t6, sort_iloop_end
lw $t4, 0($t0)
sw $t6, 0($t0)
sw $t4, -4($t0)
subi $t3, $t3, 1
j sort_iloop
sort_iloop_end:
addi $t2, $t2, 1
j sort_xloop
sort_xloop_end:
li $v0, 4
la $a0, str3
syscall
li $v0, 4
la $a0, line
syscall
jal print
exit:
li $v0,4
la $a0,terminate
syscall
li $v0,4
la $a0,ending1
syscall
li $v0,4
la $a0,promptlll
syscall
li $v0,5
syscall
move $t0,$v0
li $t1,1
beq $t0,$t1,correct
li $v0,4
la $a0,exiting
syscall
li $v0,4
la $a0,ending1
syscall
li $v0,10
syscall
print:
print_loop_prep:
la $t0, arrayi
lw $t1, size
li $t2, 0
print_loop:
bge $t2, $t1, print_end
li $v0, 1
lw $a0, 0($t0)
syscall
li $v0, 4
la $a0, line
syscall
addi $t0, $t0, 4
addi $t2, $t2, 1
j print_loop
print_end:
li $v0, 4
la $a0, line
syscall
jr $ra
selectionsort:
la $a0, str1
li $v0, 4
syscall
li $v0, 5
syscall
move $s2, $v0
sll $s0, $v0, 2
sub $sp, $sp, $s0
la $a0, str2
li $v0, 4
syscall
move $s1, $zero
for_get: bge $s1, $s2, exit_get
sll $t0, $s1, 2
add $t1, $t0, $sp
li $v0, 5
syscall
sw $v0, 0($t1)
addi $s1, $s1, 1
j for_get
exit_get: move $a0, $sp
move $a1, $s2
jal isort
la $a0, str3
li $v0, 4
syscall
move $s1, $zero
for_print: bge $s1, $s2, exit_print
sll $t0, $s1, 2
add $t1, $sp, $t0
lw $a0, 0($t1)
li $v0, 1
syscall
la $a0, str5
li $v0, 4
syscall
addi $s1, $s1, 1
j for_print
exit_print: add $sp, $sp, $s0
li $v0,4
la $a0,terminate
syscall
li $v0,4
la $a0,ending1
syscall
li $v0,4
la $a0,promptlll
syscall
li $v0,5
syscall
move $t0,$v0
li $v0,4
la $a0,exiting
syscall
li $v0,4
la $a0,ending1
syscall
li $t1,1
beq $t0,$t1,correct
li $v0, 10
syscall
isort: addi $sp, $sp, -20
sw $ra, 0($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)
sw $s2, 12($sp)
sw $s3, 16($sp)
move $s0, $a0
move $s1, $zero
subi $s2, $a1, 1
isort_for: bge $s1, $s2, isort_exit
move $a0, $s0
move $a1, $s1
move $a2, $s2
jal mini
move $s3, $v0
move $a0, $s0
move $a1, $s1
move $a2, $s3
jal swapsel
addi $s1, $s1, 1
j isort_for
isort_exit: lw $ra, 0($sp)
lw $s0, 4($sp)
lw $s1, 8($sp)
lw $s2, 12($sp)
lw $s3, 16($sp)
addi $sp, $sp, 20
jr $ra
# index_minimum routine
mini: move $t0, $a0
move $t1, $a1
move $t2, $a2
sll $t3, $t1, 2
add $t3, $t3, $t0
lw $t4, 0($t3)
addi $t5, $t1, 1
mini_for: bgt $t5, $t2, mini_end
sll $t6, $t5, 2
add $t6, $t6, $t0
lw $t7, 0($t6)
bge $t7, $t4, mini_if_exit
move $t1, $t5
move $t4, $t7
mini_if_exit: addi $t5, $t5, 1
j mini_for
mini_end: move $v0, $t1
jr $ra
swapsel:
sll $t1, $a1, 2
add $t1, $a0, $t1
sll $t2, $a2, 2
add $t2, $a0, $t2
lw $t0, 0($t1)
lw $t3, 0($t2)
sw $t3, 0($t1)
sw $t0, 0($t2)
jr $ra
# GUESS BUBBLE SORT
bubble:
jal Enter_Length
jal Enter_items
jal Display_Unsorted_arrayV
jal sort_array_ascending
jal sorted_array_ascending_displayV
j Exit
Enter_Length:
la $a0,Length
li $v0, 4
syscall
li $v0, 5
syscall
move $t0, $v0
li $v0,4
la $a0,str2
syscall
li $t1, 0
li $t2, 1
jr $ra
la $a0,str2
li $v0, 4
syscall
Enter_items:
addi $t2, $t2, 1
li $v0, 5
syscall
sw $v0, array($t1)
addi $t1, $t1, 4
ble $t2, $t0, Enter_items
jr $ra
Display_Unsorted_arrayV:
li $t1, 0
li $t2, 1
jr $ra
jr $ra
sort_array_ascending:
li $t2, 0
out:
addi $t2, $t2, 1
la $a1, array
li $t1, 0
sub $t3, $t0, 1
addi $t4, $t2, 1
ble $t2, $t3, inn
jr $ra
inn:
lw $t5, 0($a1)
lw $t6, 4($a1)
bgt $t5, $t6, swp
j cont
swp:
sw $t6, 0($a1)
sw $t5, 4($a1)
cont:
addi $a1, $a1, 4
addi $t4, $t4, 1
bgt $t4, $t0, out
j inn
sorted_array_ascending_displayV:
li $t1, 0
li $t2, 1
la $a0,str3
li $v0, 4
syscall
print_sorted_array:
addi $t2, $t2, 1
lw $a0, array($t1)
li $v0, 1
syscall
la $a0, nl
li $v0, 4
syscall
addi $t1, $t1, 4
ble $t2, $t0, print_sorted_array
jr $ra
Exit:
la $a0, terminate
li $v0, 4
syscall
li $v0,4
la $a0,ending1
syscall
li $v0,4
la $a0,promptlll
syscall
li $v0,5
syscall
move $t0,$v0
li $t1,1
beq $t0,$t1,correct
li $v0,4
la $a0,exiting
syscall
li $v0,4
la $a0,ending1
syscall
li $v0, 10
syscall