DOC PREVIEW
NMT EE 308 - THE STACK AND THE STACK POINTER

This preview shows page 1-2-3-4-5-6 out of 18 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

EE 308 Spring 2011 • Using the stack and the stack pointer o The Stack and Stack Pointer o The stack is a memory area for temporary storage o The stack pointer points to the last byte in the stack o Some instructions which use the stack, and how data is saved and retrieved off the stack o Subroutines and the stack o An example of a simple subroutine o Using a subroutine with PORTA to make a binary counter on LEDs THE STACK AND THE STACK POINTER • Sometimes it is useful to have a region of memory for temporary storage, which does not have to be allocated as named variables. • When we use subroutines and interrupts it will be essential to have such a storage region. • Such a region is called a Stack. • The Stack Pointer (SP) register is used to indicate the location of the last item put onto the stack. • When you put something onto the stack (push onto the stack), the SP is decremented before the item is placed on the stack. • When you take something off of the stack (pull from the stack), the SP is incremented after the item is pulled from the stack. • Before you can use a stack you have to initialize the Stack Pointer to point to one value higher than the highest memory location in the stack. • For the MC9S12 put the stack at the top of the data space – For most programs, use $1000 through $2000 for data. – For this region of memory, initialize the stack pointer to $2000. – If you need more space for data and the stack, and less for your program, move the program to a higher address, and use this for the initial value of the stack pointer.EE 308 Spring 2011 • Use the LDS (Load Stack Pointer) instruction to initialize the stack point. • The LDS instruction is usually the first instruction of a program which uses the stack. • The stack pointer is initialized only one time in the program. • For microcontrollers such as the MC9S12, it is up to the programmer to know how much stack his/her program will need, and to make sure enough space is allocated for the stack. If not enough space is allocated the stack can overwrite data and/or code, which will cause the program to malfunction or crash.EE 308 Spring 2011 The stack is an array of memory dedicated to temporary storage SP points to the location last item placed in block SP decreases when you put an item on stack SP increases when you pull item from stack For HC12 EVBU, use 0x3C00 as initial SP: STACK: EQU $2000 LDS #STACK ↑ 0x1EF5 0x1EF6 0x1EF7 . . . 0x1EFF 0x1F00 0x1F01 0x1F02 ↓EE 308 Spring 2011 An example of some code which used the stack Stack Pointer Initialize ONCE before first use (LDS #STACK) Points to last used storage location Decreases when you put something on stack Increases when you take something off stack STACK: equ $2000 CODE: org $2000 lds #STACK ldaa #$2e ldx #$1254 psha pshx clra ldx #$ffff CODE THAT USES A & X pulx pula ↑ 0x1FF5 0x1FF6 0x1FF7 . . . 0x1FFB 0x1FFC 0x1FFD 0x1FFE 0X1FFFEE 308 Spring 2011EE 308 Spring 2011 Subroutines • A subroutine is a section of code which performs a specific task, usually a task which needs to be executed by different parts of a program. • Example: – Math functions, such as square root • Because a subroutine can be called from different places in a program, you cannot get out of a subroutine with an instruction such as jmp label because you would need to jump to different places depending upon which section of code called the subroutine. • When you want to call the subroutine your code has to save the address where the subroutine should return to. It does this by saving the return address on the stack. – This is done automatically for you when you get to the subroutine by using the JSR (Jump to Subroutine) or BSR (Branch to Subroutine) instruction. This instruction pushes the address of the instruction following the JSR/BSR instruction on the stack. • After the subroutine is done executing its code it needs to return to the address saved on the stack. – This is done automatically for you when you return from the subroutine by using the RTS (Return from Subroutine) instruction. This instruction pulls the return address off of the stack and loads it into the program counter, so the program resumes execution of the program with the instruction following that which called the subroutine. The subroutine will probably need to use some MC9S12 registers to do its work. However, the calling code may be using its registers for some reason - the calling code may not work correctly if the subroutine changes the values of the HC12 registers. – To avoid this problem, the subroutine should save the MC9S12 registers before it uses them, and restore the MC9S12 registers after it is done with them.EE 308 Spring 2011EE 308 Spring 2011EE 308 Spring 2011 Example of a subroutine to delay for a certain amount of time ; Subroutine to wait for 100 ms delay: ldaa #100 ; execute outer loop 100 times loop2: ldx #8000 ; want inner loop to last 1ms loop1: dbne x,loop1 ; inner loop – 3 cycles x 8000 times dbne a,loop2 rts • Want inner loop to last for 1 ms. MC9S12 runs at 24,000,000 cycles/second, so 1 ms is 24,000 cycles. • Inner loop should be 24,000 cycles/ (3 cycles/loop) = 8,000 loops (times) • Problem: The subroutine changes the values of registers A and X • To solve this problem, save the values of A and X on the stack before using them, and restore them before returning. ; Subroutine to wait for 100 ms delay: psha ; save registers pshx ldaa #100 ; execute outer loop 100 times loop2: ldx #8000 ; want inner loop to last 1ms loop1: dbne x,loop1 ; inner loop – 3 cycles x 8000 times dbne a,loop2 pulx ; restore registers pula rtsEE 308 Spring 2011 ; ; Program to make a binary counter on LEDs ; ; The program uses a subroutine to insert a delay ; between counts ; ; Does not work on Dragon12-Plus. Need to write to PTJ ; to enable LEDs prog: equ $2000 data: equ $1000 STACK: equ $2000 PORTB: equ $0001 DDRB: equ $0003 org prog lds #STACK ;


View Full Document

NMT EE 308 - THE STACK AND THE STACK POINTER

Documents in this Course
Load more
Download THE STACK AND THE STACK POINTER
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view THE STACK AND THE STACK POINTER and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view THE STACK AND THE STACK POINTER 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?