8-1 EE 319K Introduction to Microcontrollers Lecture 8:Fixed Point Numbers, Local Variables, Binding, Allocation, Access, Deallocation.

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

R4 Dynamically loading processes. Overview R4 is closely related to R3, much of what you have written for R3 applies to R4 In R3, we executed procedures.
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Ch. 8 Functions.
EECC250 - Shaaban #1 Lec # 6 Winter Stack-Related Instructions PEA Push Effective Address Calculates an effective address and pushes it.
10-1 EE 319K Introduction to Microcontrollers Lecture 10: Interrupts, Output Compare Periodic Interrupts Read Book Sections 9.1, 9.2, 9.4, 9.6.1, 9.6.2,
Chapter 7: User-Defined Functions II
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Writing a Good Program 6. Pointers and Arrays
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
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)
Computer Programming 1 Functions. Computer Programming 2 Objectives Take a first look at building functions Study how a function is called Investigate.
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.
Railway Foundation Electronic, Electrical and Processor Engineering.
EECC250 - Shaaban #1 lec #7 Winter Local workspace of a subroutine: A number of temporary memory locations required by the subroutine for temporary.
11-1 Bard, Tiwari, Telang, Janapa Reddi, Gerstlauer, Valvano, Yerraballi EE 319K Introduction to Embedded Systems Lecture 11: Data Acquisition, Numerical.
Railway Foundation Electronic, Electrical and Processor Engineering.
6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion.
Review of C++ Programming Part II Sheng-Fang Huang.
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
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..
Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-7 Memory Management (1) Department of Computer Science and Software.
Machine Instruction Characteristics
Runtime Environments Compiler Construction Chapter 7.
Objectives Implement pointers using indexed addressing modes Use pointers to access arrays, strings, structures, tables, and matrices Present finite-state.
Compiler Construction
ECE 265 – LECTURE 8 The M68HC11 Basic Instruction Set The remaining instructions 10/20/ ECE265.
9-1 EE 319K Introduction to Microcontrollers Lecture 9:I/O Synchronization; LCD Interfacing; Implementing Local Variables using a Stack Frame; Parameter.
6-1 EE 319K Introduction to Embedded Systems Lecture 6: Stack and Local Variables, Fixed-Point Numbers, LCD and Recursion.
6-1 EE 319K Introduction to Microcontrollers Lecture 6: Indexed Addressing Mode and Variants, Functional Debugging, Arrays, Strings.
EE 319K Introduction to Microcontrollers
COMP3190: Principle of Programming Languages
Ch. 6 Pointers and Data Structures From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
ME4447/6405 The George W. Woodruff School of Mechanical Engineering ME4447/6405 Microprocessor Control of Manufacturing Systems and Introduction to Mechatronics.
3-1 EE 319K Introduction to Microcontrollers Lecture 3: Addressing modes, Memory Operations, Subroutines, I/O, Logical/Shift Operations.
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
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
© 2010 Kettering University, All rights reserved..
Embedded Systems Lecture 5 January 25 th, 2016.
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.
Revised: Aug 1, EE4390 Microprocessors Lessons 11, 12 Advanced Assembly Programming.
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
5-1 EE 319K Introduction to Microcontrollers Lecture 5: Conditionals, Loops, Modular Programming, Sub- routines, Parameter passing.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
EE 319K Introduction to Embedded Systems
CSCI 161: Introduction to Programming
ME4447/6405 Microprocessor Control of Manufacturing Systems and
Background Program must be brought into memory and placed within a process for it to be run. Input queue – collection of processes on the disk that are.
Memory Allocation CS 217.
The University of Adelaide, School of Computer Science
Lecture 2 SCOPE – Local and Global variables
C (and C++) Pointers April 4, 2019.
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
ECE 447: Lecture 15 Stack Operations.
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Run-time environments
Lecture 3 - Instruction Set - Al
Presentation transcript:

8-1 EE 319K Introduction to Microcontrollers Lecture 8:Fixed Point Numbers, Local Variables, Binding, Allocation, Access, Deallocation

8-2 Ramesh Yerraballi Fixed Point Numbers Why? (wish to represent non- integer values)  Next lab measures distance from 0 to 3 cm E.g., cm When? (range is known, range is small)  Range is 0 to 3cm Resolution is cm How? (value = I*)  I (Variable Integer) is a 16-bit unsigned integer. It is stored and manipulated in memory.  (Fixed Constant) that represents the resolution. It is not stored but is usually written in comments ; implicit. (What about negative numbers?)

8-3 Ramesh Yerraballi Fixed Point Numbers: Decimal Decimal (Value = I*10 m ) I is a 16-bit unsigned integer  = 10 m decimal fixed-point For example with m=-3 (resolution of or milli) the value range is to What is  represented as, in Decimal Fixed Point?  ( …) = I*10 -3 => I = Integral approximation of( …*10 3 ) I = Integral approximation of( ) I = 3142 Decimal Fixed-point numbers are human-friendly

8-4 Ramesh Yerraballi Fixed Point Numbers: Binary Binary (Value = I*2 m ) I is a 16-bit unsigned integer  = 2 m binary fixed-point For example with m=-8 (resolution of 1/256) What is  represented as, in binary Fixed Point?  ( …)= I*2 -8 => I = Integral approximation of( …*2 8 ) I = Integral approximation of( ) => I = 804 Binary Fixed-point numbers are computer- friendly

8-5 Ramesh Yerraballi Output Output an integer. Assume integer, n, is between 0 and OutChar($30+n/1000) ;thousand’s digit 2.n = n%1000 OutChar($30+n/100) ;hundred’s digit 3.n = n%100 OutChar($30+n/10) ;ten’s digit 4.OutChar ($30+n%10) ;one’s digit Output a fixed-point decimal number. Assume the integer part of the fixed point number, n, is between 0 and 9999, and resolution is OutChar($30+n/1000) ;thousand’s digit 2.n = n%1000 OutChar($2E) ;decimal point OutChar($30+n/100) ;hundred’s digit 3.n = n%100 OutChar($30+n/10) ;ten’s digit 4.OutChar ($30+n%10) ;one’s digit

8-6 Ramesh Yerraballi Local Variables - Terminology Terminology Scope: From where can this information be accessed  local means restricted to current program segment  global means any software can access it Allocation: When is it created, when is it destroyed  dynamic allocation using registers or stack  permanent allocation assigned a block of memory Local Variables  Local Scope  Dynamic Allocation  temporary information  used only by one software module  allocated, used, then de-allocated  not permanent  implement using the stack or registers

8-7 Ramesh Yerraballi Local Variables: Why Stack?  Dynamic allocation/release allows for reuse of memory  Limited scope of access provides for data protection  Only the program that created the local can access it  The code is reentrant.  The code is relocatable  The number of variables is more than registers (answer to, Why not registers?)

8-8 Ramesh Yerraballi Registers are Local Variables LineProgramRegB (Local) RegX (Global) RegY (Local) Main lds #$4000 bsr Timer_Init ldab #$FC stab DDRT ldx #goN FSM ldab OUT,x lslb stab PTT ldy WAIT,x bsr Timer_Wait10ms ldab PTT andb #$03 lslb abx ldx NEXT,x bra FSM $FC Output Input Pt Wait Program 7.1: FSM Controller

8-9 Ramesh Yerraballi In C  Global Variables  Public: global scope, permanent allocation short myGlobalVariable; // accessible by all programs void MyFunction(void){…}  Private: global scope(only to the file), permanent allocation static short myPrivateGlobalVariable; // accessible by this file // only void static MyPrivateFunction(void){…} // callable by other // routines in this file // only  Local variables  Public: local scope, dynamic allocation void MyFunction(void){ short myLocalVariable; }  Private: local scope, permanent allocation void MyFunction(void){ static short count; count++; }

8-10 Ramesh Yerraballi 9S12 Stack The tsx and tsy instructions do not modify the stack pointer. Below: The tsx instruction creates a stack frame pointer

8-11 Ramesh Yerraballi LIFO Stack Rules 1.Program segments should have an equal number of pushes and pulls; 2.Stack accesses (PUSH or PULL) should not be performed outside the allocated area; 3.Stack reads and writes should not be performed within the free area,  PUSH should first decrement SP, then store the data,  PULL should first read the data, then increment SP.

8-12 Ramesh Yerraballi Local Variables on Stack Four Stages  Binding: Address assignment  Allocation: Memory for the variable  Access: Use of the variable  De-Allocation: Free memory held by the variable

8-13 Ramesh Yerraballi Stages Binding is the assignment of the address (not value) to a symbolic name. Examples: sum set 0 ;16-bit local ; variable Allocation is the generation of memory storage for the local variable. Examples: pushx ; allocate sum ; uninitialized value Equivalently: des ;allocate sum des To do the same but initialize: movw #0,2,-sp Allocate 20 bytes for the structure big[20]: leas -20,sp

8-14 Ramesh Yerraballi …Stages Access to a local variable is a read or write operation that occurs during execution. Examples: Set the local variable sum to 0: movw #0,sum,sp Increment the local variable sum: ldd sum,sp addd #1 std sum,sp ;sum=sum+1 Deallocation is the release of memory storage for the location variable. pulx ;deallocate sum Equivalently: ins ins ;deallocate sum Deallocate 20 bytes for the structure big[20]: leas 20,sp

8-15 Ramesh Yerraballi Example org $4000 ; calculate sum of numbers ; Input: RegD num ; Output: RegD Sum of 1,2,3,...,num ; Errors: may overflow ; 1) binding num set 2 ;loop counter 1,2,3 sum set 0 ;running calc ; 2) allocation pshd ;allocate num movw #0,2,-sp ;sum=0 ; 3) access loop ldd sum,sp addd num,sp std sum,sp ;sum = sum+num ldd num,sp subd #1 std num,sp ;num = num-1 bne loop ldd sum,sp ;result ; 4) deallocate leas 4,sp rts main lds #$4000 ldd #100 jsr calc bra * org $FFFE fdb main SP-> sum SP+2-> num SP+4-> return address