I am learning SPARC assembly with a simple example that you can see below. I have several questions about this example which shows passing parameters for a procedure.
In main section, I set 5
to first input parameter %o0
and 7
to second input parameter %o1
. After, I do the sum of these registers and put it into %o2
. Then, I call the "test" function where I print this sum.
fmt0:
.asciz "%d\n"
.align 4
.global main
main:
save %sp, -64, %sp
mov 5, %o0
mov 7, %o1
add %o0, %o1, %o2
!st %o2, [%fp-4] for storing %o2 at adress %fp-4
call test
nop
ret
test:
mov %i2, %o1
!ld [%fp-4], %o1 for loading from %fp-4 into %o1
set fmt0, %o0
call printf
nop
ret
With this above code, it prints the value "-273929364" and not "12" (compiled with gcc).
It seems that "mov %i2, %o1
" doesn't work. I know that %o2
register in main section becomes %i2
in called procedure but why I can't set directly the value of %i2
into %o1
register with this "mov
" instruction ?
Second question: If I uncomment the instructions "st %o2, [%fp-4]
" in main section and "ld [%fp-4], %o1
" in test function and comment "mov %i2, %o1
", it prints correctly "12". How can we know the correct offset to put as a function of passing parameters ?
From I have seen, %sp
becomes %fp
after "save %sp, -64, %sp
" insctruction ? Has %fp
the same value in main section
and test function
?
Finally, I have seen on different examples the instruction "call function, 0
" or "call printf, 0
" : why do I have to add a "0" after the name of the function called ? Is this the returned value (like with int main(void){ ... return 0;}
) ?
Thanks for your help
restore
appear before theret
? – Fondness