2017-01-07 16 views
0

MIPSはビットを回転させずにシフトするだけであることを知ったので、この穴を掘ってMIPSの回転機能私がそれをテストした限り(以下のコードでは "シフト"という名前の関数)。基本的には、与えられた数の4つのMSBを格納し、それをLSBに変え、4ビットを左にシフトして、前のMSBのLSBをシフトした数につなぎます。アセンブリMIPS:擬似回転の10進数から32ビットへのバイナリ

Aaaannnd alakazam!数字は左に4ビット回転します。

私はこれを完全なバイナリで数字を印刷する限り、各回転の最後の4ビットをチェックすることによって動作させることを考えてきました。

のは、与えられた数は、以下のように見えるとしましょう:

aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii 

4ビットのために左に回転させることで、我々はaaaaの値を確認します

bbbb cccc dddd eeee ffff gggg hhhh iiii aaaa 

と確認し、回転を続けると、 bbbbの値を印刷:

cccc dddd eeee ffff gggg hhhh iiii aaaa bbbb 

私たちが最終決定するまでyと同じ番号に戻り、最後の4ビットを確認してください。iiii

。 。

aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii 

しかし、私はコンパイラがクラッシュするまで0を追加し続けるコードに問題があります。

.text 
main: 
li $v0, 5   #v0 = the given integer 
syscall 

move $t1, $v0   moving the integer to t1 

add $s1, $zero, $zero #s1 = counter 

shifting: 
    andi $t2, $t1, 0xF0000000 #t2 = the 4 MSB's that get pushed to the left 
    srl  $t3, $t2, 28  #turning them to LSB's 

    sll  $t4, $t1, 4   #shifting the integer 

    or  $t5, $t3, $t4  #$t5 = the pseudo-rotated number 


loop: 
    andi $t6, $t5, 0xF  #isolating the 4 new LSB's 
    beq $t6, 0xF, one   #print 1's where is necessary 

    li $v0, 1   #else print 0's 
    la $a0, 0 
    syscall 
j shifting 

next: 
    addi $s1, $s1, 1 
    beq  $s1, 32, exit #stop printing at 32 numbers 

one:    #printing the aces 
    li $v0, 1 
    la $a0, 1 
    syscall 
j shifting 

exit: 
li $v0, 10 
syscall 

私はこの事について頭をはがしてしまったようですが、私は実際にループについていけません。

私のコードで何が問題になっていますか?

+0

直接的な原因は、「次へ」は決して到達しないため、カウンターは増えないので、決して退場しないということです。また 'la $ a0、0/1'は意味をなさないので、1でない4ビットを出力する必要があります。したがって、beq $ t6、0xF、oneはどちらも意味がありません。最後に、あなたは回転する必要はありません。シフトはうまくいくでしょう。 PS:デバッガの使い方を学んでください。 – Jester

+0

@Jesterシフトした直後に次の関数を移動し、32で停止しますが、32 0を返します。私は – Coursal

+1

を微調整します。 '私は最近、MIPSがビットを回転させないことを知りましたが、それをシフトするだけです。古いMIPSのバージョンは回転する疑似命令を持っています。新しいMIPS ISAには、ハードウェアの回転命令があります。http://stackoverflow.com/q/24542657/995714 –

答えて

0

だから私は一瞬フォーカスのビットを失ってしまったが、私はそれが働いてしまった:

.text 
main: 
li $v0, 5   #v0 = the given integer 
syscall 

move $t1, $v0  #moving integer to t1 

add $s2, $zero, $zero #counter for all the 4bits 

shifting: 

    andi $t2, $t1, 0xF0000000 #t2 = the 4 MSB's that get pushed to the left 
    srl $t3, $t2, 28   #turning them to LSB's 

    sll  $t4, $t1, 4   #shifting the integer 

    or $t5, $t3, $t4  #$t5 = the pseudo-rotated number 

    andi $t6, $t5, 0xF  #isolating the 4 LSB's 

check: 

    beq  $s2, 8, exit  #32 bits = 8x 4bits 
    addi $s2, $s2, 1  #adding the counter for the 4bits 

    li $v0, 11   #spaces between 4bits 
    li $a0, ' ' 
    syscall 

    add $s1, $zero, $zero #counter for each bit in a 4bit 

bts: 
    andi $a0, $t6, 8   #4bit AND 8 
    beq  $a0, 8, one  #if a0 = 8 print 1 

    li $v0, 1   #else print 0 
    li $a0, 0 
    syscall 

next: 
    sll  $t6, $t6, 1  #shift the bit to the left 

    addi $s1, $s1, 1  #adding the counter for one 4bit 

    move $t1, $t5  #shift the pseudo-rotated number next time 

    beq $s1, 4, shifting #make sure the 4bit will have 4 bits 

one:   #function that prints 1's 
    li $v0, 1 
    li $a0, 1 
    syscall 
j next 

exit: 
li $v0, 10 
syscall 

私は時間があるとき、それは浮動小数点数のために動作させるためにしようとするでしょう。

関連する問題