-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberConversionCalculator.asm
More file actions
227 lines (207 loc) · 5.55 KB
/
NumberConversionCalculator.asm
File metadata and controls
227 lines (207 loc) · 5.55 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
.data
endl: .asciiz "\n"
baseOfNum: .asciiz "Enter base of number: "
convert: .asciiz "Enter new base to convert to: "
numberPrompt: .asciiz "Enter a number to convert: "
result: .word 80
array: .word 80
output: .space 40 #used to store final num
numberInput: .space 33
.text
main:
#enter num
li $v0, 4
la $a0, numberPrompt
syscall
#user input number
li $v0, 8
la $a0, numberInput
la $a1, 33
syscall
#enter base
li $v0, 4
la $a0, baseOfNum
syscall
li $v0, 5
syscall
la $a0, numberInput
move $a1, $v0
#calling baseToDecimal
jal baseToDecimal
move $a2, $v1 #load num into a2
#ask for base to convert to
li $v0, 4
la $a0, convert
syscall
#get base in a1
li $v0, 5
syscall
move $a1, $v0
#base = a1, number = a2
jal decimalToAny
exit:
li $v0, 10
syscall
#This function converts the user input into a Int that is also a decimal number so
#that we can call our decimalToAny function and make the conversion
# the params we take in are char s[], int base
#int base being the base of the number the user entered
baseToDecimal:
# $a1 = int base
#storing the ra to the stack so we can use it later
addi $sp, $sp, -4
sw $ra, 0($sp)
la $a3, numberInput
# jal to get the size of the users input
jal stringSize
move $s2, $v1
# $s2 = size -1
addi $s2, $s2, -1 #now it's the last element in numberInput string
#decimalNumb = 0
add $s3, $zero, $zero
#exp = 0
add $s4, $zero, $zero
loop:
#checks to see if we are done looping aka the index is -1
slt $t0, $s2, $zero
#return if index is -1
beq $t0, 1, baseToDecReturn
#if not we continue
lb $a2, numberInput($s2)
jal valueGet
#result from this would be the val of the last bit
#calling pow so we load $a1 = base, $a2 = exponent
move $a2, $s4
jal pow
#$v0 stores the product we multiply the bit val by
mulu $t1, $v0, $v1 #multiplication to get bitvalue * the base and exponent
add $s3, $s3, $t1 # put the result in the decimalNum
addi $s4, $s4, 1 # move the exponent up 1 for next loop
#gets next bit
addi $s2, $s2, -1
j loop
#loop back to run again with new bit
baseToDecReturn:
#pop so we can get ra and go back to main func
lw $ra, 0($sp)
addi $sp, $sp, 4
move $v1, $s3
jr $ra
stringSize: # $a3 = char s[]
#
addi $t8, $zero, 0 #count = 0
stringLoop:
lb $t9, 0($a3)
beqz $t9, stringReturn
addi $t8, $t8, 1
addi $a3, $a3, 1
j stringLoop
stringReturn:
addi $t8, $t8, -1 #size will hit null so we sub 1
move $v1, $t8
jr $ra
decimalToAny: # a1 = base, a2 = number
move $t0, $a2
li $t2, 0
divideLoop:
# divide number by base
div $t0, $a1
mflo $t0
mfhi $t1
#if bit is from 0-9
slti $t3, $t1, 10
beq $t3, 0, convLetter
convNum:
addi $t1, $t1, 48
j store
#if bit is over 9
convLetter:
addi $t1, $t1, 55
j store
store:
sw $t1, array($t2) #store offset in array
#add 4 to offset for next bit
addi $t2, $t2, 4
#dividing
bne $t0, $zero, divideLoop #checks qoutient
print:
addi $t2, $t2 -4
lw $a0, array($t2) # t2 = size -1 (in offset form)
li $v0, 11
syscall
bgtz $t2, print
j exit
#only takes in binary and does all conversions
binaryToOther: # a1 = convert to base a3 = number
addi $t0, $zero, 0
addi $t5, $zero, 1000
beq $a1, 8, binaryLoop
addi $t5, $zero, 10000
binaryLoop:
rem $s0, $a3, $t5
#store result in array
sw $s0, result($t0)
#adding offset
addi $t0, $t0, 4
#update number
div $a3, $a3, $t5
#repeat loop
bne $a3, $zero, binaryLoop
biprint:
addi $t0, $t0, -4
lw $t1, result($t0)
#exp
addi $t2, $zero, 0
addi $t3, $zero, 0 # current number
converting:
rem $s1, $t1, 10 # gets digit
sllv $s1, $s1, $t2
add $t3, $t3, $s1
#increase exp
addi $t2, $t2, 1
#update number
div $t1, $t1, 10
bne $t1, $zero, converting
bgt $t3, 9, cLetter
addi $t3, $t3, 48
j asciiPrint
cLetter:
addi $t3, $t3, 55
asciiPrint:
li $v0, 11
move $a0, $t3
syscall
bgtz $t0, biprint
jr $ra
# int valueGet
valueGet:
#a2 = char s
addi $sp, $sp, -4
sw $ra, ($sp)
# s - 48 = $v1
subi $v1, $a2, 48
#check if value is A-F
# if $v1 < 16 then we subtract by 7
bgt $v1, 16, letter
j vReturn
letter:
addi $v1, $v1, -7
j vReturn
vReturn:
lw $ra, ($sp)
addi $sp, $sp, 4
jr $ra
pow: # $a1 = base, $a2 = exponent
addi $t4, $zero, 1 #timesAround
addi $t5, $zero, 0 #for loop
ploop:
# if $t5 = $a2 return
beq $t5, $a2, leaving
#timesAround *= base
mulu $t4, $t4, $a1
addi $t5, $t5, 1
j ploop
leaving:
#return
move $v0, $t4
jr $ra