sections in this module City College of San Francisco - CS270
Computer Architecture

Module: MIPS-III (Procedures)
module list

A bird's-eye view of procedures

Procedures (functions) are one of the basic forms of encapsulation in computer programming. As you know, a function (or procedure) is called to isolate data from the calling program (the caller). The function executes and it uses its own variables, which must be separate from those of the caller. Assuming the function (the callee) does not use any global data, the only data shared between them is defined by the function parameters and return value. Usually (always, in the case of Java), sharing of simple data types (scalars) is unidirectional, i.e., the callee cannot alter the data of the caller. In C and C++, however, this restriction can be relaxed by the use of pointer or reference arguments. (Array datatypes as parameters are usually writable by both caller and callee. This is simply because the array is passed as a pointer.) In the remainder of this and continuing with the next topic we will discuss how this encapsulation is implemented at the machine-language level.

To implement encapsulation, procedures must have mechanisms for

In addition to providing encapsulation, procedure implementation at the machine level must protect the integrity of registers. Some mechanism must be provided so that the use of registers in the callee do not interfere with those in the caller.

A very important aspect of this implementation is that both sides of a procedure call are blind. Other than the published interface (of parameters and return value), the caller and callee do not know anything about each other. Procedures must be able to be translated separately (in separate modules) and work when the modules are joined.

All of these capabilities are provided by the platform's procedure calling convention and implemented using a stack. A stack is a block of memory with a movable pointer (the stack pointer - $sp on MIPS). At any time, memory on one side of the stack pointer is in-use and memory on the other side of the stack pointer is not yet allocated. The stack pointer itself points to the last memory address that is in-use. This is called the top-of-stack.

Stacks are usually word- or double-word aligned for simplicity. This means that the stack pointer must be moved by multiples of the alignment size - in the case of words, by four-byte increments. Any data placed on the stack must be padded to keep proper alignment. This means all data placed on the stack must be a minimum of four bytes. This means scalars that are less than four bytes are converted to their four-byte counterparts and placed on the stack.

Although stack memory and other (static and dynamic) memory could be implemented as completely separate memory areas (addresses), they are usually implemented using one large block of addresses, with each starting on opposite ends. In this case (e.g., MIPS) memory is allocated using increasing addresses and stack space is allocated using decreasing addresses (i.e., the stack grows downwards):

memory address
use
high address  --->
(stack pointer starts here)
|
|
V

(lowest stack address)
(highest static address)






low address  --->

stack
memory





static memory
...
dynamically
allocated
...
pre-allocated
(globals)

(Note that on this implementation the top of stack is actually the lowest address in-use.)

Pushing and popping

Traditionally, placing an item on the stack is called pushing the item on the stack. In our implementation, this involves the following steps

The inverse operation is called popping an item on the stack. The sequence is important:

This inverse sequence is very important to avoid referencing memory that is not in the active part of the stack. No such reference should ever be made.

OK, enough of the bird's-eye view. Let's go ahead and see how the most basic of procedure (function) calls is implemented in our next section.
 
Prev This page was made entirely with free software on linux:  
Kompozer, the Mozilla Project
and Openoffice.org    
Next

Copyright 2015 Greg Boyd - All Rights Reserved.