1
votes

The answer in the solutions manual seems wrong for this bitfield extraction problem. My issues with it are posted below.


Here is the setup: Exercise 2.14.1a

The question posed is:

Find the shortest sequence of MIPS instructions that extracts a field from $t0 for the constant values i = 22 and j = 5 and places the field into $t1 in the format shown in the data table.

The solutions manual gives this as the answer:

lui $t1, 0x003f
ori $t1, $t0, 0xffe0
and $t1, $t0, $t1
srl $t1, $t1, 5

I have 2 questions:

  1. Zeros to the right. How does this proposed answer ensure that all bits to the right of "Field" in register $t1 are all zeroes?
  2. Shift Right Logical. Isn't the last instruction supposed to be sll $t1, $t1, 10 instead?
1
2. Yes, I think it needs to end with a left-shift to put the field at the top of the register. One easy way to do this would be right shift the field to the bottom, then left shift to the top. Unless shift instructions are really slow on MIPS, two shifts should be better than 4 instructions. - Peter Cordes
Thanks, that's exactly what my answer was before I checked the solutions. Thanks for the edits as well. - Nadim Hussami

1 Answers

2
votes

It's pretty obvious that the answer in the solutions manual is less than ideal.

  1. Zeros to the right. How does this proposed answer ensure that all bits to the right of "Field" in register $t1 are all zeroes?

With the right combination of andi and sll, we can zero all the bits we need to zero.

  1. Shift Right Logical. Isn't the last instruction supposed to be sll $t1, $t1, 10 instead?

It's quite clear that this should be a sll operation instead of a srl operation.