0% found this document useful (0 votes)
60 views15 pages

EE 319K Introduction To Microcontrollers

The document discusses fixed point numbers for representing non-integer values by using an integer to store the value and a fixed constant to represent the resolution, and it describes the stages of local variable allocation on the stack including binding variables to addresses, allocating memory on the stack, accessing variables during execution, and deallocating memory when finished.

Uploaded by

so16ae
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views15 pages

EE 319K Introduction To Microcontrollers

The document discusses fixed point numbers for representing non-integer values by using an integer to store the value and a fixed constant to represent the resolution, and it describes the stages of local variable allocation on the stack including binding variables to addresses, allocating memory on the stack, accessing variables during execution, and deallocating memory when finished.

Uploaded by

so16ae
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

EE 319K

Introduction to Microcontrollers

Lecture 8:Fixed Point Numbers,


Local Variables, Binding,
Allocation, Access, Deallocation

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

Ramesh Yerraballi 8-2


Fixed Point Numbers: Decimal
Decimal
(Value = I*10m)
I is a 16-bit unsigned integer
 = 10m 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…*103)
I = Integral approximation of(3141.59)
I = 3142
Decimal Fixed-point numbers are human-friendly

Ramesh Yerraballi 8-3


Fixed Point Numbers: Binary
Binary
(Value = I*2m)
I is a 16-bit unsigned integer
 = 2m 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…*28)
I = Integral approximation of(804.2477)
=> I = 804
Decimal Fixed-point numbers are computer-
friendly

Ramesh Yerraballi 8-4


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

Ramesh Yerraballi 8-5


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

Ramesh Yerraballi 8-6


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?)

Ramesh Yerraballi 8-7


Registers are Local Variables
Line Program RegB RegX RegY
(Local) (Global) (Local)
1 Main lds #$4000
2 bsr Timer_Init
3 ldab #$FC $FC
4 stab DDRT $FC
5 ldx #goN Pt
6 FSM ldab OUT,x Output Pt
7 lslb Output Pt
8 lslb Output Pt
9 stab PTT Output Pt
10 ldy WAIT,x Pt Wait
11 bsr Timer_Wait10ms Pt Wait
12 ldab PTT Input Pt
13 andb #$03 Input Pt
14 lslb Input Pt
15 abx Input Pt
16 ldx NEXT,x Pt
17 bra FSM Pt
Program 7.1: FSM Controller
Ramesh Yerraballi 8-8
In C
 Global Variables  Local variables
 Public: global scope,
permanent allocation  Public: local scope,
dynamic allocation
short myGlobalVariable;
// accessible by all programs
void MyFunction(void){
void MyFunction(void){…} short myLocalVariable;
}

 Private: global scope(only  Private: local scope,


to the file), permanent permanent allocation
allocation
void MyFunction(void){
static short
myPrivateGlobalVariable; // static short count;
accessible by this file // count++;
only }
void static
MyPrivateFunction(void){…}
// callable by other
// routines in this file
// only

Ramesh Yerraballi 8-9


9S12 Stack
EmptyStack Stackwith3elements

SP top
next
SP

The tsx and tsy instructions do not modify the stack pointer.
Below: The tsx instruction creates a stack frame pointer

Ramesh Yerraballi 8-10


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.

Ramesh Yerraballi 8-11


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

Ramesh Yerraballi 8-12


Stages
Binding is the Allocation is the
assignment of the generation of
address (not value) memory storage for
to a symbolic name. the local variable.
Examples:
Examples: pushx ; allocate sum
sum set 0 ;16-bit local ; uninitialized value
; variable 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

Ramesh Yerraballi 8-13


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

Ramesh Yerraballi 8-14


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

You might also like