Subroutines


The high level language (C or Fortran) programmer expects several “features” or characteristics of subroutines:
  1. A mechanism for returning back to the calling point after a subroutine is called.

  2. Some means for passing arguments (values) to and from the routine.

  3. Variables that are declared within the subroutine are accessible only by that routine (i.e., local variables).
With the MIPS, only the first feature is supported by hardware—the others are implemented “by convention”—that is, an agreement among programmers that certain groups of registers will be used in certain ways. Again, the following table lists the general purpose registers and their usage.

These conventions must be followed by anyone wishing to write subroutines that will be used with other code (including code produced by compilers)—however, there is nothing inherent within the MIPS architecture that requires or enforces the use of these conventions.
Register
Number
Mnemonic
Name
Conventional Use
     $0   $zero Hardwired constant 0
     $1     $at Reserved for assembler
 $2- $3 $v0-$v1 Return results from functions.
 $4- $7 $a0-$a3 Used for arguments 1-4 of subroutine. Any additional arguments are placed on the stack.
 $8-$15 $t0-$t7 “Temporary,” not saved across a call
$16-$23 $s0-$s7 “Saved,” saved across a call, but must be saved (usually on stack) before being used, restored before return
$24-$25 $t8-$t9 More temporary
$26-$27 $k0-$k1 “Kernel”—reserved for OS use
    $28     $gp Pointer to global data area
    $29     $sp Stack pointer—contains address of top of stack
    $30     $fp Frame pointer—contains address of local variable space
    $31     $ra Return address of subroutine

Review: Subroutines
    Which subroutine feature is supported by MIPS hardware?

      Return back to the calling point after a subroutine is called.
      Return values from the routine.
      Some means for passing arguments (values) to the routine.
      Declare local variables.
Result:        




      “I used to think the worst thing in life is    
      to end up all alone. It’s not.    
      The worst thing in life is to end up with    
      people who make you feel all alone.”    
      ― Robin Williams