200 likes | 226 Views
Learn the fundamentals of stack memory in microcontrollers, including its programming, application in subroutines, and push/pull instructions. Explore how stack memory occupies a small section of RAM and is used for temporary data storage.
E N D
Microcontroller Fundamentals & Programming Stack and its Applications
Stack Memory Stack or stack memory • occupied a small section of RAM • reserved for saving or retrieving of temporary data by CPU registers. • uses LIFO (last in, first out) stack memory or Push down, Pop up stack operation
Stack Memory • Application of stack memory e.g. Executing of subroutine instructions that pushes CPU registers into the stack. • Stack Pointer (SP) • a 16-bit CPU register • holds the address of the next free location of the stack memory.
Stack Memory Instructions PUSH Executing this instruction: • save CPU register contents to stack. • decrement SP register values by one or two depend on register; an 8-bit or a 16-bit type being saved into the stack.
Stack Memory Instructions PULL Executing this instruction will: • retrieve data from stack and place them onto the register. • increment the stack pointer (SP) register values by one or two depend on register is an 8-bit or a 16-bit type.
Example of PUSH Instructions LDS #$01FF ; SP = $01FF PSHA ; SP = SP - 1 = $01FE ; ($01FF) = contents of ACCA PSHB ; SP = SP - 1 = $01FD ; ($01FE) = contents of ACCB PSHX ; SP = SP – 2 = $01FB ; ($01FD&C) = contents of IX PSHY ; SP = SP – 2 = $01F9 ; ($01FB&A) = contents of IY WAI
Example of PULL Instructions LDS #$01F0 ; SP = $01F0 PULA ;ACCA = contents of $01F0 ; SP = SP + 1 = $01F1 PULB ;ACCB = contents of $01F1 ; SP = SP + 1 = $01F2 PULX ;IX = contents of $01F2 & $01F3 ; SP = SP + 2 = $01F4 PULY ; IY = contents of $01F4 & $01F5 ; SP = SP + 2 = $01F6 WAI
PUSH operation LDS #$01FF LDAA #$22 LDAB #$33 LDX #$8899 PSHA PSHB PSHX - - - - - PULX PULB PULA - - - - - WAI 01FF SP Address Data $0100 - - - - - - - - PUSH$01FA SP-2 IX $8899 $01FB $01FC 88 SP-1 ACCB $33 $01FD 99 SP-1 ACCA $22 $01FE 33 SP $01FF 22
PULL operation LDS #$01FF LDAA #$22 LDAB #$33 LDX #$8899 PSHA PSHB PSHX - - - - - PULX PULB PULA - - - - - WAI • Address Data • $0100 • - - - • - - - - • $01FA • $01FB SP • $01FC 88 $8899 IXSP+2 • $01FD 99 • $01FE 33 $33 ACCBSP+1 • $01FF 22 $22 ACCA SP+1 PULL
Stack Application – Subroutine • A small sequence of instructions written to perform a specified task. • Called from different location of program – BSR, JSR. • Return to the right location upon completion of subroutine – RTS 15
Subroutine Operation • On calling subroutine, BSR/JSR, the next PC value is saved onto stack. • Upon completing the subroutine, RTS, retrieved return address stored in stack and loaded into PC • Subroutine example: ORG $0100 $0100 LDS #$DFFF $0103 LDAA #$55 $0105 BSR TOGGLE $0107 STAA PORTB TOGGLE: …… ...… …… RTS
PC $DFFB $D9 $DFFC $EF $DFFD $C8 $DFFE $A7 $DFFF $40 $E000 Example PC : $0100 A : $00 B : $00 IX : $0000 SP : $0000 ORG $0100 $0100 LDS #$DFFF $0103 LDAA #55 $0105 BSR TOGGLE $0107 STAA PORTB $01… … … … $013F TOGGLE: $0140 ... … … … … … … … $0150 RTS 17
PC $DFFB $D9 $DFFC $EF $DFFD $C8 $DFFE $A7 $DFFF $40 $E000 Example PC : $0103 A : $00 B : $00 IX : $0000 SP : $DFFF ORG $0100 $0100 LDS #$DFFF $0103 LDAA #55 $0105 BSR TOGGLE $0107 STAA PORTB $01… … … … $013F TOGGLE: $0140 ... … … … … … … … $0150 RTS 17
PC $DFFB $D9 $DFFC $EF $DFFD $C8 $DFFE $A7 $DFFF $40 $E000 Example PC : $0105 A : $55 B : $00 IX : $0000 SP : $DFFF ORG $0100 $0100 LDS #$DFFF $0103 LDAA #55 $0105 BSR TOGGLE $0107 STAA PORTB $01… … … … $013F TOGGLE: $0140 ... … … … … … … … $0150 RTS 17
SP PC $DFFB $D9 $DFFC $EF $DFFD $C8 $DFFE $01 $DFFF $07 $E000 Example PC : $013F A : $55 B : $00 IX : $0000 SP : $DFFD ORG $0100 $0100 LDS #$DFFF $0103 LDAA #55 $0105 BSR TOGGLE $0107 STAA PORTB $01… … … … $013F TOGGLE: $0140 ... … … … … … … … $0150 RTS 17
SP PC $DFFB $D9 $DFFC $EF $DFFD $C8 $DFFE $01 $DFFF $07 $E000 Example PC : $0140 A : $55 B : $00 IX : $0000 SP : $DFFF ORG $0100 $0100 LDS #$DFFF $0103 LDAA #55 $0105 BSR TOGGLE $0107 STAA PORTB $01… … … … $013F TOGGLE: $0140 ... … … … … … … … $0150 RTS 17
SP PC $DFFB $D9 $DFFC $EF $DFFD $C8 $DFFE $01 $DFFF $07 $E000 Example PC : $0150 A : $55 B : $00 IX : $0000 SP : $DFFF ORG $0100 $0100 LDS #$DFFF $0103 LDAA #55 $0105 BSR TOGGLE $0107 STAA PORTB $01… … … … $013F TOGGLE: $0140 ... … … … … … … … $0150 RTS 17
SP PC $DFFB $D9 $DFFC $EF $DFFD $C8 $DFFE $01 $DFFF $07 $E000 Example PC : $0107 A : $55 B : $00 IX : $0000 SP : $DFFF ORG $0100 $0100 LDS #$DFFF $0103 LDAA #55 $0105 BSR TOGGLE $0107 STAA PORTB $01… … … … $013F TOGGLE: $0140 ... … … … … … … … $0150 RTS 17
Summary • Dedicated First-In-Last-Out memory for storing and retrieving of data. • Use of stack pointer to keep track of stack. • Push and Pull instructions. • PSHA/PSHB – 1 memory address • PSHX/PSHY – 2 memory address • Operation of Subroutine.