CS 354, version A
Spring 2008
Name:___________________
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 = _____ / 15
Q2 = _____ / 18
Q3 = _____ / 15
Total = _____ / 48




Question 1 (15 points)
Write a MIPS assembly language code fragment that implements the following C for loop. Assume that all variables are integers.

Write any assumptions about the locations of variables, and please include comments to identify which variables are in which registers.

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




























Question 2 (18 points total)
A stack of integers is declared with

  stack:  .word 0:100

The stack pointer for this stack resides in register $8, and this stack pointer is initialized with the code

          la   $8, stack
Part A (4 points)
Circle the statement that is true about the stack pointer for this stack.
Circle the statement that is true about this stack.

Part B (6 points)
Write a MIPS assembly language code fragment that first pushes the value -18 onto this stack, and then pushes the value 200 onto this stack.













Part C (8 points)
Assume that the base address assigned for this stack is 0x00400bce0. Further assume that 24 integers have been pushed onto this stack. At what (hexadecimal) address is that 24th integer?














Question 3 (15 points)
The following partial function is to do exactly the same as the print_integer() function of Assignment 4. It uses an algorithm that is likely different than what you implemented.

Complete the code for the function by writing the prologue, the epilogue, and fill in the missing displacement in the lw instruction. This code must follow all the register usage and parameter passing conventions used by 354 this semester.


.data
int_str:  .byte 0:20     # array to hold characters for printing
.text

print_integer:           # prologue












   lw   $8, __($sp)      # $8 is parameter, the integer to print
   la   $9, int_str      # $9 is base address of the array
   add  $9, $9, 19       # now, $9 becomes the address of a char within str
   sb   $0, ($9)         # terminate string with null character
more_digits:
   rem $10, $8, 10       # $10 is single digit of the integer
   sub $9, $9, 1
   add $10, $10, 48      # convert integer to ASCII char
   sb  $10, ($9)
   div $8, $8, 10
   bgtz $8, more_digits

   puts $9               # null terminated string starts at address in $9

                         # epilogue