Question

Fellow SO users,

If I have a value, say 1010 saved in memory, how do I load each of the bits starting from the MSB to the LSB one at a time? By this I mean,

while loop
    load $t1 with 1
    shift some number by the amount indicated by the value in $t1 (1)
    load $t1 with 0
    shift some number by the amount indicated by the value in $t1 (0)
    do the same until you load the LSB which is 0
    start again at MSB (rotate right?)
end loop

I need some tips on how to declare this value in memory in the .data segment first. Do I declare this as:

value: .byte 00001010

And how do I load the individual bits into registers?

Kind regards

Was it helpful?

Solution

You can do this with the SLT instruction described here. Here is some pseudocode:

lbu $s0, value     # load the byte
sll $s0, $s0, 24   # shift it into the upper byte of $s0
for (i = 0; i < 8; i++) {
  # $t0 is set to 1 if the sign bit (i.e. bit 31) of $s0 is set
  slt  $t0, $s0, $zero   
  # $t0 now holds the value of the ith bit of value
  . . . .          # do something with $t0
  sll $s0, $s0, 1  # shift on to the next bit
}

Implementing the for loop in assembler is left as an exercise for the reader.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top