MIPS - 加载单个位

时间:2013-03-21 11:13:20

标签: mips declaration bits

SO用户,

如果我有一个值,比如1010保存在内存中,我如何一次加载从MSB开始到LSB的每个位?我的意思是,

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

我需要一些关于如何在.data段中首先在内存中声明此值的提示。我是否将其声明为:

value: .byte 00001010

如何将各个位加载到寄存器中?

亲切的问候

1 个答案:

答案 0 :(得分:0)

您可以使用here所述的SLT指令执行此操作。这是一些伪代码:

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
}

在汇编程序中实现for循环留给读者练习。