I have the following code, but I keep receiving an Arithmetic Overflow error. The problem that I am trying to solve is multiplying two 31-bit numbers together and storing the results in $t2 $t3 and printing out the correct result. It seems that I have coded for two numbers to be multiplied and the end result is a 31-bit number.
I would love to narrow down where I feel like I am going wrong, but I honestly cannot see where and what I need to change.
# program to multiply two 31 bit binary numbers (A & B),
# using the “shift and add” .
.data
# declare the variable lables of ASCII storage type.
prompt1: .asciiz "Enter number 1: "
prompt2: .asciiz "Enter number 2: "
result: .asciiz "The multiplication of two 31 bit binary numbers is: "
.text
main:
#prompt1.
li $v0, 4
la $a0, prompt1
syscall
#read number 1 and store in the register $t0
li $v0, 5
syscall
move $t0, $v0
#prompt2.
li $v0, 4
la $a0, prompt2
syscall
#read number 2 and store in the register $t1
li $v0, 5
syscall
move $t1, $v0
li $t2, 0 # The final result of the multiplication
#is saved into the register $t2
li $t3, 1 # Mask for extracting bit!
li $s1, 0 # set the Counter to 0 for loop.
multiply:
#if the Counter $s1 is equal to 31, then go the lable exit
beq $s1, 31, exit
and $s2, $t1, $t3
sll $t3, $t3, 1
beq $s2, 0, increment
add $t2, $t2, $t0
increment:
sll $t0, $t0, 1
addi $s1, $s1, 1
j multiply
exit:
#display the result string.
li $v0, 4
la $a0, result
syscall
#display the result value.
li $v0, 1
add $a0, $t2, $zero
syscall
li $v0, 10 # system call code for exit = 10
syscall # call operating sys
Sample input A: 1143330295 (Decimal) Sample input B: 999999223 (Decimal)
srl $t2 $t2 1andsrl $t3,$t3,1c/ reinject the lsb of $t4 in the msb of $t3 by shifting it by 31 and oring it with $t3. - Alain Merigot