• No results found

6.1 Temporary Storage Using a Stack

Often during the execution of a program there is a need to temporarily store some data in memory. Most computer systems provide an area of memory for temporary storage called the stack. Functionally, the stack is sequential block of memory configured as a LIFO (last-in, first-out). Data is written to the stack in the opposite order to that in which it is retrieved from the stack. Specifically, the last byte written to the stack is on the top of the stack; therefore, it must be the first byte taken off the stack before other data can be accessed. The concept of how this works is illustrated in Figure 6.1.

The stack of plates is a good example of how a stack works on a computer. The first plate placed on the stack ends up on the bottom of the stack. The last plate placed on the stack is on the top. When the plates are removed from the stack, they are removed from the top first; thus the last one on the stack is the first one off. A memory stack is

138

Figure 6.1 Examples of Stacks

Top Bottom Last plate placed on stack First plate placed on stack a) Stack of dinner plates

Top Bottom xx xx xx xx xx xx Last byte placed on stack First byte placed on stack

b) Memory Stack of bytes $0041 $003C $003D $003E $003F $0040

a block of sequential locations in memory. The first location used is the bottom of the stack, in the same way the first plate is on the bottom of the stack. This becomes the highest address location in the stack. The last location, or the top of the stack, will be the lowest address location used by the stack, as shown in Figure 6.1b. In this example, six locations are used. The bottom of the stack is at location $0041 in memory; and the top of the stack is at location $003C.

As the data moves to and from the stack, the address of the next available location on the memory stack is changing. After data is placed on the stack, the address is decremented. After data is removed from the stack, the address is incremented. This address is called the stack pointer, and the processor keeps it in the stack pointer register. The HC11 uses a 16-bit address as the stack pointer register so that any location within the HC11 memory map can be used as the stack.

The HC11 supports a group of special instructions designed to manage the data on the stack. Push instructions are a special type of data movement instructions that are used to place data on the stack. They copy the data from a processor register and place it on the stack. They are also responsible for decrementing the stack pointer so that it continues to point to the next available location on the memory stack.

The mnemonic form for push instructions is PSHx. Data can be pushed onto the stack from the A, B, X or Y registers using the PSHA, PSHB, PSHX or PSHY instructions, as shown in Figure 6.2. Since the source of the data is included in the mnemonic and the address of the data destination is indicated by the address in the stack pointer register, all push instructions use the inherent addressing mode. Push instructions have no effect on the CCR. Push instructions are complementary to pull instructions.

Pull instructions are a special type of data movement instructions that are used to remove data from the stack. They copy the data from the stack and place it in a processor register. They first increment the stack pointer so that it points to the last byte written to the stack. When the data has been copied to the processor register, the location it occupied on the stack is now available for another byte of data. Since the stack pointer already points to this location, it does not have to be updated at the end of the instruction.

Mnemonic Description Function IMM DIR EXT INDX INDY INH REL S X H I N Z V C

PSHA Push 8 bits from AccA onto Stack, Decrement S

(A) ⇒ MS

(S) – 1 ⇒ S - - - X - - - -

PSHB Push 8 bits from AccB onto Stack, Decrement S

(B) ⇒ MS

(S) – 1 ⇒ S - - - X - - - -

PSHX Push 16 bits from X onto Stack, Decrement S (XL) ⇒ MS (S) – 1 ⇒ S (XH) ⇒ MS (S) – 1 ⇒ S - - - X - - - -

PSHY Push 16 bits from Y onto Stack, Decrement S (YL) ⇒ MS (S) – 1 ⇒ S (YH) ⇒ MS (S) – 1 ⇒ S - - - X - - - -

Figure 6.2 Push Instructions

139

The mnemonic form for pull instructions is PULx. Data can be pulled from the stack and loaded into the A, B, X or Y registers using the PULA, PULB, PULX or PULY instructions, as shown in Figure 6.3. Since the destination of the data is included in the mnemonic and the address of the data source is indicated by the address in the stack pointer register, all pull instructions use the inherent addressing mode. Pull instructions have no effect on the CCR. Pull instructions are complementary to push instructions.

Example 6.1

Problem: For each of the following push or pull instructions, indicate what effect the instruction has on the registers or memory.

0030 81 22 33 44 55 66 77 88

A $80 X $B600

B $FF S $0035

Address Machine Code Source Code a. 0109 36 PSHA

b. 0100 32 PULB c. 01E2 38 PULX

Solution:

a. (A) →$0035, $0034 → S

PSHA writes the contents of AccA ($80) to location $0035 in memory overwriting the previous data in this location. The stack pointer is decremented to $0034 after the data is pushed.

b. $0036 → S, ($0036) → B

PULB increments the stack pointer to point to the last location used on the stack ($0036). It then reads the contents of location $0036 from memory and loads AccB with the data ($77).

140

Mnemonic Description Function IMM DIR EXT INDX INDY INH REL S X H I N Z V C

PULA Increment S, Pull 8- bits from Stack into AccA

(S) + 1 ⇒ S

(MS) ⇒ A - - - X - - - -

PULB Increment S, Pull 8- bits from Stack into AccB

(S) + 1 ⇒ S

(MS) ⇒ B - - - X - - - -

PULX Increment S, Pull 16-bits from Stack into X register (S) + 1 ⇒ S (MS) ⇒ XH (S) + 1 ⇒ S (MS) ⇒ XL - - - X - - - -

PULY Increment S, Pull 16-bits from Stack into Y register (S) + 1 ⇒ S (MS) ⇒ YH (S) + 1 ⇒ S (MS) ⇒ YL - - - X - - - -

Figure 6.3 Pull Instructions

c. $0036 → S, ($0036) → XL, $0037 → S, ($0037) → XH.

PULX increments the stack pointer to point to the last location used on the stack ($0036). It then reads the contents of location $0036 from memory and loads the low byte of the X register with the data ($77). The stack pointer is incremented again to $0037, followed by a read of that new location. The data is loaded into the high byte of the X register ($88).