Subroutines part 3

We need to talk about how subroutines communicate with their callers in the MIPS world.

When a subroutine is called, it is typically sent some actual parameters by its caller. The values of these parameters will affect the execution of the subroutine. A function will also return a value to its caller. This page is about how that communication is done.

A typical scenario for the whole thing would therefore be like this:

Here's an example: we need to call doubleit, a function that returns twice the value in its parameter. You can paste this code into MARS and run it. You can also single-step through it and watch the values in the registers and program counter change.

# call doubleit to double the value of x and put the doubled value into two-x; in C, it would look like this:
# two-x = doubleit(x);

.include    "macros.asm"      # we need this because quit is a macro

.data

x:      .word 15
two-x:  .word 0

.text

main:   lw   $a0  x           # parameter
        jal  doubleit
        sw   $v0  two-x       # returned value
        quit

# this function must put twice its parameter (in $a0) into its returned-value register ($v0)
doubleit:    add  $v0  $a0  $a0      # $v0 = 2 * $a0
             jr   $ra