Problem Set 2

from Computer Organization and Design, Fourth Edition (Revised Printing)

Problems:

  1. Some computer have explicit instructions to extract an arbitrary field from a 32-bit register and to place it in the least significant bits of a register. The figure below shows the desired operation:

    extracting an aribitrary bit sequence

    Find the shortest sequence of MIPS instructions that extracts a field for the constant values i = 5 and j = 22 from register $t3 and places it in register $t0. (Hint: it can be done in two instructions.)

  2. Construct a control flow graph (like the one shown in Figure 2.9) for the following section of C/C++/Java code:

    for( i = 0; i < x; i = i + 1 ) {
       y = y + i;
    }
    
  3. Add comments to the following MIPS code and describe in one sentence what it computes. Assume that $a0 and $a1 are used for the input and both initially contain the integers a and b, respectively. Assume that $v0 is used for the output.

            add   $t0, $zero, $zero
    loop:   beq   $a1, $zero, finish
            add   $t0, $t0, $a0
            addi  $a1, $a1, -1
            j     loop
    finish: addi  $t0, $t0, 100
            add   $v0, $t0, $zero
    
  4. The following code fragment processes two arrays and produces an important value in register $v0. Assume that each array contains 2500 words indexed 0 through 2499, that the base addresses of the arrays are held in $a0 and $a1, respectively, and that their sizes are held in $a2 and $a3, respectively. Add comments to the code and describe in one sentence what this code does. Specifically, what will be returned in $v0?

            sll   $a2, $a2, 2
            sll   $a3, $a3, 2
            add   $v0, $zero, $zero
            add   $t0, $zero, $zero
    outer:  add   $t4, $a0, $t0
            lw    $t4, 0($t4)
            add   $t1, $zero, $zero
    inner:  add   $t3, $a1, $t1
            lw    $t3, 0($t3)
            bne   $t3, $t4, skip
            addi  $v0, $v0, 1
    skip:   addi  $t1, $t1, 4
            bne   $t1, $a3, inner
            addi  $t0, $t0, 4
            bne   $t0, $a2, outer