CS 354, version A
Spring 2007
Answer Key
Exam 2 Solution

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
Q2A = _____ / 10
Q2B = _____ / 10
Q2C = _____ / 10
Q3 = _____ / 10
Total = _____ / 50







Question 1 (10 points total)
Write a MAL assembly language code fragment that does exactly what this C assignment statement does. All variables are integers, and all variables reside in memory. Include comments about what variables are in what registers.


     A = B - (C + 14);   /* C assignment statement */


lw   $8, B           # $8 has B
lw   $9, C           # $9 has C
add  $10, $9, 18     # $10 has C + 18
sub  $11, $8, $10    # $11 has B - (C + 18)
sw   $11, A

Question 2 (30 points total)
The following function receives 2 integer parameters. It adds 56 to the local copy of the first parameter, subtracts 1 from the local copy of the second parameter, and returns the sum of these two local values.


fcnA:                      # prologue
       sub  $sp, $sp, 16

       sw   $ra, 16($sp)

       sw   $8, 4($sp)

       sw   $9, 8($sp)

       sw   $10, 12($sp)



       lw   $8, 20($sp)   # get first parameter

       add  $8, $8, 56

       lw   $9, 24($sp)   # get second parameter

       sub  $9, $9, 1

       add  $10, $9, $8    # compute sum for the return value


fcnA_epilogue:             # epilogue

       move   $v0, $10   # entire instruction missing

       lw   $8, 4($sp)

       lw   $9, 8($sp)

       lw   $10, 12($sp)

       lw   $ra, 16($sp)

       sub  $sp, $sp, 16

       jr   $ra





PART A (10 points)
Fill in all the blanks (including the missing instruction) to make this function correctly implement the CS354 register usage conventions.

PART B (10 points)
Draw a diagram of the activation record for fcnA. Do not forget to show the orientation of your diagram, and identify all elements of the activation record.
     addr 0 at top of diagram

            .
            .
     |      .      |
     |-------------|
     |             |
     |-------------|
     |    $8       | ----
     |-------------|    |
     |    $9       |    |
     |-------------|    |---   fcnA's AR
     |    $10      |    |
     |-------------|    |
     |    $ra      | ----
     |-------------|    
     |             |    
     |-------------|    
     |      .      |
            .
            .


PART C (10 points)
A portion of code that is to call fcnA is shown. Add to this code to show the set up of parameters and a call to fcnA.

Assume that any registers your added code uses are correctly saved and restored (but not shown in the code). Assume that the integer variable in memory called int1 is the first parameter to be passed to fcnA. The integer variable in memory called int2 is the second parameter to be passed to fcnA.

       sub  $sp, $sp, 100
       sw   $ra, 100($sp)


       #  add code here for the set up and call to fcnA



       lw   $8, int1       # set up first parameter
       sw   $8, 4($sp)

       lw   $9, int2       # set up second parameter
       sw   $9, 8($sp)
       
       jal  fcnA




       lw   $ra, 100($sp)
       add  $sp, $sp, 100
       jr   $ra





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


     array:  .word 0:100

Assume that the element at index = 64 (the 65th element of the array) is at address 0x00cc00fc. What is the base address of this array? Give your answer in hexadecimal, and please show work to receive partial credit.
From the formula for which element is where:

  array[64]     =     base    +   offset

  0x003300fc    =     base    +   (64 * 4)



  64 in binary is  1000000
 4*64 in binary is 100000000


Do unsigned subtraction:

    0000 0000 0011 0011 0000 0000 1111 1100
  -                     0000 0001 0000 0000
  ------------------------------------------ 
    0000 0000 0011 0010 1111 1111 1111 1100


in hexadecimal, this is 0x0032fffc