CS 354, version A
Spring 2006
Answer Key
Exam 2

No electronic devices may be used while taking this exam. Examples of devices not allowed are calculators, pagers, cell phones, wrist calculators/computers, laptop computers, pocket computers. Each student is allowed one 8.5 by 11 inch sheet of paper with handwritten notes. The notes may be on both sides of the paper.

Show all work, and do any/all calculations on the exam. Extra scratch paper may not be used.

Exam Score
Q1 = _____ /   8
Q2 = _____ /   6
Q3 = _____ /   5
Q4 = _____ / 10
Q5 = _____ / 20
Total = _____ / 49







Question 1 (8 points)
Part A
For each of the four listed addressing modes, give a single MIPS assembly language instruction that implies the use of the given addressing mode.

  1. base displacement
    lw $8, 4($9)
    (circle the second operand)
  2. register
    add $8, $9, $10
    (circle any operand)
  3. PC relative
    beq $8, $9, anywhere
    (circle the third operand)
  4. direct
    sw $8, X
    (circle the second operand)
Part B
For the instructions you gave in Part A, circle the operand that implies the use of the given addressing mode.

Question 2 (6 points)
Fill in appropriate portions of the following diagram of memory with decimal values, showing what memory looks like after the following MIPS assembly language code fragment is executed. The memory location with the X beside it identifies the address associated with variable X. Each box of memory is the size of an integer.

VERSION A:


                                                   smaller addresses
    li   $8, -1                       |---------|
    li   $9,  3                       |         |
    la   $10, X                       |---------|
    sw   $8, X                        |         |
    sw   $9, 4($10)                   |---------|
    lw   $11, ($10)                   |   -100  |
    mul  $12, $11, 100                |---------|
    sw   $12, -8($10)                 |         |
                                      |---------|
                                      |    -1   | X
                                      |---------|
                                      |    3    |
                                      |---------|
                                      |         |
                                      |---------|
                                      |         |
                                                   larger addresses

VERSION B:

                                                   smaller addresses
    li   $8,  4                       |---------|
    li   $9, -1                       |         |
    la   $10, X                       |---------|
    sw   $8, X                        |         |
    sw   $9, 4($10)                   |---------|
    lw   $11, ($10)                   |   400   |
    mul  $12, $11, 100                |---------|
    sw   $12, -8($10)                 |         |
                                      |---------|
                                      |    4    | X
                                      |---------|
                                      |    -1   |
                                      |---------|
                                      |         |
                                      |---------|
                                      |         |
                                                   larger addresses

Question 3 (5 points total)
Part A (2 points)
In a leaf function, which registers should be used for local variables, $s registers or $t registers?

Use $t registers in a leaf function.
Part B (3 points)
Very briefly (no more than 1 sentence) justify the answer you gave for Part A.

Using only $t registers in a leaf function means that no activation record will be needed, and no saving or restoring (stores or loads) will reduce performance.

Question 4 (10 points)
The following MIPS assembly language code fragment is to be used to write a value to an element of a two-dimensional array of integers. Add code that detects if an improper element address has been computed, and branches to the correct spot to print an appropriate error message (as given).

Assume the following about this code:


      la   $10, array     # $10 has array base address
      lw   $11, columns
      mul  $12, $11, $8
      add  $13, $12, $9
      mul  $13, $13, 4
      add  $14, $13, $10  # $14 has the computed element address

                          # check, before improper element address is referenced



      bltz $9, bad_column      # check column index: too small 
      bge  $9, $11, bad_column #                     too big
      lw   $16, rows
      bltz $8, bad_row         # check row index: too small 
      bge  $9, $16, bad_row    #                  too big


      sw  $15, 0($14)    # store value within array
      b   other_code

bad_row:
      puts bad_row_string
      done
bad_column:
      puts bad_column_string
      done

Question 5 (20 points, 10 points each part)
The given code fragment is a function. Assume that it follows all MIPS register usage and parameter passing conventions. This code fragment is missing its prologue and epilogue, and has some missing displacements (the blanks in load and store instructions).


fcn1:                                   # prologue


        sub  $sp, $sp, 28        # allocate AR
        sw   $ra, 28($sp)
        sw   $s0, 20($sp)
        sw   $s1, 24($sp)


        add  $s0, $a1, $a2              # function return value is computed within $s0
        mul  $s1, $a0, 16

        sw   $a0, _32_($sp)              # save current, live parameters

        sw   $a1, _36_($sp)

        sw   $a2, _40_($sp)

        move $a0, $a2                   # set up parameters to proc2
        li   $a1, -1
        move $a2, $s1
        jal  proc2                      # proc2 does NOT return a value

        lw   $a0, _32_($sp)              # restore current parameters

        lw   $a1, _36_($sp)

        lw   $a2, _40_($sp)

        andi $s0, $a2, 0xa3f4
        beq  $a0, $a1, epilogue
        li   $s0, 0
epilogue:                               # epilogue

        move $v0, $s0            # set return value

        lw   $s0, 20($sp)
        lw   $s1, 24($sp)
        lw   $ra, 28($sp)
        add  $sp, $sp, 28        # deallocate AR


        jr   $ra

Part A
Draw a diagram of the required (and implied) activation record for this function. Be sure to label the diagram's orientation, and identify what each word is used for.
    |-----------|  ^ smaller addresses
    |    $a0    |
    |-----------|
    |    $a1    |
    |-----------|
    |    $a2    |
    |-----------|
    |    $a3    |                   ($a0-$a3 are allocated by fcn1 for the use of proc2)
    |-----------|            
    |    $s0    |
    |-----------|
    |    $s1    |
    |-----------|
    |    $ra    |
    |-----------|
    |           |
Part B
Fill in the displacements and write the missing prologue and epilogue into the code fragment.