For simplicity sake, I will write the same (leaf function) sumRange( ):
public class MyCLass { public static int sumRange(int[] A, int a, int b ) { int i, s; s = 0; for (i = a, i < b; i++) s = s + A[i]; return(s); } public static void main( ) { int[] A = {11,12,13,14,15,16,17,18,19,20}; int x, y, z; z = sumRange(A, x, y); } } |
Difference:
I will
allocate parameters and
allocate local variables
on the
runtime stack
I.e.:
the stack technique is
general and
it will
also
work for
leaf functions !!!
How we used registers to store the parameters and the local variables:
Disadvantage: sumRange( ) can not call another function (its parameters and local variables will be overwritten !!!)
What this example will construct on the runtime stack:
Advantage: sumRange( ) can call another function (because its parameters and local variables will not be overwritten !!!)
// Java statement: sum = sumRange(A, a, b): main: /* ------------------------------------------------- Pass 3nd parameter value b using the stack ------------------------------------------------- */ movw r0, #:lower16:b movt r0, #:upper16:b ldr r0, [r0] push {r0} /* ------------------------------------------------- Pass 2nd parameter value a using the stack ------------------------------------------------- */ movw r0, #:lower16:a movt r0, #:upper16:a ldr r0, [r0] push {r0} /* ------------------------------------------------------- Pass 1st parameter address of array A using the stack ------------------------------------------------------- */ movw r0, #:lower16:A movt r0, #:upper16:A push {r0} /* ---------------------------------------------------------------- |
sumRange: // When sumRange begins, we will have the parameters on the stack: |
Key:
use
FP + offset to
access the
parameters and
local variables in the
stack frame !
DEMO:
/home/cs255001/demo/asm/8-sub/stack1.s