Frage

I'm trying to solve a problem, but at first I'm testing rol instruction, and I'm totally sure I'm doing something very silly which makes my test not correct.

Here's a snippet:

    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

What I'm basically trying to do, is to rotate the MSB of my t0 to the LSB and then ANDing it with 1. However, what I'm getting is 0 all the time.

I hope someone could enlighten me about my mistake and how to fix it.

War es hilfreich?

Lösung

MSB of 101 when represented on 32 bits is zero, so a zero result is correct. If you load for example 0x80000000 you will get 1 as result. Note that rol is a pseudoinstruction that the assembler will replace with srl; sll; or sequence so depending on what you really want to do, it might be more efficient do use the real instructions as appropriate. For example in your case, a single srl $t3, $t0, 31 will give the same result as the rol; and sequence.

Andere Tipps

I don't see any jump to loop in your code. How can the loop run?

If the whole code below the loop label is the loop content then you're rotating without storing the result to $t0, so the loop always return (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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top