Functions/methods in high level programming are independent program units:
In a large programming project, different functions/methods can be coded by different programmers !!!
The only defined interaction with a function/method is through:
(1) input parameters and (2) a return value
A method must perform a well-defined operation on the input parameters and return the result
Functions/methods in high level programming are independent program units:
"Independent" means: a method cannot (should not) see/use/know the internals (= instructions) of any other method
Functions/methods will need to use registers in their execution:
Remember: A method does not see/know the internals (= instructions) of any other method
Question: will the instruction add r0,r2,r3 guarantee to produce the sum 11 as answer ??
Remember: A method does not see/know the internals (= instructions) of any other method
No ! The reason is because the called function can use any register for its execution:
Therefore: always reload the variable values (from memory) into registers after a function call !!
Safety condition on using registers to pass parameters and store local variables:
|
If the subroutine f( ) is not a leaf function, then the parameters or local variables stored in registers can be corrupted when it invokes another subroutine call !!
Function definition:
int f( int[] A, int a, int b ) { .... } |
A compiler will:
|
Function definition:
int f( int[] A, int a, int b ) { .... } |
The compile may make the following designations if f( ) is a leaf function:
|
The parameter name in the function body is used to identify the designated location of that parameter
Example: since the parameter a was designated to register r1
int f( int[] A, int a, int b ) { .... a = a + 1; // a = register r1 ^ ^ | | | +----- parameter a used in the RHS | will use r1 as source in an assemb instruction | +-- parameter a in the LHS will use r1 as destination in an assembler instruction } |
I will show you how to "play compiler" in the next few webpages (i.e., do what the compiler does)
|
|
|