Microcontroller Fundamentals Programming Stack and its Applications Stack

  • Slides: 20
Download presentation
Microcontroller Fundamentals & Programming Stack and its Applications

Microcontroller Fundamentals & Programming Stack and its Applications

Stack Memory Stack or stack memory § occupied a small section of RAM §

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 2

Stack Memory § Application of stack memory e. g. Executing of subroutine instructions that

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. 3

Stack Memory Instructions PUSH Executing this instruction: § save CPU register contents to stack.

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. 4

Stack Memory Instructions PULL Executing this instruction will: § retrieve data from stack and

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. 5

Example of PUSH Instructions LDS #$01 FF ; SP = $01 FF PSHA ;

Example of PUSH Instructions LDS #$01 FF ; SP = $01 FF PSHA ; SP = SP - 1 = $01 FE ; ($01 FF) = contents of ACCA PSHB ; SP = SP - 1 = $01 FD ; ($01 FE) = contents of ACCB PSHX ; SP = SP – 2 = $01 FB ; ($01 FD&C) = contents of IX PSHY ; SP = SP – 2 = $01 F 9 ; ($01 FB&A) = contents of IY WAI 6

Example of PULL Instructions LDS #$01 F 0 ; SP = $01 F 0

Example of PULL Instructions LDS #$01 F 0 ; SP = $01 F 0 PULA ; ACCA = contents of $01 F 0 ; SP = SP + 1 = $01 F 1 PULB ; ACCB = contents of $01 F 1 ; SP = SP + 1 = $01 F 2 PULX ; IX = contents of $01 F 2 & $01 F 3 ; SP = SP + 2 = $01 F 4 PULY ; IY = contents of $01 F 4 & $01 F 5 ; SP = SP + 2 = $01 F 6 WAI 7

PUSH operation LDS #$01 FF LDAA #$22 LDAB #$33 LDX #$8899 PSHA PSHB PSHX

PUSH operation LDS #$01 FF LDAA #$22 LDAB #$33 LDX #$8899 PSHA PSHB PSHX ----PULX PULB PULA ----- SP 01 FF Address Data $0100 ------PUSH SP-2 IX $01 FA $8899 $01 FB $01 FC 88 SP-1 ACCB $33 $01 FD 99 SP-1 ACCA $22 $01 FE 33 $01 FF 22 SP WAI 8

PULL operation LDS LDAA LDAB LDX PSHA PSHB PSHX ----PULX PULB PULA ----- #$01

PULL operation LDS LDAA LDAB LDX PSHA PSHB PSHX ----PULX PULB PULA ----- #$01 FF #$22 #$33 #$8899 Address Data $0100 -- - - PULL -- - - $01 FA $01 FB SP $01 FC 88 $8899 IX SP+2 $01 FD 99 $01 FE 33 $33 ACCB SP+1 $01 FF 22 $22 ACCA SP+1 WAI 9

Stack Application – Subroutine § A small sequence of instructions written to perform a

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 10 15

Subroutine Operation § On calling subroutine, BSR/JSR, the next PC value is saved onto

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: $0100 $0103 $0105 $0107 ORG LDS LDAA BSR STAA $0100 #$DFFF #$55 TOGGLE PORTB TOGGLE: ……. . . … …… RTS 11

Example PC $0100 $0103 $0105 $0107 $01… $013 F TOGGLE: $0140 …… $0150 ORG

Example PC $0100 $0103 $0105 $0107 $01… $013 F TOGGLE: $0140 …… $0150 ORG $0100 PC : $0100 LDS #$DFFF A : $00 B : $00 LDAA #55 IX : $0000 BSR TOGGLE SP : $0000 STAA PORTB ……… $D 9 $DFFB. . . … … ……… RTS $DFFC $DFFD $DFFE $DFFF $E 000 $EF $C 8 $A 7 $40 12 17

Example PC $0100 $0103 $0105 $0107 $01… $013 F TOGGLE: $0140 …… $0150 ORG

Example PC $0100 $0103 $0105 $0107 $01… $013 F TOGGLE: $0140 …… $0150 ORG $0100 PC : $0103 LDS #$DFFF A : $00 B : $00 LDAA #55 IX : $0000 BSR TOGGLE SP : $DFFF STAA PORTB ……… $D 9 $DFFB. . . … … ……… RTS $DFFC $DFFD $DFFE $DFFF $E 000 $EF $C 8 $A 7 $40 13 17

Example PC $0100 $0103 $0105 $0107 $01… $013 F TOGGLE: $0140 …… $0150 ORG

Example PC $0100 $0103 $0105 $0107 $01… $013 F TOGGLE: $0140 …… $0150 ORG $0100 PC : $0105 LDS #$DFFF A : $55 B : $00 LDAA #55 IX : $0000 BSR TOGGLE SP : $DFFF STAA PORTB ……… $D 9 $DFFB. . . … … ……… RTS $DFFC $DFFD $DFFE $DFFF $E 000 $EF $C 8 $A 7 $40 14 17

Example PC $0100 $0103 $0105 $0107 $01… $013 F TOGGLE: $0140 …… $0150 ORG

Example PC $0100 $0103 $0105 $0107 $01… $013 F TOGGLE: $0140 …… $0150 ORG $0100 PC : $013 F LDS #$DFFF A : $55 B : $00 LDAA #55 IX : $0000 BSR TOGGLE SP : $DFFD STAA PORTB ……… $D 9 $DFFB. . . … … ……… RTS SP $DFFC $DFFD $DFFE $DFFF $E 000 $EF $C 8 $01 $07 15 17

Example PC $0100 $0103 $0105 $0107 $01… $013 F TOGGLE: $0140 …… $0150 ORG

Example PC $0100 $0103 $0105 $0107 $01… $013 F TOGGLE: $0140 …… $0150 ORG $0100 PC : $0140 LDS #$DFFF A : $55 B : $00 LDAA #55 IX : $0000 BSR TOGGLE SP : $DFFF STAA PORTB ……… $D 9 $DFFB. . . … … ……… RTS SP $DFFC $DFFD $DFFE $DFFF $E 000 $EF $C 8 $01 $07 16 17

Example PC $0100 $0103 $0105 $0107 $01… $013 F TOGGLE: $0140 …… $0150 ORG

Example PC $0100 $0103 $0105 $0107 $01… $013 F TOGGLE: $0140 …… $0150 ORG $0100 PC : $0150 LDS #$DFFF A : $55 B : $00 LDAA #55 IX : $0000 BSR TOGGLE SP : $DFFF STAA PORTB ……… $D 9 $DFFB. . . … … ……… RTS SP $DFFC $DFFD $DFFE $DFFF $E 000 $EF $C 8 $01 $07 17 17

Example PC $0100 $0103 $0105 $0107 $01… $013 F TOGGLE: $0140 …… $0150 ORG

Example PC $0100 $0103 $0105 $0107 $01… $013 F TOGGLE: $0140 …… $0150 ORG $0100 PC : $0107 LDS #$DFFF A : $55 B : $00 LDAA #55 IX : $0000 BSR TOGGLE SP : $DFFF STAA PORTB ……… $D 9 $DFFB. . . … … ……… RTS SP $DFFC $DFFD $DFFE $DFFF $E 000 $EF $C 8 $01 $07 18 17

Summary § Dedicated First-In-Last-Out memory for storing and retrieving of data. § Use of

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. 19

Thank You 20

Thank You 20