Hardware Reference
In-Depth Information
the caller of the subroutine. If the values of these CPU registers were not preserved during a
subroutine call, the execution result of the caller cannot be correct. Either the caller or the cal-
lee may be responsible for saving these registers. However, it is more efficient for the subrou-
tine to do the saving because the same subroutine may be called by many callers. CPU registers
are usually saved at the entrance of the subroutine before they are modified.
Only those registers that are not used for passing incoming parameters and returning re-
sults need to be saved. The subroutine should restore the values of the saved registers immedi-
ately before returning to the caller. The saving and restoring must be done in the reverse order.
For example, if the subroutine saves D, X, and Y in the following order:
pshd
pshx
pshy
then it should have the following instruction sequence to restore them before returning to the
caller:
puly
pulx
puld
4.6 The Stack Frame
The stack is used heavily during a subroutine call: The caller may pass parameters to the
callee, and the callee may need to save registers and allocate local variables in the stack. The
region in the stack that holds incoming parameters, return addresses, saved registers, and local
variables is referred to as the stack frame . Some microprocessors have a dedicated register for
managing the stack frame; the register is referred to as the frame pointer . The HCS12, however,
does not have a register dedicated to the function of the frame pointer. Since the stack frame
is created during a subroutine call, it is also called the activation record of the subroutine. The
stack frame exists as long as the subroutine is not exited. The structure of a stack frame is
shown in Figure 4.4.
The reason for having a dedicated frame pointer is that the stack pointer may change dur-
ing the lifetime of a subroutine. Once the stack pointer changes value, there can be problems in
accessing the variables stored in the stack frame. The frame pointer is added to point to a fixed
SP
Local variables
Saved registers
Return address
Incoming parameters
Figure 4.4 Structure of the HCS12 stack frame
 
Search WWH ::




Custom Search