1 / 20

Microcontroller Fundamentals & Programming

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.

wrightwayne
Download Presentation

Microcontroller Fundamentals & Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Microcontroller Fundamentals & Programming Stack and its Applications

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  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

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

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

  20. Thank You

More Related