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

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

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 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 19 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 19 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 19 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 19 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 19 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

EE 308 Spring 2009 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 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 1 EE 308 Spring 2009 The stack is an array of memory dedicated to temporary storage SP points to location last item placed in block SP decreases when you put item on stack 0x1EF5 0x1EF6 SP increases when you pull item from stack For MC9S12 use 0x2000 as initial SP STACK 0x1EF7 EQU LDS 2000 STACK 0x1EF8 0x1EF9 0x1EFA A B D 0x1EFB 0x1EFC 0x1EFD X Y 0x1EFE 0x1FFF 0x1F00 SP PC 0x1F01 CCR 0x1F02 0x1F03 2 EE 308 Spring 2009 An example of some code which uses 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 0x1FF5 CODE org 2000 0x1FF6 0x1FF7 0x1FFB lds ldaa ldx psha pshx clra ldx 0x1FFC CODE THAT USES A X 0x1FFD pulx pula 0x1FF8 0x1FF9 0x1FFA 0x1FFE A 0x1FFF 0x2000 X SP 3 STACK 2e 1254 ffff EE 308 Spring 2009 Core User Guide S12CPU15UG V1 2 PSHA Operation PSHA Push A onto Stack SP 0001 SP A MSP Decrements SP by one and loads the value in A into the address to which SP points Push instructions are commonly used to save the contents of one or more CPU registers at the start of a subroutine Complementary pull instructions can be used to restore the saved CPU registers just before returning from the subroutine CCR Effects S X H I N Z V C Code and CPU Cycles Source Form PSHA Address Mode INH Machine Code Hex 36 CPU Cycles Os 439 4 EE 308 Spring 2009 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 MC9S12 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 5 EE 308 Spring 2009 Core User Guide S12CPU15UG V1 2 BSR Operation BSR Branch to Subroutine SP 0002 SP RTNH RTNL MSP MSP 1 PC 0002 rel PC Sets up conditions to return to normal program flow then transfers control to a subroutine Uses the address of the instruction after the BSR as a return address Decrements the SP by two to allow the two bytes of the return address to be stacked Stacks the return address the SP points to the high byte of the return address Branches to a location determined by the branch offset Subroutines are normally terminated with an RTS instruction which restores the return address from the stack CCR Effects S X H I N Z V C Code and CPU Cycles Source Form BSR rel8 Address Mode REL Machine Code Hex 07 rr CPU Cycles SPPP 333 6 EE 308 Spring 2009 Core User Guide S12CPU15UG V1 2 RTS Operation RTS Return from Subroutine MSP MSP 1 PCH PCL SP 0002 SP Restores the value of PC from the stack and increments SP by two Program execution continues at the address restored from the stack CCR Effects S X H I N Z V C Code and CPU Cycles Source Form RTS Address Mode INH Machine Code Hex 3D CPU Cycles UfPPP 463 7 EE 308 Spring 2009 Example of a subroutine to delay for a certain amount of time Subroutine to wait for 100 ms delay loop2 loop1 ldaa ldx dbne dbne rts 100 Execute outer loop 100 times 8000 Want inner loop to last 1 ms x loop1 Inner loop 3 cycles x 8000 times a loop2 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 times Problem The subroutine changes the values of registers A and X To solve save the values of …


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?