DOC PREVIEW
NMT EE 308 - The stack and the stack pointer

This preview shows page 1-2-19-20 out of 20 pages.

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

Unformatted text preview:

The stack and the stack The stack and the stack pointerpointerIf you “google” the word stack, one of the definitions you will get is:If you “google” the word stack, one of the definitions you will get is:A reserved area of memory used to keep track of a program's internal operations, including functions, return addresses, passed parameters, etc. A stack is A reserved area of memory used to keep track of a program's internal operations, including functions, return addresses, passed parameters, etc. A stack is usually maintained as a "last in, first out" (usually maintained as a "last in, first out" (LIFOLIFO) data structure, so that the last item added to the structure is the first item used.) data structure, so that the last item added to the structure is the first item used.Sometimes is useful to have a region of memory for temporary storage,Sometimes is useful to have a region of memory for temporary storage, which which does not have to be allocated as named variables.does not have to be allocated as named variables. When you use subroutines and interrupts it will be essential to have such a storage When you use subroutines and interrupts it will be essential to have such a storage region.region.Such region is called a Such region is called a StackStackThe The Stack PointerStack Pointer (SP) register is used to indicate the location of the last item put onto (SP) register is used to indicate the location of the last item put onto the stack.the stack.When you PUT something ONTO the stack (When you PUT something ONTO the stack (PUSHPUSH onto the stack), the SP is decremented onto the stack), the SP is decremented beforebefore the item is placed on the stack. the item is placed on the stack.When you take something OFF of the stack (When you take something OFF of the stack (PULLPULL from the stack), the SP is incremented from the stack), the SP is incremented after the item is pulled from the stack.after the item is pulled from the stack.Before you can use a stack you have to initialize the SP to point to one value higher thanBefore you can use a stack you have to initialize the SP to point to one value higher than the highest memory location in the stack.the highest memory location in the stack.For the HC12 use a block of memory from about $3B00 to $3BFF for the stack.For the HC12 use a block of memory from about $3B00 to $3BFF for the stack.For this region of memory, initialize the stack pointer to For this region of memory, initialize the stack pointer to $3C00$3C00. Use LDS (Load Stack. Use LDS (Load Stack Pointer) to initialize the stack pointer.Pointer) to initialize the stack pointer.The stack pointer is initialized only one time in the program.The stack pointer is initialized only one time in the program.0x3BFA0x3BFB0x3BFC0x3BFD0x3BFE0x3BFF0x3C00Memoryusedby MCU(Debug12Data)The stack is an array of memory dedicated to The stack is an array of memory dedicated to temporary storagetemporary storageSP points to location last item placed in blockSP points to location last item placed in blockSP SP decreasesdecreases when you put an item on the stack when you put an item on the stackSP SP increasesincreases when you pull the item from the stack when you pull the item from the stackFor the HC12, use For the HC12, use 0x3c000x3c00 as initial SP as initial SPSTACK:STACK:EQUEQU$3C00$3C00LDSLDS#STACK#STACKA BDXYSPPCCCR0x3BFA0x3BFB0x3BFC0x3BFD0x3BFE0x3BFF0x3C00Memoryusedby MCU0x3BF60x3BF70x3BF80x3BF9An example of some code which An example of some code which uses the stackuses the stackStack pointer:Stack pointer:Initialize ONCE before the first use (LDS #STACK)Initialize ONCE before the first use (LDS #STACK)Points to last used storage locationPoints to last used storage locationDecreases when you put something on stack, and increases when you take something off stackDecreases when you put something on stack, and increases when you take something off stackSTACK:STACK:equequ$3C00$3C00ldslds#STACK#STACKldaaldaa#$2e#$2eldxldx#$1254#$1254pshapshapshxpshxclraclraldxldx#$ffff#$ffffCODE THAT USES A & XCODE THAT USES A & Xpulxpulxpulapula0x3BFA0x3BFB0x3BFC0x3BFD0x3BFE0x3BFF0x3C00Memoryusedby MCUAXSPAn example of some code which An example of some code which uses the stackuses the stackSubroutinesSubroutinesA subroutine is a section of code which performs a specific task, usually a task which needs to be executed by different parts of the program.A subroutine is a section of code which performs a specific task, usually a task which needs to be executed by different parts of the program.Example:Example:org $1000org $1000-Math functions, such as square root (sqrt)-Math functions, such as square root (sqrt) ::Because a subroutine can be called from different places in a program, you cannot getBecause a subroutine can be called from different places in a program, you cannot get :: out of a subroutine with an instruction such as out of a subroutine with an instruction such ascall sqrtcall sqrt ::????jmp labeljmp label :: call sqrtcall sqrtBecause you would need to jump to different places depending upon which section ofBecause you would need to jump to different places depending upon which section of :: the code called the subroutine. the code called the subroutine. ::swiswiWhen you want to call the subroutine your code has to save the address where When you want to call the subroutine your code has to save the address where thethe subroutine should return tosubroutine should return to. It does this by saving the return address on the . It does this by saving the return address on the stackstack..sqrt:sqrt:compute square rootcompute square root :: - This is done automatically for you when you get to the subroutine by- This is done automatically for you when you get to the subroutine by :: using using JSRJSR (Jump to Subroutine) or BSR (Branch to Subroutine) (Jump to Subroutine) or BSR (Branch to Subroutine)jmp labeljmp label instruction. This instruction pushes the address of the instruction instruction. This instruction pushes the address of the instruction following the JSR (BSR) instruction on the stackfollowing the JSR (BSR) instruction on the stackAfter the subroutine is done executing its code, it needs to After the subroutine is done executing its code, it needs to return to the address saved return to the address saved on the on the stackstack..- This is done automatically when you return from the subroutine by- This is done automatically when


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?