【发布时间】:2017-01-07 23:01:10
【问题描述】:
我最近发现 MIPS 不旋转位,而只是移动它们,所以我一直在挖这个洞,为 MIPS 制作一个类似旋转的函数,就我测试它而言(函数名为“shifting”在下面的代码)。基本上,它存储给定数字的 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 位,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
似乎我对这件事想得太多了,而且我真的跟不上循环。
我的代码有什么问题?
【问题讨论】:
-
直接原因是
next当然永远不会到达,所以你的计数器永远不会增加,你永远不会退出。此外,la $a0, 0/1没有意义,您需要打印 4 位而不是 1,因此beq $t6, 0xF, one也没有意义。最后,你不需要轮换,换班就可以了。 PS:学习使用调试器。 -
@Jester 我在移动后立即移动了下一个函数,它在 32 处停止,但它给了我 32 个 0。我会继续调整
-
I recently found out that MIPS does not rotate bits, but only shifts them旧的 MIPS 版本确实有旋转伪指令,无需手动执行。较新的 MIPS ISA 具有硬件旋转指令 stackoverflow.com/q/24542657/995714
标签: assembly bit-manipulation mips bit-shift