CS360 Midterm -- October 18, 1999. Question 2: Answer and grading

16 points total


Part 1: 8 points

Even though f1() is called with only one argument in main(), this code will still compile with many C compilers. Given the layout of our machine, k will end up being j in f1. Here is the assembler:
.globl A                      // Allocate A

f1:
    ld [fp+12] -> %r0         // Do j += i
    ld [fp+16] -> %r1
    add %r0, %r1 -> %r1
    st %r1 -> [fp+16]
    ld [fp+16] -> %r0         // return j
    ret

main:
    push 4                    // allocate k
    mv #5 -> %r0              // k = 5
    st %r0 -> [fp]
    ld [fp] -> %r0            // push k+5 on the stack
    mv #5 -> %r1
    add %r0, %r1 -> %r1
    st %r1 -> [sp]--
    jsr f1                    // Call f1
    pop 4                     // Pop the argument off the stack
    st %r0 -> A
    ret                       // And end.

Part 2: 6 points

        Stack                          Registers
     |----------|                     |----------|
     |          |<----------\         |    10    | r0
  /--| old fp   |           |         |    15    | r1
  |  | main+32  |           |             ...  
  |  |   10     |           \---------|          | sp
  \->| k:15     |           \---------|          | fp
         ...                          |  f1+20   | pc 
                                      |----------|

Part 3: 2 points

When main() returns, A and k are both 15.

Grading