Assembly 汇编MIPS:带伪旋转的十进制到32位二进制

Assembly 汇编MIPS:带伪旋转的十进制到32位二进制,assembly,bit-manipulation,mips,bit-shift,Assembly,Bit Manipulation,Mips,Bit Shift,我最近发现MIPS不旋转位,只移动位,所以我一直在挖这个洞,为MIPS制作一个类似旋转的函数,该函数在我测试时就可以工作(下面代码中名为“移位”的函数)。基本上,它存储给定数字的4个MSB,将其转换为LSB,将数字4位向左移位,然后将前一个MSB的转换LSB与移位后的数字连接起来 安南和阿拉卡萨姆!数字向左“旋转”4位 所以我一直在考虑通过检查每一次旋转的最后4位,将其工作到以完全二进制打印一个数字 假设给定的数字如下所示: aaaa bbbb cccc dddd eeee ffff gggg

我最近发现MIPS不旋转位,只移动位,所以我一直在挖这个洞,为MIPS制作一个类似旋转的函数,该函数在我测试时就可以工作(下面代码中名为“移位”的函数)。基本上,它存储给定数字的4个MSB,将其转换为LSB,将数字4位向左移位,然后将前一个MSB的转换LSB与移位后的数字连接起来

安南和阿拉卡萨姆!数字向左“旋转”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
直到我们最终得到相同的数字,我们开始检查最后4位,
iii

。 .

但我的代码一直存在问题,不断添加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
看来我对这件事想得太多了,真的跟不上循环


我的代码出了什么问题?

所以我暂时失去了一点注意力,但我让它正常工作了:

.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

当我有时间的时候,我会尝试让它对浮点数起作用。

直接原因是
next
当然永远不会到达,所以你的计数器永远不会增加,你永远不会退出。另外,
la$a0,0/1
没有意义,您需要打印4位而不是1位,因此
beq$t6,0xF,1
也没有意义。最后,你不需要旋转,一个移位就可以了。PS:学习使用调试器。@Jester我在移位后移动了下一个函数,它在32处停止,但它给了我32个0。我将继续调整
我最近发现MIPS不旋转位,只移动它们
旧的MIPS版本有旋转伪指令,不需要手动执行。较新的MIPS ISA具有硬件旋转指令
.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