如何在MIPS中编写无除法或除法指令的除法和余数代码?

问题描述 投票:0回答:2

我必须接受2个用户输入,然后找到产品,商和余数。我也不能使用乘法运算或除法运算符。

我有乘法代码:

L1:
add  $t2,$s1,$s0  #diving $s0 by $s1
bge  $s1, $s0, EXIT   # branch if ! ( i < k )
addi $s1, $s1, 1      # k++
add  $t2, $s1, $s0    # i = i * 2 
EXIT:

我将如何找到商和余数?我尝试只将所有add更改为sub,但没有运气。

mips
2个回答
3
投票

如果在循环中使用加法将两个数字相乘,则在循环中使用减法将它们相除。一些伪代码:

main:
    #initialize registers

loop:
    #dividend -= divsor
    #quotient++

    #temp = dividend - divsor
    #if temp < 0 jump to done
    #jump to loop

done:
    #remainder = temp

2
投票

使用以下命令获取商和余数

rem   d, s1, s2     #d = s1 % s2;  gives remainder
© www.soinside.com 2019 - 2024. All rights reserved.