EET 2261 Unit 6 The Stack; Subroutines

Slides:



Advertisements
Similar presentations
Floyd, Digital Fundamentals, 10 th ed EET 2261 Unit 7 Indexed Addressing Mode Read Mazidi, Chapter 6. Homework #7 and Lab #7 due next week. Quiz next week.
Advertisements

Week 8 Stack and Subroutines. Stack  The stack is a section of data memory (or RAM) that is reserved for storage of temporary data  The data may represent.
EET 2261 Unit 3 Assembly Language; Load, Store, & Move Instructions  Read Almy, Chapters 5 and 6.  Homework #3 and Lab #3 due next week.  Quiz next.
Interrupts Chapter 8 – pp Chapter 10 – pp Appendix A – pp 537 &
Computer Organization and Architecture
EET 2261 Unit 5 Tables; Decision Trees & Logic Instructions
Lecture 6 Machine Code: How the CPU is programmed.
Set 20 Interrupts. INTERRUPTS The Pentium has a mechanism whereby external devices can interrupt it. Devices such as the keyboard, the monitor, hard disks.
68HC11 Polling and Interrupts
EET 2261 Unit 9 Interrupts  Read Almy, Chapters 17 – 19.  Homework #9 and Lab #9 due next week.  Quiz next week.
TK 2633 Microprocessor & Interfacing
LOGO Chapter 1 Interrupt handling. hardware interrupt Under x86, hardware interrupts are called IRQ's. When the CPU receives an interrupt, it stops whatever.
CSS 372 Lecture 1 Course Overview: CSS 372 Web page Syllabus Lab Ettiquette Lab Report Format Review of CSS 371: Simple Computer Architecture Traps Interrupts.
EECC250 - Shaaban #1 Lec # 5 Winter Stacks A stack is a First In Last Out (FILO) buffer containing a number of data items usually implemented.
Room: E-3-31 Phone: Dr Masri Ayob TK 2633 Microprocessor & Interfacing Lecture 1: Introduction to 8085 Assembly Language.
Chapter 12 CPU Structure and Function. Example Register Organizations.
EET 2261 Unit 2 HCS12 Architecture
Embedded Systems 7763B Mt Druitt College of TAFE
© 2010 Kettering University, All rights reserved..
Chapter 10 The Stack Stack: An Abstract Data Type An important abstraction that you will encounter in many applications. We will describe two uses:
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
ECE 265 – LECTURE 8 The M68HC11 Basic Instruction Set The remaining instructions 10/20/ ECE265.
Physics 413 Chapter 4 A Fork in the Road LDAB # $ 13 here :ADDA # $ 24 DEC B BNE here WAI BNE is the new instruction. Branch if not equal to zero.
Computer Architecture Lecture 13 – part 1 by Engineer A. Lecturer Aymen Hasan AlAwady 31/3/2014 University of Kufa - Information Technology Research and.
Today’s Lecture Unconditional branch instruction
George W. Woodruff School of Mechanical Engineering, Georgia Tech ME4447/6405 ME 4447/6405 Microprocessor Control of Manufacturing Systems and Introduction.
ECE 265 – LECTURE 5 The M68HC11 Basic Instruction Set 12/8/ ECE265.
1 Stacks, Subroutines, I/O Routines Today: First Hour: Stacks, Subroutines –Section 3.9,3.10 of Huang’s Textbook –In-class Activity #1 Second Hour: I/O.
1 ECE 372 – Microcontroller Design Assembly Programming HCS12 Assembly Programming Addressing Modes Stack Operations Subroutines.
ECE Lecture 21 Typical Assembly Language Program Bugs.
Advanced Assembly Language Programming
© 2010 Kettering University, All rights reserved..
MIPS Subroutines Subroutine Call – jal subname Saves RA in $31 and jumps to subroutine entry label subname Subroutine Return – jr $31 Loads PC with return.
Subroutines and Stacks. Stack The stack is a special area in memory used by the CPU to store register information or general data information during program.
LECTURE 12 Virtual Memory. VIRTUAL MEMORY Just as a cache can provide fast, easy access to recently-used code and data, main memory acts as a “cache”
George W. Woodruff School of Mechanical Engineering, Georgia Tech ME4447/6405 ME 4447/6405 Microprocessor Control of Manufacturing Systems and Introduction.
1 Subroutines Advanced Programming. 2 Top-Down approach to problem solving Algorithm step by step description of how to solve a problem Divide and Conquer.
1 Stack Advanced Programming. 2 The Stack It is a special area of memory used as temporary storage A stack is a LIFO data structure Putting data into.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
Revised: Aug 1, EE4390 Microprocessors Lessons 11, 12 Advanced Assembly Programming.
CPEN 231: Microcomputer Architecture and Assembly Programming Lecture 10: Stack and Subroutines John Tadrous, Ph.D. ECE Rice University
EET 2261 Unit 6 The Stack; Subroutines
ECE 3430 – Intro to Microcomputer Systems
Figure 8.1 of course package
ECE 3430 – Intro to Microcomputer Systems
ECE 3430 – Intro to Microcomputer Systems
The Stack.
ECE 3430 – Intro to Microcomputer Systems
ECE 3430 – Intro to Microcomputer Systems
CS 3305 System Calls Lecture 7.
Microprocessor and Assembly Language
Microcomputer Programming
Chapter 3 Addressing Modes
Chapter 10 The Stack.
Arithmetic using a stack
Subroutines … a 1st look procedures and functions in high level languages are modeled on subroutines typically, assembly code is very modular with.
ME4447/6405 Microprocessor Control of Manufacturing Systems and
Practice Six Chapter Eight.
Subroutines and the Stack
Chapter 8 Central Processing Unit
EE6502/MPMC/UNIT II/STACK AND SUBROUTINE/T.THARANKUMAR
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
8051 ASSEMBLY LANGUAGE PROGRAMMING
EET 2261 Unit 6 The Stack; Subroutines
Some Assembly (Part 2) set.html.
Where is all the knowledge we lost with information? T. S. Eliot
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
ECE511: Digital System & Microprocessor
Some Assembly
Presentation transcript:

EET 2261 Unit 6 The Stack; Subroutines Read Almy, Chapter 11. Homework #6 and Lab #6 due next week. Quiz next week. -Handouts: Stack practice sheet. -Take Quiz #6.

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.

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. Ask them what instruction(s) you could use to load a value into the SP.

Review: HCS12 Memory Map See page 26 of Device User Guide.

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

Push Instructions and Pull Instructions Do stack practice sheet. (Solution on next slide.) (Table from p. 82 of the HCS12 CPU Reference Manual.)

Using the Stack: Example Predict contents after each instruction executes: Instruction A B SP Mem $3FFD Mem. $3FFE Mem. $3FFF LDS #$4000 $4000 LDAA #$12 $12 PSHA $3FFF DECA $11 LDAB #$34 $34 PSHD $3FFD LDD #$00 PULD PULA After examining it, enter it & single-step it. Note that in the simulator, unused stack entries revert to uu after a PULL. (In hardware, using the Dragon12 board, the contents of these entries are unpredictable.)

Other Uses of the Stack As seen above, the programmer can use the stack by issuing push or pull instructions. Also, under certain conditions the HCS12 automatically pushes data onto the stack or pulls data off the stack. No push or pull instructions are needed in these cases. Examples: Subroutines (which we’ll study in a few slides) and interrupts (which we’ll study in a few weeks) are the two most common conditions of this sort.

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, possibly in an area of memory that overlaps with your program’s code or data.

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? Demo a stack conflict by changing LDS #$4000 in previous program to LDS #$2010, and single-step it again, watching what happens to our code in the Memory window and Assembly window; the PULA and PULD instructions will be wiped out and replaced with something else.

Subroutines A subroutine is a small program that performs 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).

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.

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

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.

No Need For You To Push or Pull Recall from earlier 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.

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.

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 a “forever” 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.

Overall Program Structure: Example ABSENTRY Entry ORG $2000 Entry: CLI LDS #$4000 MainLoop: BSR Sub1 BSR Sub2 BRA MainLoop Sub1: INCA RTS Sub2: INCB END Initialization Main body (“forever” loop) This is a silly example for illustration purposes only—you wouldn’t use subroutines for such simple code. Enter it and single-step it. Subroutines

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. Demo Step Over using previous program.