EE 3170 Microcontroller Applications

Similar documents
EE 3170 Microcontroller Applications

Part 7. Stacks. Stack. Stack. Examples of Stacks. Stack Operation: Push. Piles of Data. The Stack

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

Exam I Review February 2017

ECE 3610 MICROPROCESSING SYSTEMS

Outline. 2.8 Stack. 2.9 Subroutines

EE 3170 Microcontroller Applications

Lecture 9 Subroutines

EE 3170 Microcontroller Applications

ME 4447/6405. Microprocessor Control of Manufacturing Systems and Introduction to Mechatronics. Instructor: Professor Charles Ume LECTURE 6

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Programming the Motorola MC68HC11 Microcontroller

CSE Lecture In Class Example Handout

Stack Frames. Compilers use the stack: to store the to to a subroutine for storage of declared in the subroutine and a place to

Chapter 14 Functions. Function. Example of High-Level Structure. Functions in C

2. Arithmetic Instructions addition, subtraction, multiplication, divison (HCS12 Core Users Guide, Sections 4.3.4, and ).

Instruction Set Architectures (4)

Using the stack and the stack pointer

Lecture #4 Microcontroller Instruction Set Embedded System Engineering Philip Koopman Monday, 25-Jan-2016

Exam 2 E2-1 Fall Name: Exam 2

Assembly Language: Function Calls" Goals of this Lecture"

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

EE 5340/7340 Motorola 68HC11 Microcontroler Lecture 1. Carlos E. Davila, Electrical Engineering Dept. Southern Methodist University

Assembly Language: Function Calls" Goals of this Lecture"

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

We can emit stack-machine-style code for expressions via recursion

Arguments and Return Values. EE 109 Unit 16 Stack Frames. Assembly & HLL s. Arguments and Return Values

Anne Bracy CS 3410 Computer Science Cornell University

Assembly Language: Function Calls

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

Anne Bracy CS 3410 Computer Science Cornell University

ECE 331: PC Lab 3 Stack and Subroutines

COSC345 Software Engineering. Basic Computer Architecture and The Stack

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

EE 3170 Microcontroller Applications

Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University. See P&H 2.8 and 2.12, and A.

Xuan Guo. Lecture XIX: Subroutines (2) CSC 3210 Computer Organization and Programming Georgia State University. March 31, 2015.

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

CA Compiler Construction

Functions in C. Memory Allocation in C. C to LC3 Code generation. Next.. Complete and submit C to LC3 code generation. How to handle function calls?

Subprograms, Subroutines, and Functions

Microcontrollers and the Freescale/Motorola HC11

Course Administration

Today s Menu. >Use the Internal Register(s) >Use the Program Memory Space >Use the Stack >Use global memory

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

ME4447/6405. Microprocessor Control of Manufacturing Systems and Introduction to Mechatronics. Instructor: Professor Charles Ume LECTURE 7

Computer Architecture and System Software Lecture 06: Assembly Language Programming

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: MIPS Programming

2/12/2018. Recall Why ISAs Define Calling Conventions. ECE 220: Computer Systems & Programming. Recall the Structure of the LC-3 Stack Frame

Bonus Lecture: Fibonacci

Chapter 7 Subroutines. Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C

MIPS Assembly (Functions)

AN1064. Motorola Semiconductor Application Note. Use of Stack Simplifies M68HC11 Programming By Gordon Doughman. Introduction

Sample Problem Set #1

CHAPTER 8. Solutions for Exercises

MC9S12 Assembler Directives A Summary of MC9S12 Instructions Disassembly of MC9S12 op codes. Summary of HCS12 addressing modes ADDRESSING MODES

Chapter 14 Functions

Subroutines. we jump to a new location in the code

Compilers and computer architecture: A realistic compiler to MIPS

Special Topics: Programming Languages

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

Disassembly of MC9S12 op codes Decimal, Hexadecimal and Binary Numbers

Disassembly of MC9S12 op codes Decimal, Hexadecimal and Binary Numbers

CSE Lecture In Class Example Handout

The Stack. Lecture 15: The Stack. The Stack. Adding Elements. What is it? What is it used for?

Module 8: Atmega32 Stack & Subroutine. Stack Pointer Subroutine Call function

Computer Systems Lecture 9

COSC 243. Assembly Language Techniques. Lecture 9. COSC 243 (Computer Architecture)

UNIVERSITY OF CALIFORNIA, RIVERSIDE

Stacks and Function Calls

CIT Week13 Lecture

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

Implementing Functions at the Machine Level

CSC 2400: Computing Systems. X86 Assembly: Function Calls

real-time kernel documentation

Subroutine. Chapter 8

EE 109 Unit 15 Subroutines and Stacks

2/6/2018. Let s Act Like Compilers! ECE 220: Computer Systems & Programming. Decompose Finding Absolute Value

Summer 2003 Lecture 14 07/02/03

Programming Languages

Functions in MIPS. Functions in MIPS 1

Module 1-G. Marcos and Structured Programming

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 10. Advanced Procedures

HC11 Instruction Set Architecture

Lab 7: Asynchronous Serial I/O

Wednesday, October 15, 14. Functions

ECE331 Handout 3- ASM Instructions, Address Modes and Directives

Chapter 10 And, Finally... The Stack. ACKNOWLEDGEMENT: This lecture uses slides prepared by Gregory T. Byrd, North Carolina State University

Exam 2 E2-1 Fall Name: Exam 2

Run-time Environment

Lab 2 Use Traps. Lab 2 Input and Output 2 nd Semester. Lab 2 English. Lab 2 Pseudocode

Recursive Subroutine Calls Example

Introduction to Embedded Microcomputer Systems Lecture 10.1

Run-Time Data Structures

Functions. Ray Seyfarth. August 4, Bit Intel Assembly Language c 2011 Ray Seyfarth

CSC 2400: Computer Systems. Using the Stack for Function Calls

Code Generation. Lecture 12

1/30/2018. Conventions Provide Implicit Information. ECE 220: Computer Systems & Programming. Arithmetic with Trees is Unambiguous

ECE220: Computer Systems and Programming Spring 2018 Honors Section due: Saturday 14 April at 11:59:59 p.m. Code Generation for an LC-3 Compiler

Transcription:

EE 3170 Microcontroller Applications Lecture 12: Advanced Assembly Language Programming Part II- Stacks Calling Conventions & Base Pointer Usage & Subroutine Examples - Miller 5.5-5.7 Based on slides for ECE3170 by Profs. Davis, Kieckhafer, Tan, and Cischke Lecture Overview Call & Return Call PC generation Addressing Modes Return PC Storage Stack obedience w/in subroutine Return PC retrieval Subroutine Calling Conventions (1) Register (2) Memory (3) Stack EE3170/CC/Lecture#12-Part II 1 EE3170/CC/Lecture#12 - Part II 2 Ideal? Extensible? Protocol (1) Return Address Let s design our own calling convention Let s add features one at a time Already Defined by 68HC11 Architecture JSR& BSR instr. - Push PC onto stack (1) Pass Return Address (parent child) (2) Save Parent s Original Register Contents (3) Pass Call Parameters (parent child) (4) Pass Return variables (child parent) (5) Store Child s Local Variables RTN instr. - Pops PC from stack Parent: No responsibilities AND Let s make it Recursion-Friendly Allow only register-based & stack-based data passing Child: Must restore SP to original values before RTN EE3170/CC/Lecture#12 - Part II 3 EE3170/CC/Lecture#12 - Part II 4

(2) Saving Parent s Register Values Options: Parent can do it for those values it needs preserved pre-call: push needed regs post-ret: pop the pushed regs Child can do it for those registers it needs to use entry: push any registers it will use exit: pop the pushed regs 68HC11 has few registers it s not too critical We Choose: Let the child do it (3) Two Ways to Pass Call Parameters 1) Call-by-Value = pass a copy of the parameter value to subroutine Simple method - the main puts the data to be passed to the subroutine in the microprocessor registers. Can only pass 6 bytes and must take care to not to incorrectly overwrite data Subroutine cannot change the main programs data value example: ldaa param1 *content of memory w/ address param1 will be loaded to A psha 2) Call-by-Reference = pass the address of the parameter to the subroutine subroutine works on the actual variable (address of the parameter) example: ldy #param1 *address of memory labeled by param1 will be loaded to index register y pshy EE3170/CC/Lecture#12 - Part II 5 EE3170/CC/Lecture#12 - Part II 6 (3) Passing Call Parameters Stack Contents Parent must make values of parameters available Call-by-value Load in a Register Push onto the Stack Call-by-reference Interface Spec. for a given subroutine must specify: number of parameters, locations, the order in which they are stored (pushed) Author of each individual subr. makes these decisions Author of the parent must know what they are Pre-call: parent pushes call Call: JSR pushes Ret Addr Result: Ret Addr is at top are under Ret Addr Complication: Subroutine will need the before the Ret Addr EE3170/CC/Lecture#12 - Part II 7 EE3170/CC/Lecture#12 - Part II 8

Stack Frames Stack is starting to get a little complicated Several items may be pushed & popped for one subr. Pure canonical stack access is getting inconvenient Stack Frame = The set of all data needed on the stack for one routine One frame is created by call & destroyed by return Each routine has its own stack frame (incl. Main) Contents & structure are defined by call convention Frame-based Stack Access Rules Only the top frame on the stack is accessible Within the top stack frame, allowed random access Stack Frames Each routine has one Stack Frames A routine can access only its own frame The frame for the current routine is always on top Routine is allowed random access within its own frame Frame is destroyed when routine ends EE3170/CC/Lecture#12 - Part II 9 EE3170/CC/Lecture#12 - Part II 10 Accessing Parameters Accessing Parameters SP points to 1st word above top of stack Could we access parameters as offsets from SP? Theoretically, yes But, there are two problems with that: 68HC11 ISA does not support offset addressing from SP Most languages allow dynamic data structures, the SP can move unpredictably during child execution Solution: use another reg. as a Base-Pointer (BP) set it to point to a fixed location in the stack frame address parameters as offsets from the BP register Which Register to use as the BP Options = X or Y Both have offset addressing modes We choose: BP = X X instructions are smaller & faster than Y instructions Entry Protocol: push X (since child is going to use it as a BP) copy SP to X (so X points at stack frame) use indexed addressing (on X) to access parameters EE3170/CC/Lecture#12 - Part II 11 EE3170/CC/Lecture#12 - Part II 12

Implementing BP Protocol Protocol Development In entry protocol of subroutine psh? * push other regs * to be used by child Can now access by indexed addressing on X Exit protocol of subroutine must undo the pushes (in reverse order) push stack-passed load reg-passed Post-Return (parent) instr(s) to remove stack-passed psh? * push other regs pul? * pop other regs EE3170/CC/Lecture#12 - Part II 13 EE3170/CC/Lecture#12 - Part II 14 Example: Protocol Example Assume parent passes 2 single-byte parameters in the stack, 1 parameter in accumulator B Assume child routine will need to use: accumulator A accumulator B - but only to read the parameter value in B doesn t need to be pushed ldab param1 ldab param2 ldab param3 jsr child Post-Return (parent) psha? * push other regs pula? * pop other regs EE3170/CC/Lecture#12 - Part II 15 EE3170/CC/Lecture#12 - Part II 16

(4) Passing Return Variables Protocol Development If Passed in Stack parent must leave room in stack frame for child to store them often can over-write the call parameters IF need extra space for more return variables THEN parent can reserve that space with des instr. des = decrement stack pointer equivalent to push nothing Child stores return vars with indexed addressing on X Parent can pop them off stack after return push stack-passed leave extra space for return variables load reg-passed Post-Return (parent) pop return variables instr(s) to remove stack-passed psh? * push other regs pul? * pop other regs EE3170/CC/Lecture#12 - Part II 17 EE3170/CC/Lecture#12 - Part II 18 Protocol Example (5) Subroutine Local Variables ldab param1 ldab param2 des des ldab param3 jsr child Post-Return (parent) Pop return variables psha? * push other regs pula * pop other regs Could declare storage space with the routine e.g. using rmb directives Works, but has disadvantages Only works with non-recursive subroutines recursive call would over-write at the same addresses Only works for static variables need to know how many there are at assembly time Requires direct or extended addressing NOT lead to position-independent code Reserved storage is permanent Can be used for simple routines with few variables EE3170/CC/Lecture#12 - Part II 19 EE3170/CC/Lecture#12 - Part II 20

(5) Subroutine Local Variables Protocol Summary Can also store local variables on the stack can access in stack-order can also access randomly via indexing on X Need to allocate space in stack frame for local vars leave a hole in frame just before pushing X so variables will have positive offsets from X recall indexed addressing always has positive offsets Implementation: There is no convenient instr. to subtract from the SP Miller suggests sequence of des instructions Arbitrary Parameters/Returns Supported by using Stack for Parameters/Returns Support Recursion Store local variables also on Stack Random access to stack-based parameters/variables Use Base Pointer (X) to access stack variables Convention typical of those used in compilers Highly Extensible calling convention Flexibility is prioritized over speed for large software packages Reading Assignment: Miller 5.5-5.7 (examples) EE3170/CC/Lecture#12 - Part II 21 EE3170/CC/Lecture#12 - Part II 22