CS 354, version A
Fall 2004
Solution
Exam 3 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 = _____ /   6
Q2 = _____ /   6
Q3 = _____ /   5
Q4 = _____ / 11
Q5 = _____ / 12
Total = _____ / 40









Question 1 (6 points)
Of the 7 procedures/functions/methods implemented in Program 3 (which read in 2 integers and then printed out the equations to add these integers in a decimal form and in a sign magnitude form), circle the names of those that are leaf procedures/functions/methods.

get_input              get_integer                sum

print_decimal_eqn      print_decimal_int 

print_sm_eqn           print_sm_int


Question 2 (6 points total)
Give 3 different TAL equivalents (translations, syntheses) for the MAL instruction

    b loop_top
    beq $0, $0, loop_top
    beq $1, $1, loop_top
    beq $2, $2, loop_top   # etc.

    blez $0, loop_top

    bgez $0, loop_top

    j   loop_top


    lui $1, 0x ms 16 bits of address loop_top
    ori $1, $1, 0x ls 16 bits of address loop_top
    jr  $1

Question 3 (5 points)
Circle ALL operands within the following MAL code fragment that will result in absolute addresses within the machine code.

fcn1:   sub   $sp, $sp, 32

        sw    $ra, 20($sp)

        sw    $s0, 24($sp)

        sw    $s1, 28($sp)

        sw    $s2, 32($sp)

        li    $s0, 1

        li    $s1, 1000

        li    $s2, 0

loop:   beq   $s0, $s1, done_loop

        move  $a0, $s0

        la    $a1, array

        jal   fcn2

        add   $s2, $s2, $v0

        add   $s0, $s0, 1

        b     loop

done_loop:
        move  $v0, $s2

        lw    $ra, 20($sp)

        lw    $s0, 24($sp)

        lw    $s1, 28($sp)

        lw    $s2, 32($sp)

        add   $sp, $sp, 32

        jr    $ra



Question 4 (11 points total)
Use the code for fcn1 given in Question 3 to answer this question.


Part A (2 points)
Does fcn1 follow the MIPS conventions (as given for 354 Program 3) for register usage and parameter passing?

No, it doesn't! The location of $ra within the AR for fcn1 is wrong relative to the locations of the $s registers. However, this is much too obsure of an answer for most of us, so full credit was give to all students with an answer of either yes or no.

Part B (5 points)
Draw a diagram of the activation record allocated and used by fcn1. Identify all items within the activation record, and show where the stack pointer points just after allocation of the activation record.


              address 0  ^

   |------|
   |      |<- $sp (after allocation, and to orient the diagram)
   |------|
   | $a0  |  4($sp)  -----   -----
   |------|              |       |
   | $a1  |  8($sp)              |
   |------|              |       |---- space used by fcn2
   | $a2  |  12($sp)             |
   |------|              |       |
   | $a3  |  16($sp)         -----
   |------|              |----- space allocated by fcn1
   | $ra  |  20($sp)
   |------|              |
   | $s0  |  24($sp)
   |------|              |
   | $s1  |  28($sp)
   |------|              |
   | $s2  |  32($sp) -----
   |------|

Part C (4 points)
Could register $t0 be used in place of (substituted for) $s0 within fcn1, and still follow the MIPS conventions for register usage? Briefly justify your answer.

No. The value in $s0 is live across the call to fcn2. Therefore, correct usage (according to conventions) of a $t register would require an extra save restore pair (to load/store $t0) within the loop.

Question 5 (12 points total)
Consider the following MIPS machine code fragment:


   
address        contents
------------------------------
   0xa500 0004    0xa20c0004 sb  $12, 4($16)  
   
-->0xa500 0008    0x8f380008
|  
|  0xa500 000c    0x01ae6020
|                 ---------- 
|  0xa500 0010    0x0c10000c  Circle this one, it is the jal
|  
|  0xa500 0014    0x01ae6022
|  
|  0xa500 0018    0x3c041001
|  
|  0xa500 001c    0x34020004
|  
|  0xa500 0020    0x0000000c
|  
---0xa500 0024    0x11c7fff8

   0xa500 0028    0x01cd7824





Part A (3 points) One of the instructions is a branch instruction. Draw an arrow from the branch instruction to its branch target, to show where the instruction would branch to if the branch were taken.


Part B (3 points) One of the instructions is a jal instruction. Identify the jal instruction by circling its address and contents.


Part C (3 points) One of the instructions is an addition instruction. Underline the machine code for the addition instruction.


Part D (3 points) One of the instructions is a store instruction. Identify the store instruction by giving its TAL assembly language to the right of the store instruction.