Next: About this document
Up: Procedures
Previous: Stack Frame
The use of procedures in MAL is summarized below:
- The transfer of control to a procedure and return from the
procedure is implemented using the jal and jr instructions.
- Parameters can be passed to procedures in two ways:
- By value. The value of the parameter is placed in a register.
- By address. The address of the parameter is placed in a register.
The first 4 parameters are passed in registers $a0-$a3. - Functions return integer values in registers $v0-$v1 and floating
results in $f0-$f2.
- Nested procedures must save their return addresses on the
system stack.
- The system stack is also used to pass parameters and save registers
in accordance with the MIPs register conventions.
- Registers can be saved in two ways:
- By the calling program. This is necessary when the calling program
is using a temporary register ($t0-$t9) at the time of a procedure call.
- By the called procedure. This is necessary when the called procedure
changes any of the saved registers $s0-$s7.
- The stack pointer ($sp) must have the same value
at the end of a procedure that it had when the procedure was called.
(The number of pushes and pops on the system stack must be equal inside
a procedure.)
- Recursive procedures are nested procedures which call themselves.
- Every recursive procedure can be written using iteration instead of
recursion. The iterative version will generally run faster.
- The block of information stored on the system stack to preserve the
state of a procedure is called the stack frame. The procedure
calling convention specifies the structure of the stack frame.
In assembly language, procedures have both advantages and disadvantages.
The advantages of procedures are:
- Procedures facilitate modular code and make code easier to develop
and understand in high-level languages.
- The high-level language compiler writes the assembly language
code to implement procedures.
- Procedures allow code to be reused, resulting in shorter programs.
The main disadvantage of procedures is that code written using
procedures will run more slowly than code without procedures
because:
- Additional instructions and memory accesses are required to:
- call the procedures,
- pass parameters,
- save and restore registers.
- Jump instructions disrupt the flow of instructions through the
CPU, forcing the CPU to wait until new instructions beginning
at the jump address can be fetched (see Pipelining).
Next: About this document
Up: Procedures
Previous: Stack Frame
CS301 Class Account
Mon Nov 25 11:40:41 AST 1996