next up previous
Next: Nested Procedures Up: Procedures Previous: Procedure Linkage

Parameter Passing

Parameters are used to pass data in and out of procedures. MAL provides general registers $4-$7 ($a0-$a3) and floating point registers $f12-$f14 for passing parameters.

Two methods of parameter passing are possible:

Pass by Value
The register contains the value of the parameter. This method allows the procedure to read and write the value of the parameter stored in a register.
Pass by Address
The register contains the address where the value of the parameter is located. This method allows a procedure to read and write the value of a parameter stored in RAM. This method is equivalent to Pascal var parameters.

The caller and callee must agree on the usage of the registers for passing parameters between themselves.

The following MAL code illustrates a procedure which swaps the parameters it receives in registers $a0 and $a1:

Swap:   move    $t0, $a0
        move    $a0, $a1
        move    $a1, $t0
        jr      $ra

To use the procedure, the values of the parameters must first be loaded into registers $a0 and $a1:

        .data
I1:     .word   1
I2:     .word   2
        .text
            .
            .
        lw      $a0, I1 # load p1
        lw      $a1, I2 # load p2
        jal     Swap    # swap p1, p2 in registers
            .
            .

If the parameters are passed by address, the Swap procedure can be modified to swap the contents of two memory locations:

Swap:   lw      $t0, ($a0) # load contents of p1
        lw      $t1, ($a1) # load contents of p2
        sw      $t0, ($a1) # store p1 into p2
        sw      $t1, ($a0) # store p2 into p1
        jr      $ra        # return

The calling code must supply the addresses of the memory locations to be swapped in $a0 and $a1:

        .data
I1:     .word   1
I2:     .word   2
        .text
            .
            .
        la      $a0, I1 # load address of p1
        la      $a1, I2 # load address of p2
        jal     Swap    # swap contents of p1 and p2
            .
            .

When the number of parameters to be passed exceeds the number of registers, parameters must be passed using external memory in RAM.



CS 301 Class Account
Mon Sep 13 11:15:41 ADT 1999