Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 242
Mithrax Avatar asked Apr 15 '09 02:04

Mithrax


People also ask

What is masking in MIPS?

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.

What is the size of 1 word for MIPS?

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.

How many bits is a byte MIPS?

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.

What does lb do in MIPS?

The MIPS ―load byte‖ instruction lb transfers one byte of data from main memory to a register.


3 Answers

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.

like image 70
Michael Avatar answered Nov 10 '22 09:11

Michael


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
like image 41
MadCoder Avatar answered Nov 10 '22 11:11

MadCoder


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.

like image 38
Lance Richardson Avatar answered Nov 10 '22 10:11

Lance Richardson