У меня есть следующий код, но я продолжаю получать сообщение об ошибке арифметического переполнения. Проблема, которую я пытаюсь решить, заключается в перемножении двух 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 (десятичный)
srl $t2 $t2 1
иsrl $t3,$t3,1
c/ повторно ввести младший бит $t4 в старшего разряда $t3, сдвинув его на 31 и объединив его с $t3. - person Alain Merigot   schedule 27.03.2019