CS201 Lab 12: Modular Programming

System Calls and Functions



Function Calls

Since your first lab you have been writing a simple function. The first thing that the SPIM system code does is call the main function which will be located at 0x40000024 if main: is the first line in your .text segment. As your programs grow in complexity, however, you will find the need to do the same thing more than once, or you may find you need to break a piece of the code away from the rest so you can work on it in isolation and not have to worry about the clutter of the rest of the program. It becomes important to structure your program.

One of the most important structuring devices for a program is its division into functions. Functions also provide a simple interface for allowing piece of code to be re-used in the same or different programs. A function in MIPS is made up of a call-return pair.

The function is invoked with the jal, or Jump and Link, statement:

jal MyFunc

jal stores the next instruction's address in $ra then sets the PC based on the label.

The return instruction is called jr or Jump Register. It sets the PC to the value in the specified register.

        ;code to accomplish the purpose of the subprogram

        jr $ra ; return to the program that calls this subprogram.


Lab Assignment

Click here for details.

Hand In Before The End of This Lab Session


This page last modified by Alex Clarke:
Tuesday, 12-Dec-2006 14:53:33 CST

CS Dept Home Page
CS Dept Class Files

Copyright: Department of Computer Science, University of Regina.