CS 354, version A
Fall 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 = _____ /   9
Q2 = _____ /   30
Q3 = _____ /   20
Q4 = _____ /   12
Q5 = _____ /   29
Total = _____ /100






Question 1 (9 points)
Pretend that an architecture uses the same syntax as the MIPS 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:

    or    ($10),  0xaabbccdd,  20($8)

















Question 2 (30 points total)
A program will use a stack to hold integer values. This stack is declared with

stack:  .word  0:8

Part A (20 points) The code to initialize the stack pointer is given. Fill in the 4 missing code fragments to do what the comments specify. Ignore the possiblity of stack overflow or underflow for this question.
     # Initialization code for the stack pointer, which resides in register $14
     la    $14, stack
     add   $14, $14, 32


     # push the value -13 onto this stack







     # pop a value off this stack and place it into register $22







     # push the value 600 onto this stack







     # push the value 2 onto this stack














Part B (10 points)
Fill in the diagram with memory contents for the allocation of stack after the code fragment (completed in Part A) finishes its execution.


                  |--------------|
                  |              |                 ^ address 0
                  |--------------|
                  |              | stack
                  |--------------|
                  |              |
                  |--------------|
                  |              |
                  |--------------|
                  |              |
                  |--------------|
                  |              |
                  |--------------|
                  |              |
                  |--------------|
                  |              |
                  |--------------|
                  |              |
                  |--------------|
                  |              |
                  |--------------|
                  |              |
                  |--------------|
                  |              |





























Question 3 (20 points)
2 arrays of integers are declared with

.data
ar1:   .word  0:100
ar2:   .word  0:100

This MIPS assembly language code fragment is to copy the elements of ar1 into corresponding elements of ar2. Use the existing code, and add code to complete this fragment. Use registers $8-$25 as needed.

.text

       li    $8, 100

       li    $9, 0          # loop iteration counter

       la    $10, ar1       # $10 starts as the address of ar1[0]

       la    $11, ar2       # $11 starts as the address of ar2[0]


loop:  beq   $9, $8, loop_done  # loop iterates exactly 100 times











       b     loop

loop_done:

Question 4 (12 points total)
We specified 2 conventions for the implementation of functions this semester. Briefly describe each.

1. convention for parameter passing




2. convention for register usage




Question 5 (29 points total)
Function swap() uses the same register usage and parameter passing conventions as 354 is using this semester. The code below is missing the prologue and epilogue code, as well as the displacements needed for accessing the parameters. swap(int *x, int *y) receives pointers to 2 integers that it is to exchange.

swap:       # PROLOGUE code missing here

    lw  $8, ___($sp)  # P1 is x; address of integer to be swapped
    lw  $9, ($8)      
    lw  $10, ___($sp) # P2 is y; address of integer to be swapped
    lw  $11, ($10)
    sw  $11, ($8)
    sw  $10, ($10)

            # EPILOGUE code missing here

Part A (20 points)
Diagram the activation record needed by swap().

                  |--------------|
                  |              |                 ^ address 0
                  |--------------|
                  |              |
                  |--------------|
                  |              |
                  |--------------|
                  |              |
                  |--------------|
                  |              |
                  |--------------|
                  |              |
                  |--------------|
                  |              |
                  |--------------|
                  |              |
                  |--------------|
                  |              |
                  |--------------|
                  |              |
                  |--------------|
                  |              |
Part B (9 points)
Would an implementation of caller saved registers or callee saved registers be more efficient for the registers used in this function's implementation? Briefly, why?