MIPS中的移位加法乘法,不带乘法 [英] Multiplication in MIPS by shifting and adding, without mult
本文介绍了MIPS中的移位加法乘法,不带乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码,但我不断收到算术溢出错误。我试图解决的问题是将两个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
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.
乘法:
#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(十进制)
推荐答案
以下是可能的实现。
与您的代码的差异:
32x32乘法生成64位结果。在32位MIPS上,结果必须拆分为两个寄存器
不是操作数左移,而是结果右移,这样会导致溢出。排出的位被保存并重新注入到结果的下部
使用Addu进行加法。数字是无符号的,如果没有无符号运算,可能会发生溢出
以"do While"形式更改了循环。循环计数器递减
显示的结果目前分为两部分。如果设置了LSB(并被视为显示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 "
and 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
这篇关于MIPS中的移位加法乘法,不带乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文