Search code examples
assemblybit-manipulationmipscpu-registersmips32

MIPS instructions to extract a field from a register


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?

Solution

  • 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.