首页 文章

汇编MIPS旋转ROL代码

提问于
浏览
0

我'm trying to solve a problem, but at first I' m测试 rol 指令,我做了一些非常愚蠢的事情,这使我的测试不正确 .

这是一个片段:

li $t0, 101 ## our data 
    li $t1, 32 ## our loop because we will rotate 32 times later
loop: 
    ## whatever the bit is, just print it or put it inside the asciiz
    ## and print it later, after we finish rotating,
    ## so we rotate starting with the first bit,
    ## and we put it as the first bit lsb

    ## first we rotate##
    rol $t2, $t0, 1 ## rotate and store the new rotated inside $t7
    and $t3, $t2, 1 ## now we AND to check our lsb if it one or not

    li $v0, 1
    move $a0, $t3 ## Print the result of AND
    syscall

我基本上要做的是将我的 t0 的MSB旋转到LSB,然后用1对它进行AND运算 . 但是,我得到的是0 .

我希望有人可以告诉我我的错误以及如何解决它 .

谢谢!

2 回答

  • 0

    我没有看到你的代码中跳转到 loop . 循环怎么运行?

    如果 loop 标签下面的整个代码是循环内容,那么你是在旋转而不将结果存储到$ t0,所以循环总是返回 (101 rol 1) and 1

    li $t0, 101 ## our data
    ## where did you store the new rotated value to $t0? If not, it's always 101
    rol $t2, $t0, 1 ## $t2 = $t0 rol 1 = 101 rol 1 = 202
    and $t3, $t2, 1 ## $t3 = $t2 and 1 = 202 and 1 = 0
    
  • 0

    当以32位表示时, 101 的MSB为零,因此零结果是正确的 . 如果您加载例如 0x80000000 ,结果将得到 1 . 请注意 rol 是一个伪指令,汇编器将用 srl; sll; or 序列替换,因此根据您真正想要做的事情,使用实际指令可能会更有效 . 例如,在您的情况下,单个 srl $t3, $t0, 31 将提供与 rol; and 序列相同的结果 .

相关问题