2011-12-26 16 views
0

私はMIPSでの除算のアルゴリズムを実装しようとしている、と私は彼が次の操作を行うことになってる:、上半分MIPS分割実装

  1. 余りと商が同じレジスタであります下半分は商であり、このレジスタは被除数の値に初期化されます。場合シフト残りは、REMが0未満である場合、もしREM < 0次にREM = REM +除数剰余レジスタの残りの部分からのみ
  2. チェック除数を減算した後、1ビット
  3. 左に登録
  4. 最初のビット(最下位ビットを1にする)を入れてください。
  5. ここで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 

誰かが自分のコードをチェックして、どこが間違っていたか教えてください。 ありがとう

+0

デバッガでこれをステップ実行して、その動作が期待どおりでない場所を見つけましたか? –

+0

私はpcspimでデバッガを使用する方法を知っていませんが、どうすればそれをデバッグするか教えてくれれば嬉しいです –

+2

この特定のデバッガの使い方はわかりませんが、さもなければ、あなた自身でこの種の問題を解決することは事実上不可能になるでしょう(単にコードを見たり、他の人にコードを凝視してもらうなど)。デバッガの使い方を理解したら、問題の特定は容易でなければなりません。 –

答えて

0

this questionを参照してください。Cでの参照コードについては、答えます。符号付き整数の直接的な除算はかなり複雑です。あなたが勇気があれば、ブースの分割アルゴリズムを参照してください。

関連する問題