In this writing, I will deal about Procedure Call.
Procedure is function. To using them, MIPS use special registers.
$a0 - $a3 registers for arguments to pass parameters. $v0 - $v1 registers for return values. $ra register is used to express return address.
MIPS has insturction pair JAL ProcedureAddr and JR. JAL is used to function call with label. PC is procedure address and $ra is next instruction address(PC or PC + 4).
They are used with each other. First, JAL instruction is jump labeled function(place) and doing something. In the last line, JR instruction jump to $ra register. $ra register saved the address of PC or PC + 4(The next address of JAL instruction). By doing that, we can express function call.
However, there are many cases that required more registers than four arguments registers and two return value registers. In this case, we can use stack.
Stack is an ideal place for spilling register. This situation occured in function call, so we can use the concept of stack.
If there are 5 arguments and they are saved at $s0 - $s4. In this case we can't use all of them.
move $a0, $s0
move $a1, $s1
move $a2, $s2
move $a3, $s3
we can use 4 argument and the next is used by stack.
addi $sp, $sp, -4
sw $s4, 0( $sp)
Finally, we should clear stack.
addi $sp, $sp, 4
In this process, we can use more than 4 arguments.
To reduce the register spilling, we can use $t0 - $t9 they are not preserved and $s0 - $s7 they are must be preserved.
Also, there are Caller and Callee concept.