Hardware Reference
In-Depth Information
4.2 Introduction
It is common that the same sequence of instructions need to be executed in several places of
the program. The user may define macros as a solution to this issue. However, using the macro
call will duplicate the same sequence of instructions in places where the macro is invoked
and cause the program size to be bloated. Fortunately, all microprocessors and microcontrollers
provide the subroutine mechanism in which the program can invoke the same sequence of
instructions in many places of the program without duplicating it.
A subroutine is often written to perform operations on the basis of inputs provided by the
caller. The inputs provided by the caller are called incoming parameters . The caller of the sub-
routine often expects the subroutine to return certain results to it.
A subroutine call instruction causes the program control flow to change. When calling a
subroutine, the processor loads the starting address of the subroutine into the program counter
and then the CPU starts to execute the subroutine instructions. When the processor finishes
execution of the subroutine, it should return to the instruction immediately after the instruc-
tion that makes the subroutine call. This is achieved by executing a return instruction.
The subroutine call and return instructions work together to make the subroutine mecha-
nism work. The subroutine call instruction saves the return address in the stack data structure
while at the same time changing the program control flow to the start of the subroutine. The re-
turn instruction fetches the return address from the stack and places it in the program counter
and hence returns the program control back to the instruction immediately after the subroutine
call instruction.
The subroutine mechanism has great impact on the program development methodology. It is
mentioned in Chapter 2 that the most popular software development methodology is top-down
design with hierarchical refinement . The subroutine mechanism makes this approach possible.
Reusable macros and subroutines should be made into files and included in programs that
need them. This approach can increase programmers' productivity. Subroutines are especially
convenient for this approach. One of the objectives of this text is to promote software reuse and
writing reusable software.
4.3 Stack
A stack is a data structure from which elements can be accessed from only its top. The
processor can add a new element to the stack by performing a push operation and remove an
element by performing a pull (or pop ) operation. Physically, a stack can grow from a high address
toward lower addresses or from a low address toward higher addresses. Depending on the pro-
cessor, the stack pointer can point to the top element of the stack or to the byte immediately
above the top element of the stack. As shown in Figure 4.1, the HCS12 stack grows from a high
address toward lower addresses and has a 16-bit stack pointer (SP) that points to the top byte
of the stack. The memory space available for use by the stack is limited in a computer system.
There is always a danger of stack overflow and stack underflow . Stack overflow is a situation
in which the processor pushes data into the stack too many times so that the SP points to a
location outside the area allocated to the stack. Stack underflow is a situation in which the
processor pulls data from the stack too many times so that the SP points to an area below the
stack bottom. We must check the stack overflow and underflow in order to make sure that
the program won't crash. Of course, this checking adds overhead to the stack access.
The HCS12 provides instructions for pushing and pulling all CPU registers except the
stack pointer. A push instruction writes data from the source to the stack after decrementing
 
Search WWH ::




Custom Search