Presentation is loading. Please wait.

Presentation is loading. Please wait.

Subroutines and Stacks. Stack The stack is a special area in memory used by the CPU to store register information or general data information during program.

Similar presentations


Presentation on theme: "Subroutines and Stacks. Stack The stack is a special area in memory used by the CPU to store register information or general data information during program."— Presentation transcript:

1 Subroutines and Stacks

2 Stack The stack is a special area in memory used by the CPU to store register information or general data information during program execution The stack has a top and a bottom The SP register is the special register that controls the address in the stack

3 The 68HC11 Stack SP low address high address top element bottom...... -A 16-bit stack pointer (SP) points to the next available location in the stack. -The 68HC11 CPU registers can be pushed into the stack. -The top element (s) of the stack can be pulled into a CPU register. -The stack grows from high addresses toward lower addresses. Push and Pull Instructions -PSHA: Push A onto the stack -PSHB:Push B onto the stack -PSHX:Push X onto the stack (low order byte is pushed first) -PSHY:Push Y onto the stack (low order byte is pushed first) -PULA:Pull A from the stack -PULB:Pull B from the stack -PULX:Pull X from the stack (high order byte is pulled first) -PULY:Pull Y from the stack (high order byte is pulled first)

4 Example: Suppose that [A] = $33, [B] = $20, [SP] = $00FF. What will be the contents of the top byte of the stack before and after the execution of PSHA? What will be the contents of the top two bytes of the stack if PSHB is executed? Solution: The contents of the stack before and after the execution of PSHA and PSHB are: XX original stack [SP] = $00FF $33 XX [SP] = $00FE XX $20 $33 [SP] = $00FD after PSHAafter PSHB

5 Stack as a storage Buffer ORG $0200 ;start address of program BEGINLDS #$FF ;You must define the stack first LDAA $1031 ;Get first data, x TAB ;and square it MUL ADCA #$00 ;round it to 8-bit result PSHA ;and save it LDAA $1032 ;Get second data, y TAB ;and square it MUL ADCA #$00 ;round it to 8-bit result PULB ;retrieve first result ABA ;and add them HEREBRA HERE

6 Subroutine A subroutine is a self-contained subprogram. Each subroutine contains one or more instructions associated with a specific task. To call a subroutine JSR (jump to subroutine) and BSR (Branch to subroutine) instructions can be used. Each subroutine must end with RTS (Return From Subroutine) instruction. Whenever a JSR/BSR instruction is executed, the address of the instruction following the JSR/BSR is copied into the Stack (the contents of PC is pushed into the Stack). Once that the subroutine has ended (RTS), the address of the following instruction in the main program will be pulled from the Stack into the PC.

7 Program example

8 Subroutine Example

9 Continue

10 Chapter problems 1,2,3


Download ppt "Subroutines and Stacks. Stack The stack is a special area in memory used by the CPU to store register information or general data information during program."

Similar presentations


Ads by Google