Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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

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.

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