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?
A mask is a special bit pattern that is constructed so that an AND or OR operation can be applied to a selected sequence of bits to isolate it. In our case, we want to construct a mask so that we can extract our single bit, i.e., so that the operation.
In the case of MIPS, a word is 32 bits, that is, 4 bytes. Words are always stored in consecutive bytes, starting with an address that is divisible by 4.
Each computer manufacturer has its own idea of what to call groupings larger than a byte. The following is used for MIPS chips. byte — eight bits. word — four bytes, 32 bits.
The MIPS ―load byte‖ instruction lb transfers one byte of data from main memory to a register.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With