Hardware Reference
In-Depth Information
ldab
#40
pshb
ldx
#0
pshx
Solution: The first instruction initializes the stack pointer to $1500. The second and the third
instructions together push the 8-bit value $20 into the stack. The fourth and fifth instruc-
tions push the 8-bit value 40 (hex $28) into the stack. The sixth and seventh instructions
push the value 0 to the top 2 bytes of the stack. The contents of the HCS12 stack are shown
in Figure 4.2.
SP (= $14FC)
0
0
$28
$20
Figure 4.2 The contents of the HCS12 stack
One of the main uses of the stack is saving the return address for a subroutine call. Before
the stack can be used, we need to set up the stack pointer. Since the stack can only be imple-
mented in the RAM, we must make sure not to violate this requirement. Because the stack is a
last-in-first-out data structure, it can be used to reverse a data structure.
4.4 What Is a Subroutine?
A subroutine is a sequence of instructions that can be called from many different places in a pro-
gram. A key issue in a subroutine call is to make sure that the program execution returns to the point
immediately after the subroutine call (this address is called the return address ) when the subroutine
completes its execution. This is normally achieved by saving and retrieving the return address in and
from the stack. The program flow change involved in a subroutine call is illustrated in Figure 4.3.
Caller
<call> subroutine_x
subroutine_x
<return>
Figure 4.3 Program flow during a subroutine call
 
Search WWH ::




Custom Search