CS 2734
Computer Organization II
int myfun2(void)
{
int i;
int a[4];
for (i = 0; i < 4; i++)
a[i] = i + 3;
return a[3];
}
We'll place i at %fp - 4 and a[0] through
a[3] in locations %fp - 20 through %fp - 8
respectively.
.global myfun2
myfun2:
save %sp, -88, %sp
st %g0, [%fp - 4] ! Set i to 0
sub %fp, 20, %l0 ! Put address of a[0] in %l0
top_loop:
ld [%fp - 4], %l1 !
subcc %l1, 4, %g0 ! if (i - 4) >= 0, goto done
bge done_loop
nop
add %l1, 3, %l2
st %l2, [%l0] ! a[i] = i + 3
add %l0, 4, %l0 ! update pointer to a
add %l1, 1, %l1 ! i++
st %l1, [%fp - 4]
ba top_loop
nop
done_loop:
ld [%fp - 8], %i0 ! return a[3]
ret
restore
The C compiler translated this as myfun2.s. If you're curious you can compare this to the result of compiler optimization by cc myfun2.c -O -S.