I am working on a assignment where I have to input two 64-bit unsigned numbers in decimal and then perform addition and multiplication on them, and output the results in decimal.
Result of addition is stored in $s4 for low half of the number and $s5 for the high half. I have a problem with converting these two values into one string that contains the result in decimal.
After some research I've found out that Method:
You should generate the string from right to left, each time dividing the 2-word number by 10, calculating the remainder and quotient. The remainder is converted to the next character and stored in a string for printing later. The quotient is rewritten into the two words and used in the next iteration.
How to perform divison of 64-bit number by 10 Let A (64 bits) be composed of B and C, where B contains the high end 32 bits and C the low end 32 bits. That is, A = B(2^32) + C. Use divu and remu instructions on B and C to determine the overall quotient and remainder for the 64-bit number A divided by 10.
Here is the procedure that I came up with:
itoa:
li $t0, 0
li $t3, 0
li $t2, 0
li $t1, 0
li $t5, 0
li $t6, 0
li $t8, 0
addiu $t5, $zero, 6 # 2%32%10
addiu $t0,$zero,10 # t0=10
lw $t1, exponent #2^32/10
addu $t2,$t2, $a0 #low half of addition
addu $t3,$t3, $a1 #high half of addition
itoa_loop:
#B/10
beqz $t3, skip_high
divu $t3, $t0
mflo $t3 #quotient of B
mfhi $t4 #remainder of B
multu $t4, $t5 #multiplying remainders of B/10 and 2^32/10
mflo $t6
skip_high:
#C/10
divu $t2, $t0
mflo $t2 #quotient of C
mfhi $t4 #remainder of C
addu $t8, $t4, $t6 #summing up remainders
addi $t8, $t8, 48 #convert to ascii
sb $t8, ($t9) #t9 is the output string
addi $t9, $t9, 1
#2^32/10
divu $t1, $t0
mflo $t1 #2^32/10 quotient
mfhi $t5 #2^32%10 remainder
bne $t2, $zero, itoa_loop
sb $zero, ($t9)
jr $ra
It works for 32-bit numbers only unfortunately.