-
Notifications
You must be signed in to change notification settings - Fork 0
/
pa2.s
354 lines (198 loc) · 6.79 KB
/
pa2.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#-------------------------------------------------------------------------------------
# Timothy Hitchener
# Due: December 1, 2017
#-------------------------------------------------------------------------------------
.data
error_msg: .asciiz "NaN" #error message for invalid hexadecimal
too_large_msg: .asciiz "too large"
output_line: .asciiz "\n" #stores new line for output
input: .space 1001 #allocated space for characters
.text
main:
jal input_data
loop: # main loop
jal find_start_end
add $s0, $zero, $v0
add $s1, $zero, $v1
add $a0, $zero, $v0
add $a1, $zero, $v1
jal validity_check
add $s2, $zero, $v0
add $s3, $zero, $v1
add $a0, $zero, $s0
add $a1, $zero, $s1
add $a2, $zero, $s3
add $a3, $zero, $s2
jal subprogram2
add $a0, $zero, $s2
jal subprogram3
addi $a0, $s1, 1
j loop
end:
li $v0, 10
syscall
input_data:
la $a0, input
li $v0, 4
syscall
la $a0, input
li $a1, 1000
li $v0, 8
syscall
jr $ra
find_start_end:
add $t0, $zero, $a0
start:
lb $t1, 0($t0)
beq $t1, 10, end
beq $t1, 0, end
beq $t1, 32, incr_start_ptr
beq $t1, 44, incr_start_ptr
addi $t2, $t0, 1
comma:
lb $t1, 0($t2)
beq $t1, 10, shift_left
beq $t1, 0, shift_left
bne $t1, 44, incr_last_ptr
addi $t2, $t2, -1
shift_left:
lb $t1, 0($t2)
beq $t1, 32, decr_last_ptr
beq $t1, 0, decr_last_ptr
beq $t1, 10, decr_last_ptr
add $v0, $zero, $t0
add $v1, $zero, $t2
jr $ra
incr_start_ptr:
addi $t0, $t0, 1
j start
incr_last_ptr:
addi $t2, $t2, 1
j comma
decr_last_ptr:
addi $t2, $t2, -1
j shift_left
validity_check:
add $t2, $zero, $zero
is_valid:
lb $t1, 0($t0)
bge $t1, 103, not_a_number
bge $t1, 96, incrChar
bge $t1, 71, not_a_number
bge $t1, 64, incrChar
bge $t1, 58, not_a_number
bge $t1, 47, incrChar
j not_a_number
incrChar:
addi $t0, $t0, 1
addi $t2, $t2, 1
bgt $t0, $a1, valid
j is_valid
not_a_number:
addi $v0, $zero, 1
addi $v1, $zero, 0
jr $ra
too_large:
addi $v0, $zero, 2
addi $v1, $zero, 0
jr $ra
valid:
bgt $t2, 8, too_large
addi $v0, $zero, 3
add $v1, $zero, $t2
jr $ra
return:
jr $ra
subprogram1:
addi $v0, $zero, 1
addi $t3, $zero, 16
ascii_to_hex:
bge $a2, 96, lower
bge $a2, 64, upper
bge $a2, 47, number
lower:
addi $t0, $a2, -87
j calc_exp
upper:
addi $t0, $a2, -55
j calc_exp
number:
addi $t0, $a2, -48
j calc_exp
calc_exp:
sub $t1, $a0, $a1
addi $t1, $t1, -1
raise_to_exp:
beq $t1, $zero, multiply
mult $v0, $t3
mflo $v0
addi $t1, $t1, -1
j raise_to_exp
multiply:
mult $v0, $t0
mflo $v0
jr $ra
subprogram2:
bne $a3, 3, return
add $t0, $zero, $a0
add $t1, $zero, $a1
add $t2, $zero, $a2
add $s6, $zero, $zero
add $t4, $zero, $t0
add $t9, $zero, $zero
add $s5, $zero, $ra
hexadecimal_conversion:
add $a0, $zero, $t2
add $a1, $zero, $s6
lb $a2, 0($t4)
jal subprogram1
add $t9, $t9, $v0
addi $t4, $t4, 1
addi $s6, $s6, 1
blt $s6, $t2, hexadecimal_conversion
done:
addi $sp, $sp, -4
sw $t9, 0($sp)
add $ra, $zero, $s5
jr $ra
subprogram3:
beq $a0, 1, print_NaN
beq $a0, 2, print_too_large
beq $a0, 3, print_decimal
jr $ra
print_NaN:
la $a0, error_msg
li $v0, 4
syscall
la $a0, 44
li $v0, 11
syscall
jr $ra
print_too_large:
la $a0, too_large_msg
li $v0, 4
syscall
la $a0, 44
li $v0, 11
syscall
jr $ra
print_decimal:
addi $t0, $zero, 10000
lw $t1, 0($sp)
addi $sp, $sp, 4
divu $t1, $t0
mflo $t2
mfhi $t3
beq $t2, $zero, print_remainder
print_quotient:
add $a0, $zero, $t2
li $v0, 1
syscall
print_remainder:
add $a0, $zero, $t3
li $v0, 1
syscall
la $a0, 44
li $v0, 11
syscall
jr $ra