CS 354, version A
Spring 2011
Name:___________________
Section:______________
ID:___________________
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 = _____ /   10
Q2 = _____ /   30
Q3 = _____ /   20
Q4 = _____ /   40
Total = _____ /100






Question 1 (10 points)
Assume that an architecture uses the same syntax as the MIPS R2000 for specifying operands, but allows any addressing mode for any operand. Give the name of the addressing mode implied for each of the operands in the instruction:

    add    $10,  ($9),  4($8)















Question 2 (30 points)
Below is partial MIPS assembly language code that is to set a value of -1 for the elements at indices 0, 4, 8, 12, 16, etc. of an array of integers.

Use the existing code, and add code to complete this fragment. Use registers $8-$25 as needed.

.data
AR_SIZE:   .word  20000
array:     .word  0:20000

.text

    lw    $8, AR_SIZE 
    la    $9, array     # $9 is the address of array element to change;
                        #    initial value is the address of array[0]
    mul   $10, $8, 4
    add   $11, $9, $10  # $11 is the address of the first word beyond
                        #    the array's allocation
  




loop:

    bge   $9, ____, loop_done












    b     loop

loop_done:



Question 3 (20 points total)
A program will use a stack to hold integer values. It is declared with

stack:  .word  0:1000

Initialization code for the stack pointer, which will reside in register $8 is

     la   $8, stack
     add  $8, $8, 4000
Part A (6 points)
Place an 'X' next to the true statement about this stack implementation.

______ This stack grows towards smaller addresses. The stack pointer ($8) contains the address of the next available, currently empty location at the top of the stack.

______ This stack grows towards smaller addresses. The stack pointer ($8) contains the address of the currently full location at the top of the stack.

______ This stack grows towards larger addresses. The stack pointer ($8) contains the address of the next available, currently empty location at the top of the stack.

______ This stack grows towards larger addresses. The stack pointer ($8) contains the address of the currently full location at the top of the stack.

Part B (4 points)
Assuming that this stack is not full, write a MIPS code fragment to push the value currently in $12 onto this stack.





Part C (10 points)
If 20 integers have already been pushed onto this stack, write a mathematical expression for the contents in $8, the stack pointer.






Question 4 (40 points total)
Function summation() is a recursive implementation of a function that calculates the sum of positive integers x through y (inclusive). For example if x=3 and y=6, then summation(3, 6) returns the value 18, since 3 + 4 + 5 + 6 = 18.

This code must use the same register usage and parameter passing conventions as 354 is using this semester.

Part A (15 points)
Write the PROLOGUE, the EPILOGUE, and fill in the missing displacements to complete the recursive implementation of this code.

#  int summation(int x, int y) {       /* C version */
#     if (x == y) {
#        return y;
#     } else {
#        return ( summation(x+1, y) + x );
#     }
#  }

summation:       # PROLOGUE







   lw   $8, ___($sp)    # load P1, x is in $8

   lw   $9, ___($sp)    # load P2, y is in $9
   bne  $8, $9, else    #  if (x == y)
   move $10, $9         #     return y; return value in $10
   b    return
else:
   add  $11, $8, 1      #  $11 has (x+1)

   sw   $11, ___($sp)   #  set up P1 for recursive call

   sw   $9, ___($sp)    #  set up P2 for recursive call
   jal  summation
   add  $10, $v0, $8    #  return value in $10
return:          # EPILOGUE





Part B (15 points)
Diagram the activation record of summation().
                  |--------------|
                  |              |                 ^ address 0
                  |--------------|
                  |              |
                  |--------------|
                  |              |
                  |--------------|
                  |              |
                  |--------------|
                  |              |
                  |--------------|
                  |              |
                  |--------------|
                  |              |
                  |--------------|
                  |              |
                  |--------------|
                  |              |
                  |--------------|
                  |              |
                  |--------------|
                  |              |
                  |--------------|
                  |              |
                  |--------------|
                  |              |
Part C (10 points)
Instead of a recursive implementation, assume that we had the assembly language implementation of an iterative one as represented by the C code:

  int summation(int x, int y) {
     int i;   /* for loop induction variable */
     int sum; /* running sum */

     sum = 0;
     for (i = x; i <= y; i++) {
        sum = sum + i;
     }
  }
Which implementation is more efficient? Why? Be sure to include your assumptions about how you measure efficiency.