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
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) |
(highest static address)| V (lowest stack 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.
Prev | This page was made entirely
with free software on linux: Kompozer, the Mozilla Project and Openoffice.org |
Next |