Search code examples
assemblystacksparc

Simple SPARC assembly printf code (print the stack variables)


fmt0:   
    .asciz "%d\n"
    .align 4

    .global main, printf

main:
    save %sp, -76 & -8, %sp
    mov 5, %l0
    st %l0, [%fp-4]

    mov 7, %l1
    st %l1, [%fp-8]

    add %l0, %l1, %l2
    st %l2, [%fp-12]

    clr %l3
    clr %l4
    clr %l5
    mov 1, %l3
    mov 3, %l4
mov 0, %l5

test:
    cmp %l3, %l4
    bg exit
    sub %l5, 4, %l5
    set fmt0, %o0
    ld [%fp + %l5], %o1
    call printf
    inc %l3
    ba test
    nop

exit:
    mov 1, %g1
    ta 0

the expected value is
5
7
12

But the result value was
5
5
12

What's wrong with my code?
Thanks in advance


Solution

  • Your stack frame is too small. With three automatic one-word sized variables, you need a stack frame of 26 words. The stack frame in your program is only 20 words.