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

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

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

Unformatted text preview:

• More on programming in assembly language • Introduction to Ports on the HC12 • Huang Sections 7.1 through 7.5 o Good programming style o Tips for writing programs o Input and Output Ports  Simplified Input Port  Simplified Output Port o Ports on the HC12  PORTA, PORTB, DDRA, DDRB  A simple program to use PORTA and PORTB 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 HC12 use a block of memory from about $3B00 to $3BFF for the stack.• For this region of memory, initialize the stack pointer to $3C00.• 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 HC12, 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.The stack is an array of memory dedicated to temporary storage↑0x3AF50x3AF60x3AF7...0x3BFF0x3B000x3B010x3B02↓SP points to the location last item placed in blockSP decreases when you put an item on stackSP increases when you pull item from stackFor HC12 EVBU, use 0x3C00 as initial SP:STACK: EQU $3C00 LDS #STACKAn example of some code which used the stack↑0x3BF50x3BF60x3BF7...0x3BFC0x3BFD0x3BFE0x3BFF0X3C00 ↓Stack PointerInitialize ONCE before first use (LDS #STACK)Points to last used storage locationDecreases when you put something on stackIncreases when you take something off stackSTACK: EQU $3C00 org 0x1000 lds #STACK ldaa #$2e ldx #$1254 psha pshx clra ldx #$ffff CODE THAT USES A & X pulx pulaSubroutines• A subroutine is a section of code which performs a specific task, usuallya 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 asjmp labelbecause you would need to jump to different places depending uponwhich section of code called the subroutine.• When you want to call the subroutine your code has to save the addresswhere the subroutine should return to. It does this by saving the returnaddress on the stack.– This is done automatically for you when you get to the subroutine byusing the JSR (Jump to Subroutine) or BSR (Branch to Subroutine)instruction. This instruction pushes the address of the instructionfollowing the JSR (BSR) instruction on the stack.• After the subroutine is done executing its code it needs to return to theaddress saved on the stack.– This is done automatically for you when you return from the subroutineby using the RTS (Return from Subroutine) instruction. Thisinstruction pulls the return address off of the stack and loads it intothe program counter, so the program resumes execution of the programwith the instruction following that which called the subroutine.The subroutine will probably need to use some HC12 registers to do itswork. However, the calling code may be using its registers for some reason— the calling code may not work correctly if the subroutine changesthe values of the HC12 registers.– To avoid this problem, the subroutine should save the HC12 registersbefore it uses them, and restore the HC12 registers after it is donewith them.Example of a subroutine to delay for a certain amount of timedelay: ldaa #250loop2: ldx #800loop1: dex bne loop1 deca bne loop2 rts• Problem: The subroutine changes the values of registers A and X• To solve, save the values of A and X on the stack before using them, andrestore them before returning.delay: psha ; Save regs used by sub on stack pshx ldaa #250loop2: ldx #800loop1: dexbne loop1decabne loop2pulx ; Restore regs in oppositepula ; orderrts; Program to make a binary counter on LEDs;; The program uses a subroutine to insert a delay; between countsprog: equ $1000STACK: equ $3C00 ;Stack ends of $3BFFPORTA: equ $0000PORTB: equ $0001DDRA: equ $0002DDRB: equ $0003org proglds #STACK ; initialize stack pointerldaa #$ff ; put all ones into DDRAstaa DDRA ; to make PORTA outputclr PORTA ; put $00 into PORTAloop: jsr delay ; wait a bitinc PORTA ; add one to PORTAbra loop ; repeat forever; Subroutine to wait for a few millisecondsdelay: pshapshxldaa #250loop2: ldx #800loop1: dexbne loop1decabne loop2pulxpulartsJSR and BSR place return address on stackRTS returns to instruction after JSR or BSR3c00 STACK: EQU $3C001000 ORG $1000 1000 cf 3c 00 LDS #STACK1003 16 10 07 JSR MY_SUB1006 3f SWI1007 ce 12 34 MY_SUB: LDX #$1234100a 3d RTSAnother example of using a subroutine; Program fragment to write the word "hello" to the; HC12 serial portldx $strloop: ldaa 1,x+ ; get next charbeq done ; char == 0 => no morejsr putcharbra loopswistr: dc.b "hello"fc.b $0A,$0D,0 ; CR LFHere is the complete program to write a line to the screen:prog: equ $1000data: equ $2000stack: equ $3c00org proglds #stackldx #strloop: ldaa 1,x+ ; get next charbeq done ; char == 0 => no


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?