2013-03-26 46 views
0

我在使用MIPS实现除法算法时遇到了一些麻烦,我假设它与我正在移位并设置最不重要位的方式有关,但我不是完全确定。使用移位除法的问题

该算法是这样的:

1)中减去从所述余数寄存器除数寄存器并将结果置于在余数寄存器。

2A)如果余> = 0,商寄存器左移时,新的最右边的位设置为1

2B)如果余< 0,除数寄存器添加到该余数寄存器和存储剩余寄存器中的结果(恢复先前的剩余值)。 Shifht商寄存器到右侧,最右侧的位设置为0。

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

4)执行此n + 1个repititions。由于我们正在做无符号的8位除法,我会假设这意味着9个回复

编辑:为了使这更简单,我只设置$ t0 = 12和$ t1 = 5。然而,对于商我得到63和余下我得到0


    .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. 

回答

1

该算法的说明看起来有点关闭给我。这是一个在C工作的8位除法算法是这样的:

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汇编应该不会太困难。

+0

(N&0x80)是指什么?之后我会转向右边的那个7吗? – richard008 2013-03-27 01:48:48

+0

这是一个按位AND与'1 << 7'(即0x80)。是的,你会在AND之后立即转移它。什么'R | =(N&0x80)>> 7;'是将N的第7位拷贝到R的第0位。 – Michael 2013-03-27 05:54:59