CS360 Midterm Exam: March 2, 2004. Answer to Question 3

main:
    push #4               / Allocate i, which will be at [fp]

    st %g1 -> [fp]        / i = 1
    ld [fp+16] -> %r0

    mov #4 -> %r1
    add %r0, %r1, %r0     / put &(argv[1]) into r0
    ld [r0] -> %r0        / put argv[1] into r0
    st %r0 -> [sp]--      / store argv[1] onto the stack

    jsr atoi              / call atoi(argv[1])
    pop #4                
    st %r0 -> [sp]--      / push atoi(argv[1]) onto the stack

    mov #6 -> %r0         / store 6 onto the stack
    st %r0 -> [sp]--

    jsr b                 / call b
    pop #8                / pop the arguments off the stack

    ld [fp] -> %r0        / return i
    ret

Grading