By now you know that machine language is the pattern of bits a CPU uses to encod
ID: 3597301 • Letter: B
Question
By now you know that machine language is the pattern of bits a CPU uses to encode its instructions. Instructions are usually encoded as bitfields in order to pack a lot of information into a small number of bits.
MIPS uses a few different instruction formats in order to encode its instructions. No matter what format, all MIPS instructions are 32 bits.
The one you'll be looking at today is the I-type format, where "I" stands for "immediate." This is used to encode instructions with two registers and an immediate such as li, addi, lw, and beq.
Here's how it looks:
The bit numbers show the numbers of the first and last bits, inclusive, of each field.
The opcode field says which instruction this is.
The rs and rt fields encode the two registers used.
The immediate field is the immediate value (the number in the instruction).
So for example, addi t0, s1, 123 will be encoded as:
opcode = 8 (the designers decided that 8 means addi)
rs = 17 (s1 is register 17)
rt = 8 (t0 is register 8)
immediate = 123
Part 1: Bitfields are fun
Answer the following in your text file using the diagram above.
1. How many bits are in each field (opcode, rs, rt, and immediate)?
Be careful, it's easy to mis-count.
2. Compute the mask for each field in hexadecimal.
Remember, the mask is the special value that you AND with after shifting right.
The TAs said to compute the mask before shifting. I will accept either answer.
It's based on the number of bits in the field.
The mask for rs and rt will be the same.
...unless you compute the mask before shifting, in which case they will differ.
3. I'm writing a C or Java program, and I have a variable inst which holds an encoded MIPS instruction. I want to take it apart into 4 variables, one for each field. Show how I can do that using shifting and masking.
I showed how to do this on slide 20 of the "Bitfields and Logic Basics" lecture.
4. Now I want to put those 4 variables (the opcode, rs, rt, and immediate) back together into a single encoded instruction. Show how I can do that.
Slide 16 of that lecture.
5. I have a variable inst which holds an encoded MIPS instruction. I want to set the rt field to 0 without changing the other fields. I can do this in one line: inst = inst & (some value); Figure out what the (some value) is, and write it as C/Java code that will calculate that value.
Hint: it has something to do with the mask.
Another hint: try writing things out in binary.
Yet another hint: look up what ~ (tilde) does in C/Java.
6. After problem 5, I now have an instruction with the rt field set to 0. Now I want to put the value of the variable new_reg into the rt field without changing the other fields. I can do this as: inst = inst | (something); Figure out what the (something) is, and write it as C/Java code.
It will use new_reg.
31 26 25 21 20 16 15 0 opcode rs rt immediateExplanation / Answer
Hi,
Please find answers for first 4 questions:
1. Below are total number of bits in each bit field:
immidiate = 16 (0-15)
rt = 5 (16-20)
rs = 5 (21-25)
opcode = 6 (26-32)
2. Below is the way to compute mask for each field. I have used AND before shifting, but it can also be done either way.
Suppose inst contains the 32bit encoded MIPS instruction. Then the mask of four fields can be calculated as below with the help of ANDing and shifting
immidiate = (inst & 0x0000ffffUL);
rt = (inst & 0x001f0000UL) >> 16;
rs = (inst & 0x01f00000UL) >> 21;
opcode = (inst & 0xf3000000UL) >> 26;
3. Below is the C snippet to split the encoded MIPS instruction into 4 variables.
unsigned long inst = 43259621;
unsigned long immidiate = 0;
unsigned char rt = 0;
unsigned char rs = 0;
unsigned char opcode = 0;
immidiate = (inst & 0x0000ffffUL); //AND first 16 bits, mask all other
rt = (inst & 0x001f0000UL) >> 16; //AND 16-20 bits with 1f, mask all other
rs = (inst & 0x01f00000UL) >> 21; //AND 21-25 bits with 1f, mask all other
opcode = (inst & 0xf3000000UL) >> 26; //AND 26-31 bits with f3, mask all other
4. Below is the C snippet to combine these four variables in the encoded MIPS instruction.
unsigned long inst2 = (unsigned long) opcode << 26 | rs << 21 | rt << 16 | immidiate;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.