-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortInts.s
More file actions
333 lines (293 loc) · 6.87 KB
/
Copy pathSortInts.s
File metadata and controls
333 lines (293 loc) · 6.87 KB
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
.data
newline: .asciiz "\n"
insert: .asciiz "Insert"
delete: .asciiz "Delete"
find: .asciiz "Find"
size: .asciiz "Size"
print: .asciiz "Print"
quit: .asciiz "Quit"
whatnumberinsert: .asciiz "Enter number to insert: "
whatnumberfind: .asciiz "Enter number to find: "
whatnumberdelete: .asciiz "Enter number to delete: "
command: .asciiz "Command: "
mistake: .asciiz "You've entered an invalid command. Valid commands are: \n Insert, Delete, Find, Size, Print, and Quit\n"
goodbye: .asciiz "Good-bye!\n"
comma: .asciiz ","
input_string: .space 6 #make space for 6 characters from input command
input_int: .word 0 #make space for 32-bit integer
int_list: .word -1, 2, 3, 4 #make space for 250 integer list
list_size: .word 4 #size of the list is 0 at first
sizeof: .asciiz "Size of list = "
.text
main:
jal programloop
j endcode
endcode:
li $v0, 4
la $a0, goodbye
syscall
li $v0, 10
syscall
programloop:
jal printcommandstring #print "Command: "
jal readstring #read the input string and save in input_string
jal execcommand #go to the command exec function
j programloop #begin program again
execcommand:
subu $sp, $sp, 4 #allocate 5 words on the stack
sw $ra, 0($sp) #save return address
jal whichcommand
lw $ra, 0($sp)
addiu $sp, $sp, 4 #clear the stack space
jr $ra #go back to execcommand
whichcommand:
la $t0, input_string
lb $t1, 0($t0)
la $t2, insert
lb $t3, 0($t2)
addi $t2, $t2, 1
addi $t0, $t0, 1
beq $t1, $t3, matchinsert
la $t2, delete
lb $t3, 0($t2)
addi $t2, $t2, 1
beq $t1, $t3, matchdelete
la $t2, find
lb $t3, 0($t2)
addi $t2, $t2, 1
beq $t1, $t3, matchfind
la $t2, size
lb $t3, 0($t2)
addi $t2, $t2, 1
beq $t1, $t3, matchsize
la $t2, print
lb $t3, 0($t2)
addi $t2, $t2, 1
beq $t1, $t3, matchprint
la $t2, quit
lb $t3, 0($t2)
addi $t2, $t2, 1
beq $t1, $t3, matchquit
matchinsert:
li $t4, 5
j matchinsertloop
matchinsertloop:
beq $t4, $zero, execinsert
lb $t1, 0($t0)
lb $t3, 0($t2)
bne $t1, $t3, nomatch
sub $t4, $t4, 1
addi $t2, $t2, 1
addi $t0, $t0, 1
j matchinsertloop
execinsert:
subu $sp, $sp, 4
sw $ra, 0($sp) #store return address on the stack
la $a0, whatnumberinsert
jal printstring
jal readint #read an int from the console and store in input_int
lw $a0, input_int #load input_int as an argument
jal afterreadint
lw $ra, 0($sp) #bring back return address
addiu $sp, $sp, 4
jr $ra
#takes int in $a0 as an argument, inserts it into int_list
addtolist:
matchdelete:
li $t4, 5
j matchdeleteloop
matchdeleteloop:
beq $t4, $zero, execdelete
lb $t1, 0($t0)
lb $t3, 0($t2)
bne $t1, $t3, nomatch
sub $t4, $t4, 1
addi $t2, $t2, 1
addi $t0, $t0, 1
j matchdeleteloop
execdelete:
subu $sp, $sp, 4
sw $ra, 0($sp)
la $a0, whatnumberdelete
jal printstring
lw $ra, 0($sp)
addiu $sp, $sp, 4
jr $ra
matchfind:
li $t4, 3
j matchfindloop
matchfindloop:
beq $t4, $zero, execfind
lb $t1, 0($t0)
lb $t3, 0($t2)
bne $t1, $t3, nomatch
sub $t4, $t4, 1
addi $t2, $t2, 1
addi $t0, $t0, 1
j matchfindloop
execfind:
subu $sp, $sp, 4
sw $ra, 0($sp)
la $a0, whatnumberfind
jal printstring
jal readint #read an int from the console and store in input_int
afterreadint:
lw $a0, input_int #load input_int as an argument
la $a1, int_list #lower address of list
lw $t0, list_size #load the size of the list
beqz $t0, listempty #list is empty, answer is zero
li $t4, 4
mult $t0, $t4 #size of the list in bytes
mflo $t0 #move size of list in bytes to t0
add $a2, $t0, $a1 #add that many bytes to the address of the lower bound
sub $a2, $a2, 4 #but subtract 4
jal search
lw $ra, 0($sp)
addiu $sp, $sp, 4
jr $ra
listempty:
li $v0, 0
lw $ra, 0($sp)
addiu $sp, $sp, 4
jr $ra
#a0 is int you're trying to find, a1 is lower address of list, a2 is upper address. Returns index $v0
search:
sub $t0, $a2, $a1 #t0 is size of list (in bytes) between bounds
bltz $t0, notfound #if the size is negative then number isn't in list
add $t1, $a1, $a2 #t1 is upper bound + lower bound
sra $t0, $t0, 3 #divide size of list by 8
sll $t0, $t0, 2 #multiply size by 4 (this is equal to # of bytes to mid from bounds)
addu $t0, $a1, $t0 #equal to address of middle number
lw $t1, 0($t0) #equal to int_list[middle]
beq $t1, $a0, found #go to found procedure if int_list[middle] is the number
j notequal
found:
la $t2, int_list
sub $t0, $t0, $t2 #store (middle address - beginning add. of int_list) in $t0
li $t1, 4
div $t0, $t1 #calculate index of found
mflo $v0 #store here
addi $v0, $v0, 1
add $a0, $v0, $zero
jal printint
j programloop
notfound:
la $t1, int_list
sub $t0, $a1, $t1
li $t1, 4
div $t0, $t1
mflo $t0
add $v0, $t0, $zero
add $a0, $zero, $v0
jal printint
j programloop #go back to main proc.
notequal:
blt $t1, $a0, midless #if midpoint is less than what you're searching for, go to less than function
bgt $t1, $a0, midgreater #if midpoint is greater than what you're searching for, go to grtr than funct
midless:
move $a1, $t0 #move the midpoint to the lowerbound
addi $a1, $a1, 4 #move up the midpoint by one address
j search
midgreater:
move $a2, $t0
sub $a2, $a2, 4
j search
matchsize:
li $t4, 3
j matchsizeloop
matchsizeloop:
beq $t4, $zero, execsize
lb $t1, 0($t0)
lb $t3, 0($t2)
bne $t1, $t3, nomatch
sub $t4, $t4, 1
addi $t2, $t2, 1
addi $t0, $t0, 1
j matchsizeloop
execsize:
subu $sp, $sp, 4
sw $ra, 0($sp)
la $a0, sizeof
jal printstring
lw $ra, 0($sp)
addiu $sp, $sp, 4
lw $a0, list_size
j printint
matchprint:
li $t4, 4
j matchprintloop
matchprintloop:
beq $t4, $zero, execprint
lb $t1, 0($t0)
lb $t3, 0($t2)
bne $t1, $t3, nomatch
sub $t4, $t4, 1
addi $t2, $t2, 1
addi $t0, $t0, 1
j matchprintloop
execprint:
la $t0, int_list
lw $t1, list_size
li $t2, 4
mult $t1, $t2
mflo $t1
sub $t1, $t1, $t2
li $t2, 0
printloop:
lw $a0, 0($t0)
addi $t0, 4
addi $t2, 4
li $v0, 1
syscall
bgt $t2, $t1, endprintloop
la $a0, comma
jal printstring
j printloop
endprintloop:
la $a0, newline
jal printstring
j programloop
matchquit:
li $t4, 3
j matchquitloop
matchquitloop:
beq $t4, $zero, endcode
lb $t1, 0($t0)
lb $t3, 0($t2)
bne $t1, $t3, nomatch
sub $t4, $t4, 1
addi $t2, $t2, 1
addi $t0, $t0, 1
j matchquitloop
nomatch:
la $a0, mistake
li $v0, 4
syscall
jr $ra
printcommandstring:
li $v0, 4
la $a0, command
syscall
jr $ra
readint:
li $v0, 5
syscall
sw $v0, input_int
jr $ra
readstring:
li $v0, 8
la $a0, input_string
li $a1, 32 #max of 32 bytes
syscall
nop
jr $ra
printstring:
li $v0, 4
syscall
jr $ra
printint:
li $v0, 1
syscall
la $a0, newline
j printstring
jr $ra