Two methods of parameter passing are possible:
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.