У меня есть следующий код, но я продолжаю получать сообщение об ошибке арифметического переполнения. Проблема, которую я пытаюсь решить, состоит в умножении двух 31-битных чисел и сохранении результатов в $ t2 $ t3 и распечатке правильного результата. Кажется, я кодировал два числа, которые нужно умножить, а конечный результат - 31-разрядное число.
Мне бы хотелось сузиться, где я чувствую, что я иду не так, но я, честно говоря, не вижу, где и что мне нужно изменить.
# 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
главный:
#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.
умножения:
#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
прибавка:
sll $t0, $t0, 1
addi $s1, $s1, 1
j multiply
выход:
#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
Пример ввода A: 1143330295 (десятичный)
Пример ввода B: 999999223 (десятичное число)