I need to obtain a hex input from the memory and then I need to convert it to its decimal equivalent. My approach was separating each of the hex values, for example, if the input was 0x0000abcd, then I wuld basically create a counter which would update when I store one hex digit. I would store one hex digit at a time by basically creating a loop which would basically divide the input by 10 (by subtracting 10) and then compare to see if the input is now less than or equal to 0x0000000f. If this was true then the counter would update and the result would be stored. Then my program loops back to the main function, checks the counter and then shifts the input by 4, 8, or 12 bits and then divides to obtain the next hex value. Basically once I had all the hex values I would multiply each by 16^x, x being their place in the input and then add them all up...but this doesn't work as the output is still in hex..
Main: lhu t0, 0x11000100
lhu s0, 0x11000100
addi s1, zero, 0
addi t3, zero, 0XF# check for the remainder
addi s1, zero, 0# set to zero so that it resets when main is called
addi s2, zero, 0
addi s3, zero, 0
addi s4, zero, 0
addi s5, zero, 0
addi s6, zero, 0
addi a1, s1, 0
addi a2, s2, 1
addi a3, s3, 2
addi a4, s4, 3
addi a5, s5, 4
addi a6, s6, 5
div: beq t4, zero, div10 # count = 1 then obtains lsb
beq t4, a2, div10_2 # count = 2 then obtains lsb+1
beq t4, a3, div10_3 # count = 3 then obtains lsb+2
beq t4, a4, div10_4 # count = 4 then obtains msb
beq t4, a5, multiply
j main
div10:
bleu t0, t3, store # store function stores each hex value
addi t0, t0, -0x10
j div10
div10_2:
srai t0, s0, 4 #shifts by 4 bits so that next hex digit is obtained
j div10
div10_3:srai t0, s0, 8 #shifts by 8 bits so that next hex digit is obtained
j div10
Div10_4: srai t0, s0, 12 #shifts by 12 bits so that next hex digit is obtained
j div10
store:
addi t4, t4, 1
beq t4, a2, store1
beq t4, a3, store2
beq t4, a4, store3
beq t4, a5, store4
store1:
sw t0, 0x11000120, t6
j main
store2:
sw t0, 0x11000140, t1
j main
store3:
sw t0, 0x11000160, t2
j main
store4:
sw t0, 0x11000180, t5
j main
multiply:#all hex values are multiplied and then added
lhu t1, 0x11000120
lhu t2, 0x11000140
lhu t3, 0x11000160
lhu s3, 0x11000180
addi t4, zero, 16
addi t5, zero, 256
addi t6, zero, 2047
addi t6, t6, 2047
addi t6, zero, 2
addi s0, zero, 0
addi s1, zero, 0
addi s2, zero, 0
mul1:
beq s0, t4, mul2
add t2, t2, t2
addi s0, s0, 1
j mul1
mul2:
beq s1, t5, mul3
add t3, t3, t3
addi s1, s1, 1
j mul2
mul3:
beq s2, t6, final
add s3, s3, s3
addi s2, s2, 1
j mul3
final:
add s4, t1, t2
add s5, s4, t3
add s6, s5, s3
sw s6, 0x11000120, s6
end:
User contributions licensed under CC BY-SA 3.0