# 5: Simple MIPS Subprograms


Learning Objectives

1. how to create a subprogram.
2. the jal (call subprogram) and jra $ra (return from subprogram) operators. 3. the .include MARS assembly directive. 4. How to pass parameters to and retrieve return values from a subprogram. 5. how the Program Counter register ($pc) the control program execution sequence.
6. how to structure and comment a file of subprograms.
7. why the subprograms in this chapter cannot call themselves or other subprograms.

The programs that were implemented in Chapter 3 were very long and required a lot of code to implement simple operations. Many of these operations were common to more than one of the programs, and having to handle the details of these operations each time was distracting, and a possible source of errors. For example, every program needs to use syscall service 10 to exit a program. It would be nice to abstract the method of exiting the program once, and then use this abstraction in every program. One way to abstract data is to use subprograms. Something similar to subprograms exist in every language, and go by the names functions, procedures, or methods.14

The concept of a subprogram to create a group of MIPS assembly language statements which can be called (or dispatched) to perform a task. A good example is an abstraction we will call "Exit", which will automatically exit the program for the programmer.

These subprograms are called simple subprogram because they will be allowed to call any other subprograms, and they will not be allowed to modify any save registers. These limits on subprograms simplify the implementation of subprograms tremendously. These limits will be taken away Chapter 8, which will allow the creation of much more powerful, but also much more complex, subprograms.

13 These subprograms are simple because they are non-reentrant. In simple terms, this means that these subprograms cannot call other subprograms and cannot call themselves. The reason they are non-reentrant will be explained in the chapter. How to make reentrant subprograms requires a concept called a program stack, and will be covered in chapter 8.

14 All subprograms in this chapter could be implemented using Mars macros. However there is real value in introducing the concept of subprograms at this point in the text, and so macros will not be covered.

This page titled 5: Simple MIPS Subprograms is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by Charles W. Kann III.