views:

303

answers:

3

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?

+3  A: 

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.

Michael
+2  A: 

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
MadCoder
I don't do mips, but won't that jump if bits 3 *or* 6 are set?
dmckee
You're right. I was being sloppy. It should be a comparison to 0x48 and then jz instead of jnz.
MadCoder
If you fixed it I would vote you up.
Steve Rowe
Is it proper for me to edit my answer to fix the problem after people have commented on it?
MadCoder
+1  A: 

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.

Lance Richardson