Chapter 4 - v3.1 - Chapter 4 The Stack Subroutines...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
29 Chapter 4 The Stack, Subroutines, Interrupts and Resets In many computers, memory is divided into three distinct areas: - program area - data area - stack The stack is an area of memory used for the temporary storage of information. Subroutines and interrupts make use of the stack. The stack has a top and a bottom. The top part of stack memory has a lower address. The bottom of the stack is fixed, but the top can change, moving up or down. The stack pointer (SP) is a register within the μ P that contains the address of the next location available for the stack. The μ P’s internal logic causes the SP to decrement automatically when data is stored in the stack and to automatically increment when it is removed. Therefore, the SP must initially be set to the highest address in the stack area (the bottom of the stack). e.g., if the stack is to occupy locations $0200 to $02FF then use LDS #$02FF instruction as initialization before using the stack. SP instructions are: - DES SP - 1 SP - INS SP + 1 SP - LDS M:M + 1 SP (in immediate, direct, extended and indexed modes) - STS SP M:M + 1 (in direct, extended and indexed modes) - TXS IX - 1 SP - TSX SP + 1 IX - TYS IY - 1 SP - TSY SP + 1 IY PUSH and PULL Instructions The push (PSH) and pull (PUL) instructions store and load data to and from the stack. PSHA (or B or X or Y) instruction writes the contents of the specified register in the stack at the SP location (at the location whose address is contained in SP) and then decrements the SP once (for PSHA and PSHB) or twice (for PSHX and PSHY) because the original stack location is no longer vacant but contains the pushed data . PULA (or B or X or Y) instruction first increments the SP once to point to the last item that has been inserted into the stack and then transfers the contents of the stack appropriately to the specified register. Note that once the contents of a stack location have been pulled, the location is considered vacant, although the data is still there. It will be overwritten by the next PUSH or other use of the stack. Also note that the stack acts as a Last-In-First-Out (LIFO) structure. A pull instruction retrieves the information that was pushed last onto the stack.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
30 (b) Pull data from stack Stack structure and stack operations Subroutines When the same function is required more than once in a program, it is frequently written as a subroutine, that is, a subprogram that can be used any number of times by the main program. This capability is provided by the following three instructions; o JSR (jump to subroutine) o BSR (branch to subroutine) o RTS (return from subroutine) The following figure illustrates the use of the same subroutine by two different parts of the main program. The subroutine located at $0200 can be entered from either location $0011 or $00CC by placing a JSR (opcode = $BD) instruction at these addresses followed by the subroutine address ($0200).
Background image of page 2
31 JSR $0200 has the machine code “BD 02 00” in the extended addressing mode. After the subroutine
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 11

Chapter 4 - v3.1 - Chapter 4 The Stack Subroutines...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online