Mithrax
Mithrax

Reputation: 7807

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

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?

Upvotes: 6

Views: 21349

Answers (3)

MadCoder
MadCoder

Reputation: 1352

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

Upvotes: 4

Lance Richardson
Lance Richardson

Reputation: 4610

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.

Upvotes: 2

Michael
Michael

Reputation: 55445

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.

Upvotes: 6

Related Questions