How do I access the state of individual bits of a word in MIPS?

≯℡__Kan透↙ 提交于 2019-12-10 17:23:18

问题


I'm writing a program and I need to determine if bits 3 and 6 are set. I know that I can rotate a word or left/right shift it.

But how do I access individual bit's state? Do I use a bitwise operator like and/xor?


回答1:


You would do a bitwise and with 0x08 and 0x40 (presuming bit 0 is the lowest order bit). You would use the andi instruction to do this.

If $t0 is the value you want to test:

andi $t1, $t0, 0x08
andi $t2, $t0, 0x40

$t1 will be non-zero if bit 3 is set, $t2 will be non-zero if bit 6 is set.




回答2:


Yes, bitwise operators are what you use. You can AND with a bitmask that has only bits 3 and 6 set. Then do a comparison to zero.

something like (I haven't done assembler in a long time):

and     r2, r1, 0x48  # r2 = r1 & 0x48
cmp     r2, 0x48
jz     zzzzzz   #jmp to zzzzz if bits 6 and 3 are set



回答3:


One technique for testing a single bit in MIPS assembly is to shift the desired bit into the most-significant bit position and use bltz/bgez to test the state of the bit. This saves an instruction in cases where the andi instruction can't be used to select the desired bit.



来源:https://stackoverflow.com/questions/750013/how-do-i-access-the-state-of-individual-bits-of-a-word-in-mips

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!