CS 354, version A
Fall 2006
Name:___________________
ID:___________________
Login:_____________________
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 = _____ /   6
Q3 = _____ /   9
Q4 = _____ /   4
Q5 = _____ / 20
Total = _____ / 49



Question 1 (10 points)
Give a MAL code fragment that implements this C code. Use register $8 for variable sum, and register $9 for variable i. Assume all variables are integers.


   sum = 3;
   for (i = 0; i < 12; i++) {
      sum = sum + i;
   }













Question 2 (6 points total)
The base address of an array of 100 integers is 0x00883400. At what (hexadecimal) address is the element with index = 65? Assume integers are 32 bits and a byte addressable memory.











Question 3 (9 points)
An array of 100 integers is declared with


   ints:  .word  0:100

This code fragment (that does NOT work!) is written to subtract 12 from each element of the array.


       lw   $t0, ints  # $t0 is the address of an array element

       li   $t1, 0     # $t1 is a counter (loop induction variable)

       li   $t2, 100   # $t2 is the constant 100

for:   bge  $t1, $t2, end_for

       sw   $t3, ($t0)

       sub  $t3, $t3, 12

       lw   $t3, ($t0)

       add  $t1, $t1, 1

       add  $t0, $t0, 1

       b    for

end_for:

There are 3 errors in this code fragment. On the code fragment, identify the 3 errors and show how to fix the code.


Question 4 (4 points)
An application is utilizing a stack with the capability to hold 100 integers. Chose the best answer from the following lettered options to answer the questions.

A. Before pushing data, when the stack has 100 entries.
B. After pushing data, when the stack then has 100 entries.
C. Before popping data, when the stack has 100 entries.
D. After popping data, when the stack then has 99 entries.
E. Before pushing data, when the stack has no entries.
F. After pushing data, when the stack then has 1 entry.
G. Before popping data, when the stack has no entries.
H. After popping data, when the stack then has no entries.

____ When should the application detect and identify that the stack is full?

____ When should the application detect and identify that the stack is empty?







Question 5 (20 points total)
A MIPS assembly language program follows all register usage and parameter passing conventions. Assume the following:

PART A (8 points) Given the assumptions, diagram the activation record necessary for parent. Be sure to identify what goes where within the activation record, as well as the orientation of your diagram.















PART B (4 points) The contents of the first parameter register ($a0) is live across parent's call to child. To save this register, its contents are to be placed on the stack. Complete this code fragment by filling in the correct displacements for the sw and lw instructions.

    sw   $a0, ____($sp)

    jal  child

    lw   $a0, ____($sp)


PART C (8 points) Function parent has been accumulating (calculating) its return value in register $s0. Write the epilogue code fragment for parent.