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.

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

Calling sequence ESP.
Subroutines – parameter passing passing data to/from a subroutine can be done through the parameters and through the return value of a function subroutine.
Slides revised 3/25/2014 by Patrick Kelley. 2 Procedures Unlike other branching structures (loops, etc.) a Procedure has to return to where it was called.
Functions Functions and Parameters. History A function call needs to save the registers in use The called function will use the registers The registers.
Assembler Programming Chapter 6. EEL-4746 Best Practices.
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.
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
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.
Run-Time Storage Organization
Subroutines and Stacks Lecture L3.1. Subroutine and Stacks The System Stack Subroutines A Data Stack.
Programming the HC12 in C. Some Key Differences – Note that in C, the starting location of the program is defined when you compile the program, not in.
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
28/06/2015CMPUT Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.
EECC250 - Shaaban #1 lec #7 Winter Local workspace of a subroutine: A number of temporary memory locations required by the subroutine for temporary.
Design of Embedded Systems Using 68HC12(11) Microcontrollers - R. E. Haskell Subroutine and Stacks Chapter 2.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
© 2010 Kettering University, All rights reserved..
George W. Woodruff School of Mechanical Engineering, Georgia Tech ME4447/6405 ME 4447/6405 Microprocessor Control of Manufacturing Systems and Introduction.
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.
Objectives Implement pointers using indexed addressing modes Use pointers to access arrays, strings, structures, tables, and matrices Present finite-state.
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
Compiler Construction
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..
9-1 EE 319K Introduction to Microcontrollers Lecture 9:I/O Synchronization; LCD Interfacing; Implementing Local Variables using a Stack Frame; Parameter.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
Passing Parameters using Stack Calling program pushes parameters on the stack one element at a time before calling subroutine. Subroutine Call (jsr, bsr)
ELE22MIC Lecture 8 ASll Examples –16 Bit Counters –Buffalo Jump Table Interrupt processing (IRQ/RTI) Stack Frame & Base Pointer Wired OR.
1 Interrupts, Resets Today: First Hour: Interrupts –Section 5.2 of Huang’s Textbook –In-class Activity #1 Second Hour: More Interrupts Section 5.2 of Huang’s.
ECE 265 – LECTURE 5 The M68HC11 Basic Instruction Set 12/8/ ECE265.
1 ECE 372 – Microcontroller Design Assembly Programming HCS12 Assembly Programming Addressing Modes Stack Operations Subroutines.
8-1 EE 319K Introduction to Microcontrollers Lecture 8:Fixed Point Numbers, Local Variables, Binding, Allocation, Access, Deallocation.
ECE Lecture 21 Typical Assembly Language Program Bugs.
CSC 8505 Compiler Construction Runtime Environments.
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.
Physics 413 Chapter 4: Advanced Assembly Programming.
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.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Revised: Aug 1, EE4390 Microprocessors Lessons 11, 12 Advanced Assembly Programming.
ELE22MIC Lecture 17 Writing 68HC11 software 68HC11 Main Timer System –Output Compare –What is different about TOC1?
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
Computer Science 210 Computer Organization
ECE 3430 – Intro to Microcomputer Systems
Procedures 101: There and Back Again
ECE 3430 – Intro to Microcomputer Systems
Functions and Procedures
Chapter 9 :: Subroutines and Control Abstraction
Stack Frame Linkage.
MIPS Instructions.
Subroutines – parameter passing
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
Subroutines … passing data
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
ECE 447: Lecture 15 Stack Operations.
Runtime Environments What is in the memory?.
EET 2261 Unit 6 The Stack; Subroutines
Where is all the knowledge we lost with information? T. S. Eliot
Lecture 3 - Instruction Set - Al
Presentation transcript:

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 Routines Section 3.11 of Huang’s Textbook –In-class Activity #2

2 Main program Subroutine 1Subroutine 2Subroutine 3 Subroutine 1.1Subroutine 2.1Subroutine 3.1 Subroutine 2.1.1Subroutine Program Structure Main program Subroutine sub_x sub_x: Subroutine Processing Subroutines:Recap Subroutine call Subroutine call: JSR, BSR Subroutine return Subroutine return: RTS

3 Simple Subroutine * Goal: to increment the number in the accumulator A using a subroutine. ORG $C000 START LDS #INITSP LDAA #$32 ;the number to increment JSR INCREMENT_A ;call subroutine SWI ; software interrupt ************************************************************************** * subroutine INCREMENT_A INCREMENT_A INCA RTS Note: the parameter to the subroutine is passed, and the result returned through the register A itself. No local variables are used.

4 1.Parameter passing options: -use registers -use the stack -use global memory 2.Returning results -use registers -use the stack (caller creates a hole in the stack; callee places the result in this hole) -use global memory 3. Protect registers: - Any registers that may be “corrupted” by the subroutine must be pushed on the stack first 4. Local variables allocation - allocated by the callee - need to be deallocated before return Subroutine Call Issues

5 Local variables SP Saved registers Return address Incoming parameters Y (or X) Example of 68HC11 stack frame Stack Frame Stack used a lot for passing parameters, returning results and storing registers! The stack is therefore organized as a bunch of stack frames, one per subroutine

6 Swap Subroutine * Goal: swap accumulators A and B through a subroutine ORG $C000 START LDS #INITSP JSR SWAP ;call subroutine... * subroutine SWAP SWAP PSHX; save register IX DES; create space for a local var on stack TSX; copy SP to IX STAA 1,IX; Save A in the local var LDAA #$0; Reset A to 0 ABA; A+B  A, I.e. move B to A LDAB 1,IX; Load local var to B INS; Deallocate local var PULX; restore register IX RTS; Return

7 Stack frame for Swap Example Stack Frame 1 byte for local var SP ret_addr_H ret_addr_L upper byte of return address lower byte of return address X_H X_L Upper byte of IX lower byte of IX

8 Modified Swap Subroutine * Goal: swap 1-byte passed as params, leave result in A, B ORG $C000 START LDS #INITSP PSHB; parameter B PSHA; parameter A JSR NEW_SWAP;call subroutine... * subroutine NEW_SWAP NEW_SWAP PSHX; save register IX DES; create space for a local var on stack TSX; copy SP to IX LDAA 6,IX; load A LDAB 7,IX; load B STAA 1,IX; Save A in the local var LDAA #$0; Reset A to 0 ABA; A+B  A, I.e. move B to A LDAB 1,IX; Load local var to B INS; Deallocate local var PULX; restore register IX RTS; Return

9 Stack frame for New_Swap Example Stack Frame 1 byte for local var SP ret_addr_H ret_addr_L upper byte of return address lower byte of return address X_H X_L Upper byte of IX lower byte of IX A B Stack positions used to only pass values of A, B as parameters.

10 Allocation: If less than 5 bytes needed for local variables, use DES instructions If more than 5 bytes needed for local variables, use: TSX XGDX SUBD #N; allocate N bytes XGDX TXS; move the stack pointer up by N bytes De-allocation: If less than 5 bytes to be deallocated, use INS instructions If more than 5 bytes to be deallocated, use: TSX XGDX ADDD#N; deallocate N bytes XGDX TXS; move down the stack pointer by N bytes Local Variables

11 Do Activity #1 Now

12 Complex Swap Subroutine * Goal: Swap 1-byte passed as params, Return result via stack thru same locations ORG $C000 START LDS #INITSP PSHB; parameter B PSHA; parameter A JSR COMPLEX_SWAP;call subroutine PULA PULB...

13 Complex Swap Subroutine * subroutine COMPLEX_SWAP COMPLEX_SWAP PSHX; save register IX DES; create space for a local var on stack TSX; copy SP to IX LDAA 6,IX; load A LDAB 7,IX; load B STAA 1,IX; Save A in the local var LDAA #$0; Reset A to 0 ABA; A+B  A, I.e. move B to A LDAB 1,IX; Load local var to B STAA 6,IX; Return swapped value of A thru stack STAB 7,IX; Return swapped value of B thru stack INS; Deallocate local var PULX; restore register IX RTS; Return

14 Stack frame for New_Swap Example Stack Frame 1 byte for local var SP ret_addr_H ret_addr_L upper byte of return address lower byte of return address X_H X_L Upper byte of IX lower byte of IX A B Stack positions used to both pass and return A, B

15 ldaa#N psha ldx#mat pshx bsrsubx... Subx: (1)pshb (2)psha (3)pshx (4)pshy (5)tsx (6)xgdx (7)subd#6 (8)xgdx (9)txs... 6 bytes for local variables SP Y_H Y_L X_H X_L A B ret_addr_H ret_addr_L upper byte of return address lower byte of return address MAT_H MAT_L N upper byte of Y lower byte of Y upper byte of X lower byte of X upper byte of MAT lower byte of MAT Complex Stack Frame

16 Buffalo Utilities Problem The actual location in ROM of the Buffalo utility input-output subroutines may be changed with revisions and updates of the Buffalo monitor program Input - Output Subroutines are in Buffalo Monitor ROM ROM rev. n ROM rev. n I-O Utility ROM rev. n+m ROM rev. n+m I-O Utility The Jump Table is on next slide Jump Table Jump Table fixed location Use a Jump Table that has a fixed location in ROM Solution Jumps to Subr code I/O Subr Entry Pt

17 Jump Table for I/O Subroutines ROMROM Memory AddrContentsComments ffa0JMP UPCASElower  UPPER ffa3JMPWCHEKwhitespace ffa6JMPDCHEKdelimiter ffacJMPINPUTRead Input ffafJMP OUTPUTWrite Output ffb2JMP OUTLHLF ffb5JMP OUTRHLF ffb8JMPOUTA ffbbJMP OUT1BYT ffbeJMP OUT1BSP ffc1JMP OUT2BSP ffc4JMP OUTCRLF ffc7JMP OUTSTRG ffcaJMP OUTSTRG0 ffcdJMP INCHAR More complete descriptions are given in the handout.

18 Example Use of Utilities These program fragments illustrate how to do I/O *EQUATES INPUTEQU$FFAC INCHAREQU$FFCD OUTPUTEQU$FFAF OUTSTRGEQU$FFC7 ORG$6000 JMPSTART *DATA SECTION TEXTFCC"Text" FCB$04 PROMPTFCC'> ASCIIRMB1 BUFFERRMB80

19 Example Use of Utilities *MAIN START STARTLDX #TEXT JSROUTSTRG... LDAAPROMPT JSROUTPUT... JSRINPUT STAAASCII... LDY#BUFFER LOOPJSRINCHAR STAA0,Y...

20 Do Activity #2 Now Due: End of Class Today. RETAIN THE LAST PAGE(S) (#3 onwards)!! For Next Class: Bring Huang Textbook, & HC11 PRG Required Reading: – Sec of Huang This reading is necessary for getting points in the Studio Activity!