Function/method is a independent program unit
 

Functions/methods in high level programming are independent program units:

 
 

In a large programming project, different functions/methods can be coded by different programmers !!!

 

Interactions between functions/methods
 

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

Function/method is a independent program unit
 

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

 

How register use can jeopardize the independence of functions

Functions/methods will need to use registers in their execution:

 
 

Remember: A method does not see/know the internals (= instructions) of any other method

How register use can jeopardize the independence of functions

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

How register use can jeopardize the independence of functions

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 store parameters and local variables
 

Safety condition on using registers to pass parameters and store local variables:

  • In order to use CPU's registers to store:

      • parameters of a subroutine f( )
      • local variable of a subroutine f( )     

    the subroutine f( ) must be a leaf function

    (Since a leaf function does not call any function, the registers will not be corrupted by another function !)

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 !!

Passing parameters: how a compiler processes a function definition

Function definition:

  int f( int[] A, int a, int b )           
  {
     ....
  }
   

A compiler will:

  • Remember the name and data type of each parameter:

    Example:   parameter 1: int[] A parameter 2: int a, parameter 3: int b

  • Designate (= assign/reserve) a storage location for the parameter

    Each compiler has a fixed algorithm to assign (designate) storage locations

Passing parameters: how a compiler processes a function definition - Example

Function definition:

  int f( int[] A, int a, int b )           
  {
      ....
  }
   

The compile may make the following designations if f( ) is a leaf function:

  • Parameter 1: register r0 stores A (= address of the array)
  • Parameter 2: register r1 stores a (= value of the variable)
  • Parameter 3: register r2 stores b (= value of the variable)
 

 

What does a compiler do with the information about a function definition ?

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)

How to pass parameters using a register
 

  • Suppose the parameter designations are as follows :

      • Parameter a is stored in register R0       
      • Parameter b is stored in register R1

  • The caller method (= the "caller") must pass the parameter value(s) using the designated register(s)   (i.e.: x in R0 and y in R1)

How to return a value from a function
 

  • The called method (= the "callee") will store the return value into a designated register location (e.g.: R0) and then return to its caller

  • The caller method (= the "caller") must use the value in the designated register immediately after the function call

Next webpage: use a very simple example to illustrate the techniques
 

  • I will illustrate the techniques that we discussed with an example on the next webpage

  • We will examine this skeleton example in the next set of slides:

         main( )                
         {
            int x;
      
            x = f(1111, 2222);    // Pass 2 parameters 
         }
      
         int f(int x, int y)
         {
             return(9999);    // Pass a return value
         }