Stacks and Subroutines. Some example stacks Stacks and subroutine usage The stack is a special area of the random access memory in the overall memory.

Slides:



Advertisements
Similar presentations
Ch. 7 Local Variables and Parameter Passing From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.
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 6 The Stack; Subroutines
Chapter 10 And, Finally.... Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display A Final Collection of ISA-related.
CPU Review and Programming Models CT101 – Computing Systems.
1/1/ / faculty of Electrical Engineering eindhoven university of technology Introduction Part 2: Data types and addressing modes dr.ir. A.C. Verschueren.
INSTRUCTION SET ARCHITECTURES
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
EECC250 - Shaaban #1 Lec # 6 Winter Stack-Related Instructions PEA Push Effective Address Calculates an effective address and pushes it.
ECE 353 Introduction to Microprocessor Systems Michael G. Morrow, P.E. Week 6.
Procedures and Stacks. Outline Stack organization PUSH and POP instructions Defining and Calling procedures.
Assembler Programming Chapter 6. EEL-4746 Best Practices.
Software engineering, program management. The problem  Software is expensive to design! – Industry estimates put software development labor costs at.
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.
EECC250 - Shaaban #1 lec #7 Winter Local workspace of a subroutine: A number of temporary memory locations required by the subroutine for temporary.
Stacks and Subroutines ELEC 330 Digital Systems Engineering Dr. Ron Hayne Images Courtesy of Ramesh Gaonkar and Delmar Learning.
© 2010 Kettering University, All rights reserved..
Chapter 10 And, Finally... The Stack. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Stacks A LIFO.
Chapter 10 The Stack Stack: An Abstract Data Type An important abstraction that you will encounter in many applications. We will describe two uses:
CS-2710 Dr. Mark L. Hornick 1 Defining and calling procedures (subroutines) in assembly And using the Stack.
Machine Instruction Characteristics
ECE 265 – LECTURE 8 The M68HC11 Basic Instruction Set The remaining instructions 10/20/ ECE265.
Interrupts and reset operations. Overview  Introduction to interrupts – What are they – How are they used  68HC11 interrupt mechanisms – Types of interrupts.
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.
© 2010 Kettering University, All rights reserved..
Objective At the conclusion of this chapter you will be able to:
9-1 EE 319K Introduction to Microcontrollers Lecture 9:I/O Synchronization; LCD Interfacing; Implementing Local Variables using a Stack Frame; Parameter.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
Passing Parameters using Stack Calling program pushes parameters on the stack one element at a time before calling subroutine. Subroutine Call (jsr, bsr)
Runtime Stack Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens MIPS Memory Organization In addition to memory for static.
ELE22MIC Lecture 8 ASll Examples –16 Bit Counters –Buffalo Jump Table Interrupt processing (IRQ/RTI) Stack Frame & Base Pointer Wired OR.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
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.
Ch.2 Intro. To Assembly Language Programming
ECE Lecture 21 Typical Assembly Language Program Bugs.
Ch.2 Intro. To Assembly Language Programming From Introduction to Embedded Systems: Interfacing to the Freescale 9s12 by Valvano, published by CENGAGE.
Advanced Assembly Language Programming
© 2010 Kettering University, All rights reserved..
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.
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.
Revised: Aug 1, EE4390 Microprocessors Lessons 11, 12 Advanced Assembly Programming.
Stacks and Subroutines May 23. Stacks and subroutine usage The stack is a special area of the random access memory in the overall memory system The stack.
5-1 EE 319K Introduction to Microcontrollers Lecture 5: Conditionals, Loops, Modular Programming, Sub- routines, Parameter passing.
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
The Stack.
ECE 3430 – Intro to Microcomputer Systems
Chapter 10 The Stack.
Branching and Looping Lecture L3.2.
MIPS Instructions.
Chapter 8 Central Processing Unit
The University of Adelaide, School of Computer Science
Procedures and Calling Conventions
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
8051 ASSEMBLY LANGUAGE PROGRAMMING
ECE 447: Lecture 15 Stack Operations.
Program and memory layout
EET 2261 Unit 6 The Stack; Subroutines
Program and memory layout
Where is all the knowledge we lost with information? T. S. Eliot
Computer Organization and Assembly Language
ECE511: Digital System & Microprocessor
Lecture 3 - Instruction Set - Al
Presentation transcript:

Stacks and Subroutines

Some example stacks

Stacks and subroutine usage The stack is a special area of the random access memory in the overall memory system The stack is used for oGeneral data storage (limited) oTemporary data storage (true) oParameter and control information storage for subroutines (yes!)

Stack Last-in, first-out (LIFO) structure All access is at the top of the stack Operations oPush  Places an item onto the stack oPull  Removes an item from the stack  Some (most!) people use the term “pop” Bottom of the stack is at highest memory address, and top of the stack is at the lowest address oStack grows from high to low address Cont..

Stack oFor EVBU, stack is usually placed at $01FF Stack Pointer (SP) oRegister that points to the memory location immediately preceding the top of the stack oIn other words, the SP normally contains the address for a new data item to be pushed Cont..

HC11 Implementation

PUSH Instructions Push instructions oPSHA, PSHB, PSHX, PSHY  Stores data in memory at address pointed to by SP  Decrements SP  For 16-bit values, low- order byte is pushed first, followed by high- order byte oHow can we push CCR?

PUSH Instructions Pull instructions oPULA, PULB, PULX, PULY  Increments SP  Loads data from memory address pointed to by SP

Other Stack Operations Other stack operations oCan modify or read the SP LDSLoad the SP STSStore the SP INSIncrement SP DESDecrement SP TSXTransfer SP+1 to IX TSYTransfer SP+1 to IY TXSTransfer IX-1 to SP TYSTransfer IY-1 to SP

Stacks Stacks oRemember to initialize SP at beginning of oprogram oImportant to pull data in reverse order that you pushed it oNote that SP points to empty memory location (= location where next value will be stored)

Using the Stacks Using the stack oPassing parameters to subroutines (more on this later) oTemporary storage  68HC11 has limited number of registers  Use stack to preserve register values Example: Multiply ACCA and ACCB, round to 8 bits PSHB ; save value in ACCB MUL ; ACCD = ACCA*ACCB ADCA #$00 ; round to 8 bits PULB ; restore ACCB Cont..

Using the Stacks Using the stack oYou can also use the stack to save intermediate ovalues oExample: (Listing 3.1 in text) This calculates x squared plus y squared. x and y are 8-bit numbers stored in addresses $1031 and $1032. The 8-bit result is put in ACCA. ORG $E000 BEGIN: LDS #$FF ; initialize SP LDAA $1031 ; get x value TAB ; square it Cont..

Using the Stacks MUL ADCA #$00 ; round to 8 bits PSHA ; save it LDAA $1032 ; get y value TAB ; square it MUL ADCA #$00 ; round to 8 bits PULB ; retrieve first result ABA ; add them

Dangers Using the Stacks Dangers in using the stack oOverflow  Too many pushes Stack grows so large that it overwrites portions of memory that are being used for other purposes  Example: ORG $E0 FOO RMB 2 ORG $E000 LDS #$FF ; initialize SP LDAB #$FF ; initialize loop count LOOP: Cont..

Dangers Using the Stacks PSHA ; push value on stack DECB ; decrement count BNE LOOP ; and repeat oUnderflow  Too many pulls  SP points to address higher than the intended bottom of the stack oThe HC11 does not protect you from overflow or underflow!  Only way to recover is to reset the system  Be aware of the stack size that your program needs. Cont..

Dangers Using the Stacks oTypical memory layout for EVBU:  512 bytes of RAM ($00-$1FF)  $00 - $FF used by Buffalo monitor Don’t use these locations in your programs  User variables should start at $100 ($100-?)  Stack starts at $1FF (? - $1FF)  More variables = less stack space

Subroutine Why use them? oProvides modularity to reduce program complexity oCreates many smaller, but more easily developed and tested program units oMay save on memory space (to store program) oCan be reused in different parts of a program oCan be placed in a library for use by many programs and applications Subroutines should... oBe small, to ease writing, testing, and modifying oHave a well defined purpose oHave a well defined interface Cont..

Subroutine oBe well documented (like the rest of the program!)  Describe input parameters Registers, memory locations, values on the stack, etc.  Describe output parameters Registers, memory locations, stack, condition codes  Describe any side effects Registers that are changed, condition codes, etc.

Example of proper Documentation Format Example of proper documentation format: ;****************************** ; Function: ToUpper ; This function replaces all lower-case ; characters in a string with upper-case. ; The end of the string is marked with ; $00. ; Input: IX = starting address of string ; Output: none ; Registers affected: ACCA, CCR ;*******************************

Calling Subroutines Calling a subroutine JSR sbr BSR sbr Address of next instruction (return address) is pushed onto the stack  This is the current value of PC oPC is loaded with the starting address of the subroutine  Next instruction fetched will be from the subroutine

Returning Subroutine Returning from a subroutine oRTS instruction oMust be the last instruction in subroutine oPops return address off the stack, loads it into PC  Next instruction fetch will be the instruction following the JSR/BSR oIf your subroutine manipulates the stack, be aware of where the return address is stored.  It must be at the top of the stack when you execute the RTS

Subroutine Parameter Passing Subroutine parameter passing -- how are the parameters / arguments for the subroutine passed to it? oRegisters  Parameters are placed in predetermined register locations  Simple and fast  Number of parameters is limited by the number of "free" registers  Code is not reentrant oDedicated memory  A series of (predetermined) memory locations are used to hold the parameters Cont..

Subroutine Parameter Passing  Simple, but added overhead due to memory operations  Possible to pass many parameters  Code is not reentrant oPointer to memory  Pass the subroutine a register value containing the address of the parameters in memory  More complex, extra overhead for use of the pointer  Code is reentrant oUse the stack  Parameters, in addition to the return address, are pushed onto the stack -- the combination is called the stack frame You must push the parameters before the JSR/BSR Cont..

Subroutine Parameter Passing  Requires care in manipulating the parameters Don’t forget about the return address!  Many parameters can be passed  Separate parameter stack can be created and used in addition to the system stack  Code is reentrant

Example Example: oTiming delay  Write a subroutine that will delay for a specified number of milliseconds. The delay time is passed as a parameter in ACCB. The subroutine should also preserve the contents of all registers as well as the condition codes oMain program: LDAB #$0A ; delay for 10 ms JSR DELAY

Example Example: oWrite a subroutine to find the maximum of two signed 8-bit numbers. The two numbers are passed to the subroutine on the stack, and the maximum is returned to the calling routine on the stack. oMain program: LDAA #$73 ; load the 2 values LDAB #$A8 PSHA ; push them on the stack PSHB JSR MAX ; find the max PULA ; pull max off stack

Example Example: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Subroutine - MAX ; This subroutine returns the maximum of 2 ; signed 8-bit numbers. ; Input - The 2 numbers are passed on the stack ; Output - The maximum is returned on the stack ; Registers used - ACCA, ACCB, IY, CCR ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; MAX: PULY ; save return address PULB ; get the 2 numbers Cont..

Example PULA CBA ; compare them BGT A_IS_MAX ; branch if ACCA > ACCB B_IS_MAX: PSHB ; return the value in ACCB BRA MAX_EXIT A_IS_MAX: PSHA ; return the value in ACCA MAX_EXIT: PSHY ; put return address back on stack RTS ; and return

Example Example: oConvert an 8-bit hex number into 2 ASCII characters  For example, convert $3F into ‘3’ and ‘F’ oThe hex number is passed to the subroutine on the stack, and the 2 ASCII characters are returned on the stack, with the high-order character on top. oMain program: LDAA #$3F ; get hex number PSHA JSR HEX_TO_ASCII ; convert it PULB ; ACCB = ‘3’ = $33 PULA ; ACCA = ‘F’ = $46

Example Example oConverting hex digit to 2 ASCII characters  Pseudo code: Get hex digit from stack Convert lower nibble to ASCII Push char on stack Convert upper nibble to ASCII Push char on stack Return oWe’ll use a second subroutine to convert a nibble to ASCII  Note that the ASCII codes for 0-9 are $30-$39, and the codes for A-F are $41-$46 Cont..

Example  Pseudo code: Add #$30 to nibble If (result > $39) Add #$07 Return

Example Example: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Subroutine - CONVERT_NIBBLE ; This subroutine converts a nibble (4-bit value) ; to ASCII. ; Input: ACCB contains the nibble ; Output: ACCB contains the ASCII code ; Registers affected: None ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; CONVERT_NIBBLE: PSHA ; save ACCA Cont..

Example TPA ; save CCR PSHA ADDB #$30 ; convert to ASCII CMPB #$39 ; is nibble > 9? BLS CN_EXIT ; branch if not ADDB #$07 ; for A-F CN_EXIT: PULA ; restore CCR TAP PULA ; restore ACCA RTS ; return

Example Example: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Subroutine - HEX_TO_ASCII ; This subroutine converts an 8-bit hex number ; to two ASCII digits. ; Input: Hex number is passed on the stack ; Output: ASCII characters are returned on the ; stack, with the high-order nibble on top. ; Registers affected: ACCA, ACCB, IY, CCR ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; HEX_TO_ASCII: PULY ; save return address Cont..

Example PULA ; get hex number TAB ; copy it to ACCB ANDB #$0F ; get lower nibble JSR CONVERT_NIBBLE PSHB ; save ASCII on stack TAB ; get hex number again LSRB ; get upper nibble LSRB JSR CONVERT_NIBBLE Cont..

Example PSHB ; save ASCII on stack PSHY ; push return address RTS ; and return

Summary Summary oStack  Be sure to initialize the stack pointer at the beginning of your program LDS #$1FF Don’t do this if you’re calling your program from Buffalo Buffalo initializes SP for you  Push and Pull operations  Remember SP points to next empty location

Summary Summary oSubroutines  Break your program into subroutines Well-defined (and documented!) function Well-defined (and documented!) interface  Passing parameters Use registers if possible Else, use stack if needed oDon’t forget about the return address