0

MIPSで除算アルゴリズムを実装するのに問題があります。これは、シフトして最下位ビットを設定する方法に関係していると思いますが、完全にはわかりません。

アルゴリズムは次のようになります。

1)剰余レジスタから除数レジスタを減算し、その結果を剰余レジスタに配置します。

2a)余りが0より大きい場合は、商レジスタを左にシフトし、新しい右端のビットを1に設定します。

2b)剰余が0未満の場合は、除数レジスタを剰余レジスタに追加し、結果を剰余レジスタに格納します(剰余を以前の値に戻すため)。商レジスタを右にシフトし、右端のビットを0に設定します。

3)除数レジスタを右に1ビットシフトします

4)これをn+1回繰り返します。符号なし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.

4

1 に答える 1

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アセンブリに転写するのはそれほど難しいことではありません。

于 2013-03-26T11:15:25.220 に答える