Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "8-1 EE 319K Introduction to Microcontrollers Lecture 8:Fixed Point Numbers, Local Variables, Binding, Allocation, Access, Deallocation."— Presentation transcript:

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

2 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., 1.234 cm When? (range is known, range is small)  Range is 0 to 3cm Resolution is 0.003 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?)

3 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 0.001 or milli) the value range is 0.000 to 65.535 What is  represented as, in Decimal Fixed Point?  (3.14159…) = I*10 -3 => I = Integral approximation of(3.14159…*10 3 ) I = Integral approximation of(3141.59) I = 3142 Decimal Fixed-point numbers are human-friendly

4 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?  (3.14159…)= I*2 -8 => I = Integral approximation of(3.14159…*2 8 ) I = Integral approximation of(804.2477) => I = 804 Binary Fixed-point numbers are computer- friendly

5 8-5 Ramesh Yerraballi Output Output an integer. Assume integer, n, is between 0 and 9999. 1.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 0.001. 1.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

6 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

7 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-8 Ramesh Yerraballi Registers are Local Variables LineProgramRegB (Local) RegX (Global) RegY (Local) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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

9 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++; }

10 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

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

12 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

13 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

14 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

15 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


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

Similar presentations


Ads by Google