首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >移位带来的除法问题

移位带来的除法问题
EN

Stack Overflow用户
提问于 2013-03-26 09:03:40
回答 1查看 2.5K关注 0票数 0

我在使用MIPS实现除法算法时遇到了一些问题,我假设这与我移位和设置最低有效位的方式有关,但我不完全确定。

算法是这样的:

1)从余数寄存器中减去除数寄存器,并将结果放入余数寄存器。

2a)如果余数>=为0,则将商寄存器向左移位,将新的最右边的位设置为1

2b)如果余数<0,则将除数寄存器添加到余数寄存器,并将结果存储在余数寄存器中(以恢复余数之前的值)。将商寄存器向右移位,将最右边的位设置为0。

3)将除数寄存器右移一位

4)执行此n+1重复。因为我们做的是无符号的8位除法,所以我假设这意味着9次重复

编辑:为了简单起见,我只设置了$t0 = 12和$t1 = 5。

代码语言:javascript
复制
    .data
    quotient_a:     .asciiz "Quotient (a/b): "
    remainder_a:        .asciiz "Remainder (a/b):"
    quotient_b:     .asciiz "Quotient (b/a): "
    remainder_b:        .asciiz "Remainder (b/a):"
    error:          .asciiz "Sorry, divide-by-zero not allowed"
    new_line:       .asciiz "\n"


    .text
main:
    li $s0, 8   #8 bit

    li $t0, 12
    li $t1, 5

    ## Quotient  A message
    la $a0, quotient_a      # load the addr of display_sum into $a0.
    li $v0, 4           # 4 is the print_string syscall.
    syscall             # do the syscall.

    ##Computer the Quotient and remainder
    ## $t0 = a     $t1 = b
    ## $t0 = dividend       $t1 = divisor
    li $t2, 0       ##Quotient
    li $t3, 0       ##Remainder
    li $t9, 0       ##i
    li $s0, 9       ##count

loop:
    sub $t3, $t3, $t1
    blt $t3, 0, less_than
    ## >= 0
    sll $t2, $t2, 1
    addi $t2, $t2, 1
    j cont

less_than:
    add $t3, $t3, $t1
    sll $t2, $t2, 1

cont:   
    srl $t1, $t1, 1 
    addi $t9, $t9, 1
    bne $t9, $s0, loop



    ## print the quotient 
    move $a0, $t2
    li $v0, 1           # load syscall print_int into $v0.
    syscall             # make the syscall.

    ## new line
    la $a0, new_line        # load the addr of new_line into $a0.
    li $v0, 4           # 4 is the print_string syscall.
    syscall 

    ## Remainder A message
    la $a0, remainder_a         # load the addr of display_sum into $a0.
    li $v0, 4           # 4 is the print_string syscall.
    syscall             # do the syscall.

    ## print the remainder 
    move $a0, $t3
    li $v0, 1           # load syscall print_int into $v0.
    syscall             # make the syscall.

    #Exit Program
    li $v0, 10          # syscall code 10 is for exit.
    syscall             # make the syscall.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-26 19:15:25

对算法的描述在我看来有点不对劲。这是在C中工作的8位除法算法的样子:

代码语言:javascript
复制
unsigned char Q,R;
unsigned char N=12,D=5;
int i;

Q = 0;
R = 0;
for (i = 0; i < 8; i++) {
    R <<= 1;
    R |= (N & 0x80) >> 7;
    N <<= 1;
    Q <<= 1;
    if (R >= D) {
        R -= D;
        Q |= 1;
    }
}

将其转录成MIPS汇编应该不会太困难。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15627589

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档