1
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

manutd
  • 564
  • 9
  • 22
  • Are you sure you got the argument passing to printf correct? I found [this](http://stackoverflow.com/questions/5814244/printf-format-specifier-in-sparc-assembly-argument) previous SO discussion where someone else had a problem with `printf`. – user786653 Oct 23 '11 at 08:36

1 Answers1

2

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.

torgny
  • 176
  • 4
  • SPARC architecture demands that additional 64bytes of the stack memory should be required because of the registers. So I made 64bytes of the stack memory and 12bytes(3 variable of 4bytes each). I think this is the best suitable way for the stack size. – manutd Oct 24 '11 at 07:01
  • But I was wrong, When I made the stack size for 104bytes, It worked perfectly. The result was 5 /7 /12 as I had expected. But I wonder, Why this large stack size is neededd? Although I used only 3 variables? Do you know? Thanks – manutd Oct 24 '11 at 07:04
  • 1
    The stack frame of your function should have space for 16 words for the register window, one word for where functions that your function calls store return value and 6 words for where functions that your function calls may store passed arguments. Apart from those "mandatory" 23 words (which has to be rounded up to 24), you need to reserve space for passing more than six parameters (which you are not doing so you don't need to do that) and space for your automatic variables, of which there are three words. All in all: 26 words. As you can see, the called function (printf) may modify your stack. – torgny Oct 24 '11 at 08:30
  • As an extra credit assignment, you can try to work out exactly how the calls to printf corrupted your stack. ;) – torgny Oct 24 '11 at 08:35