-
Notifications
You must be signed in to change notification settings - Fork 1
/
Project2.s
158 lines (137 loc) · 2.39 KB
/
Project2.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
.data
emptyErrorMessage: .asciiz "Input is empty."
lengthErrorMessage: .asciiz "Input is too long."
baseErrorMessage: .asciiz "Invalid base-34 number."
userInput: .space 60
.text
main:
li $v0, 8
la $a0, userInput
li $a1, 60
syscall
removeLeadingSpace:
li $t8, 32
lb $t9, 0($a0)
beq $t8, $t9, removeFirst
move $t9, $a0
j checkLength
checkLength:
addi $t0, $t0, 0
addi $t1, $t1, 10
add $t4, $t4, $a0
lengthLoop:
lb $t2, 0($a0)
beqz $t2, done
beq $t2, $t1, done
addi $a0, $a0, 1
addi $t0, $t0, 1
j lengthLoop
done:
beqz $t0, nullError
slti $t3, $t0, 5
beqz $t3, lengthError
move $a0, $t4
j checkString
emptyError:
li $v0, 4
la $a0, emptyErrorMessage
syscall
j exit
lengthError:
li $v0, 4
la $a0, lengthErrorMessage
syscall
j exit
checkString:
lb $t5, 0($a0)
beqz $t5, conversionInitializations
beq $t5, $t1, conversionInitializations
slti $t6, $t5, 48
bne $t6, $zero, baseError
slti $t6, $t5, 58
bne $t6, $zero, Increment
slti $t6, $t5, 65
bne $t6, $zero, baseError
slti $t6, $t5, 89
bne $t6, $zero, Increment
slti $t6, $t5, 97
bne $t6, $zero, baseError
slti $t6, $t5, 121
bne $t6, $zero, Increment
bgt $t5, 120, baseError
Increment:
addi $a0, $a0, 1
j checkString
baseError:
li $v0, 4
la $a0, baseErrorMessage
syscall
j exit
conversionInitializations:
move $a0, $t4
addi $t7, $t7, 0
add $s0, $s0, $t0
addi $s0, $s0, -1
li $s3, 3
li $s2, 2
li $s1, 1
li $s5, 0
convertString:
lb $s4, 0($a0)
beqz $s4, displaySum
beq $s4, $t1, displaySum
slti $t6, $s4, 58
bne $t6, $zero, zeroToNine
slti $t6, $s4, 89
bne $t6, $zero, AToX
slti $t6, $s4, 121
bne $t6, $zero, aTox
zeroToNine:
addi $s4, $s4, -48
j nextStep
AToX:
addi $s4, $s4, -55
j nextStep
aTox:
addi $s4, $s4, -87
nextStep:
beq $s0, $s3, threePower
beq $s0, $s2, twoPower
beq $s0, $s1, onePower
beq $s0, $s5, zeroPower
threePower:
li $s6, 39304
mult $s4, $s6
mflo $s7
add $t7, $t7, $s7
addi $s0, $s0, -1
addi $a0, $a0, 1
j convertString
twoPower:
li $s6, 1156
mult $s4, $s6
mflo $s7
add $t7, $t7, $s7
addi $s0, $s0, -1
addi $a0, $a0, 1
j convertString
onePower:
li $s6, 34
mult $s4, $s6
mflo $s7
add $t7, $t7, $s7
addi $s0, $s0, -1
addi $a0, $a0, 1
j convertString
zeroPower:
li $s6, 1
mult $s4, $s6
mflo $s7
add $t7, $t7, $s7
displaySum:
li $v0, 1
move $a0, $t7
syscall
exit:
li $v0, 10
syscall