1 / 18

EET 2261 Unit 6 The Stack; Subroutines

EET 2261 Unit 6 The Stack; Subroutines. Read Almy , Section 11 . Homework #6 and Lab #6 due next week. Quiz next week. The Stack. The stack is a section of RAM used for temporary storage. The term “ LIFO ” (last-in-first-out) is often used to describe the stack.

kaiser
Télécharger la présentation

EET 2261 Unit 6 The Stack; Subroutines

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. EET 2261 Unit 6The Stack; Subroutines • Read Almy, Section 11. • Homework #6 and Lab #6 due next week. • Quiz next week.

  2. The Stack • The stack is a section of RAM used for temporary storage. • The term “LIFO” (last-in-first-out) is often used to describe the stack. • The last value you put in is the first one you’ll take out, as in a plate dispenser at a salad bar.

  3. The Stack Pointer • The programmer gets to decide where in RAM the stack is located. She does this by setting the value in the stack pointer register (SP). • This value is treated as the address of the “top” of the stack. • Usually the address used is the highest RAM address, plus one. For us, that address is $4000. • So in many programs, one of the first instructions will be LDS #$4000.

  4. Pushing and Pulling • We use the word “push” for putting values onto the stack, and the word “pull” for retrieving values from the stack. • See next slide for push and pull instructions. • As we push or pull, the stack pointer (SP) is automatically updated to reflect the fact that the top of the stack has moved.

  5. Push Instructions and Pull Instructions (Table from p. 82 of the HCS12 CPU Reference Manual.)

  6. Using the stack SLIDE NEEDS UPDATING • on PSHA execute, registercontents are saved on thestack, SP decrements by 1 • for the PSHY instruction,SP is decremented by 2,since Y is a 16-bit register • Examples 2-10 & 2-11 demonstrate the PULL instructions

  7. Other Uses of the Stack • As seen above, the programmer can use the stack by issuing push or pull instructions. • Also, under certain conditions that we’ll study later, the HCS12 automatically pushes data onto the stack or pulls data off the stack. No push or pull instructions are needed in these cases. • Example: When an interrupt condition occurs, the HCS12 pushes the contents of all registers onto the stack, then services the interrupt, then restores the registers by pulling their contents back off the stack.

  8. Initializing the Stack Pointer • Since the HCS12 automatically uses the stack under certain conditions, it’s a good idea to initialize the stack pointer (with an LDS instruction) at the beginning of every program, even if you don’t plan to use any push or pull instructions. • Otherwise, the stack could be located anywhere in memory, possible in an area of memory that overlaps with your program’s code or data.

  9. Avoiding Conflicts with the Stack • Be sure to locate the top of the stack far away from the location of your code and data. Otherwise your code and data may be wiped out by stack operations. • If we always start our programs at $2000 and locate the top of the stack at $4000, that leaves plenty of room between our code and the stack. • Question: How many bytes (and kilobytes) are there between addresses $2000 and $4000?

  10. Subroutines • A subroutine is a small program that does some well-defined task within a bigger program. • Why use subroutines? • If you have a block of code that must be executed at different places within your program, make it a subroutine. • Long, complicated programs are easier to understand and maintain if you break the program up into separate chunks (subroutines).

  11. Calling a Subroutine • The first instruction in a subroutine has a label, which serves as that subroutine’s name. • You can call a subroutine by name using either of these instructions: • Branch to Subroutine (BSR), which uses relative addressing mode. • Jump to Subroutine (JSR), which uses direct, extended, or indexed addressing mode.

  12. Returning from a Subroutine • A subroutine must end with the RTS (Return from Subroutine) instruction, which causes execution of the calling program to resume. • BSR is similar to BRA, except that it also causes the processor to “remember” where to resume when it hits a RTS. Main Program Subroutine BSR Subby ADDA $1000 Subby: LDAB #05 RTS ... ... ...

  13. Subroutines and the Stack • How does the HCS12 remember where in the main program to resume when it hits a RTS? • When it executes the BSR instruction, it pushes the Program Counter (which contains the address of the next instruction) onto the stack. • When it hits the RTS, it pulls this address back from the stack into the Program Counter. This lets it pick up where it left off in the main program.

  14. No Need For You To Push or Pull • Recall that the HCS12 has several push and pull instructions (such as PSHA and PULA) that let you place values on the stack or remove them from the stack. • You don’t need to use these instructions when you branch to a subroutine. The HCS12 pushes and pulls the desired values automatically.

  15. Jump and Subroutine Instructions (Table from p. 79 of the HCS12 CPU Reference Manual.) • CALL and RTC are advanced instructions that we won’t use. CALL is similar to BSR, and RTC is similar to RTS.

  16. Overall Program Structure • Many programs have a general structure that contains three major parts: • Initialization code that executes once when the program starts. • The program’s main body, which typically is an endless loop that starts with a label and ends with a BRA to that label. The main body will often contain one or more BSR instructions that call subroutines. • Subroutines, each of which begins with a label and ends with an RTS.

  17. Overall Program Structure: Example ABSENTRY Entry ORG $2000 Entry: CLI LDS $4000 MainLoop: BSR Sub1 BSR Sub2 BRA MainLoop Sub1: INCA RTS Sub2: INCB RTS END • Initialization • Main body (endless loop) • Subroutines

  18. Troubleshooting Programs with Subroutines Single Step (F11). Steps one instruction at a time. Step Over (F10). For most instructions it’s the same as Single Step, but for BSR it executes the entire subroutine as one step.

More Related