Chapter 10. Subprogram Calls and the Stack

Table of Contents

10.1. Motivation
10.2. Modular Design
10.2.1. Abstraction and Layers
10.3. MIPS Subprograms
10.3.1. JAL and JR
10.3.2. syscall
10.4. Memory Segments
10.5. Using the Stack with Subprograms
10.6. Value and Reference Arguments
10.6.1. Pass by Value
10.6.2. Pass by Reference
10.6.3. Value and Reference Arguments in MAL
10.7. The Stack Frame
10.7.1. The Basics
10.7.2. Allocating Space on the Stack for Local Variables
10.7.3. The Frame Pointer
10.7.4. Passing Arguments on the Stack
10.8. Macros vs. Subprograms
10.9. Coding Practice
10.10. Top-down Programming and Stubs
10.11. Homework

Assigned reading: Britton chapter 6.

10.1. Motivation

One of the most important subjects in beginning programming courses is the use of subprograms.

Without subprograms, the implementation of a program from a well-planned top-down design would be difficult. With the use of subprograms, each module in the design can be clearly represented in the implementation.

In this chapter we examine how subprograms work at the machine level, and in the process gain an understanding of the wonderfully useful illusions that high-level languages create for us, such as local variables, arguments, and recursion.

Subprograms go by many names, depending on the language. In C they are called functions, in Fortran they are called subroutines and functions, in Pascal they are procedures and functions. In C++, they are technically functions, but many C++ programmers have adopted the term methods from Java.