Function call + return expressed in high level programming language syntax:
function f1( ) function f2( ) { +------->{ ... | ... | ... | ... | ... call | ... | f2( ) ----------+ ... | ... <--------------+ ... | ... | ... V ... +------ return; } } |
We have learned how the compiler will translate (= implement) the function call + return.
Function call + return in ARM assembler - works only if f2 is a leaf function:
f1: +----> f2: ... ... | ... | ... | ... | ... call | ... | bl f2 ----------+ ... | ... <--------------+ ... | ... | ... V ... +------ mov pc, lr |
Function call + return in ARM assembler - general solution (will always work):
f1: +----> f2: push {lr} ... | ... | ... | ... | ... call | ... | bl f2 ----------+ ... | ... <--------------+ ... | ... | ... V ... +------ pop {pc} |
Consider a function (= method) definition in a high level programming language:
/* ------------------------------------------------------ sumRange(A,a,b): returns (A[a] + A[a+1] + ... + A[b-1]) ------------------------------------------------------ */ 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); } |
There are 3 things missing in the discussion
(1): a function can receive parameters:
/* ------------------------------------------------------
sumRange(A,a,b): returns (A[a] + A[a+1] + ... + A[b-1])
------------------------------------------------------ */
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);
}
|
Questions: (a) how are parameters passed to a function and (2) how to access them ?
(2): a function can return a value:
/* ------------------------------------------------------ sumRange(A,a,b): returns (A[a] + A[a+1] + ... + A[b-1]) ------------------------------------------------------ */ 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); } |
Questions: (a) how does a function return a value and (2) how to use the return value ?
(3): a function can have local variables (that are only accessible within the function body):
/* ------------------------------------------------------
sumRange(A,a,b): returns (A[a] + A[a+1] + ... + A[b-1])
------------------------------------------------------ */
int sumRange( int[] A, int a, int b )
{
int i, s; // Local variables
s = 0;
for (i = a, i < b; i++)
s = s + A[i];
return(s);
}
|
Questions: (a) how to create local variables and (2) how to use the local variables ?
|
Terminology:
|
How the caller passes parameters to the callee:
|
How the callee passes a return value to the caller:
|
Because function
returns
a
single value:
|
The (memory) locations used to pass parameters:
|
I will discuss method 1 (using registers) first...