我有以下代码,但我一直收到算术溢出错误。我试图解决的问题是将两个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(十进制)
这是一个可能的实现。
与您的代码的差异:
显示的结果目前分为两部分。如果设置了LSB,则可能会出现不正确的显示(并且被显示为由int int syscall签名的负面),但大多数mips模拟器无法显示大的无符号。
# 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: "
result2: .asciiz "\nand the upper part of result 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 64 bits
# MSB part is in register $t2
li $t4, 0 # and LSB part of result is in $t4
li $t3, 1 # Mask for extracting bit!
li $s1, 32 # set the Counter to 32 for loop.
multiply:
and $s2, $t1, $t3
beq $s2, 0, increment
addu $t2, $t2, $t0
increment:
sll $t3, $t3, 1 # update mask
##sll $t0, $t0, 1 # useless, we srl result instead
andi $s4, $t2,1 # save lsb of result
srl $t2,$t2,1 # t2>>=1
srl $t4,$t4,1 # t4>>=1
sll $s4,$s4,31
or $t4,$t4,$s4 # reinject saved lsb of result in msb of $t4
addi $s1, $s1, -1 # decrement loop counter
#if the Counter $s1 reaches 0 then go the label exit
beq $s1, $zero, exit
j multiply
exit:
#display the result string.
## must be changed to take into account the 64 bits of the result
## but AFAIK, there is no syscall for that
## can be done in two steps t4 and t2
## and result is t4+2**32*t2
#display the result value.
li $v0, 4
la $a0, result
syscall
li $v0, 1 # if using mars replace 1 by 36 to print as an unsigned
add $a0, $t4, $zero
syscall
li $v0, 4
la $a0, result2
syscall
li $v0, 1 # if using mars replace 1 by 36 to print as an unsigned
add $a0, $t2, $zero
syscall
li $v0, 10 # system call code for exit = 10
syscall # call operating sys