mips司实施

发布于 2024-12-23 10:11:12 字数 1775 浏览 1 评论 0原文

我正在尝试在MIPS中实现除法算法,我应该执行以下操作:

  1. 余数和商位于同一个寄存器中,上半部分是余数,下半部分是商,该寄存器被初始化为被除数的值。
  2. 将余数寄存器左移 1 位
  3. ,然后从余数寄存器的余数部分减去除数,仅
  4. 检查 rem 是否小于 0,如果 rem < 0 0 则 rem = rem + 除数,如果 rem > 0,然后只放入第一位(最低有效位为1)
  5. 继续执行n次,其中n是除数的宽度,在我的例子中它是6位宽,所以循环是6次

这里是代码到目前为止,我已经写过,我试图首先对正操作数进行操作,然后想对签名操作数进行操作

# A program that divides two integers according to the approach described in Fig. 3.12

.data   # Data declaration section

.text

main:       # Start of code section 
li $s2, 4 # dividend, will als0 be used as the remainder register which will hold remainder and quotient initialized to dividend
li $s3, 2 # divisor

li $s4, 2 # another register to hold the dividend shifted 6 bits, for adding and subtracting dividend from remainder
sll $s4, $s4, 6

li $t0, 0 # counter of the loop

LOOP:   sll $s2, $s2, 1 # shift the remainder regitser by 1 bit to right
sub $s2, $s2, $s4 # subtract divisor from remainder part of the remainder register

slt $t1, $s2, $zero # to check if rem < 0
beq $t1, $zero, MORE # if rem no < 0 then branch to MORE label
nop

add $s2, $s2, $s4 # if rem < 0, to add the divisor to the remainder part of the remainder register

j LOOP # jump back to the loop
nop

MORE:   # if rem > 0, then do arithmetic right shift and place 1 as the 0th position
rol $s2, $s2, 1 # rotate the number to the left by 1 bit which is arithmetic right shift

j LOOP # jump back to loop
nop

addi $t0, $t0, 1 # adding 1 to the counter of the loop
slti $t1, $t0, 6 # checking if the loop condition is working or not
bne $t1, $zero, LOOP
nop

add $a0, $zero, $s2 # putting the result in regitser a0

li $v0, 1 # printing out the result
syscall

# END OF PROGRAM

有人可以检查我的代码,并告诉我哪里出错了。 谢谢

I'm trying to implement an algorithm of a division in MIPS, and I'm supposed to do he following:

  1. the remainder and the quotient are in the same register, the upper half is the remainder, and the lower half is the quotient, and this register is initialized to the value of the dividend.
  2. shift remainder register to left by 1 bit
  3. then subtract the divisor from the remainder part of the remainder register only
  4. check if rem is less than 0, if rem < 0 then rem = rem + divisor, if rem > 0, then just put the first bit (least significant bit as 1)
  5. keep doing this n-times, where n is the width of the divisor, in my case its 6-bit wide, so the loop is 6 times

here is the code I've written so far, i am trying to do it on positive operands first, and then want to make it for signed operands

# A program that divides two integers according to the approach described in Fig. 3.12

.data   # Data declaration section

.text

main:       # Start of code section 
li $s2, 4 # dividend, will als0 be used as the remainder register which will hold remainder and quotient initialized to dividend
li $s3, 2 # divisor

li $s4, 2 # another register to hold the dividend shifted 6 bits, for adding and subtracting dividend from remainder
sll $s4, $s4, 6

li $t0, 0 # counter of the loop

LOOP:   sll $s2, $s2, 1 # shift the remainder regitser by 1 bit to right
sub $s2, $s2, $s4 # subtract divisor from remainder part of the remainder register

slt $t1, $s2, $zero # to check if rem < 0
beq $t1, $zero, MORE # if rem no < 0 then branch to MORE label
nop

add $s2, $s2, $s4 # if rem < 0, to add the divisor to the remainder part of the remainder register

j LOOP # jump back to the loop
nop

MORE:   # if rem > 0, then do arithmetic right shift and place 1 as the 0th position
rol $s2, $s2, 1 # rotate the number to the left by 1 bit which is arithmetic right shift

j LOOP # jump back to loop
nop

addi $t0, $t0, 1 # adding 1 to the counter of the loop
slti $t1, $t0, 6 # checking if the loop condition is working or not
bne $t1, $zero, LOOP
nop

add $a0, $zero, $s2 # putting the result in regitser a0

li $v0, 1 # printing out the result
syscall

# END OF PROGRAM

Can someone please check my code, and tell me where did I go wrong.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风尘浪孓 2024-12-30 10:11:12

请参阅此问题并回答 C 中的参考代码。有符号整数的直接除法相当复杂。如果您足够勇敢,请查阅布斯除法算法

See this question and answer for a reference code in C. Direct division of signed integers is rather complicated. If you are brave, look up Booth's Division Algorithm.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文