Transcript Lec8
EE 319K
Introduction to Microcontrollers
Lecture 8:Fixed Point Numbers,
Local Variables, Binding,
Allocation, Access, Deallocation
8-1
Fixed Point Numbers
Why? (wish to
represent noninteger 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
Ramesh Yerraballi
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-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
Binary Fixed-point numbers are computerfriendly
Ramesh Yerraballi
8-4
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
Ramesh Yerraballi
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
8-5
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
Ramesh Yerraballi
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-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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Main lds
bsr
ldab
stab
ldx
FSM ldab
lslb
lslb
stab
ldy
bsr
ldab
andb
lslb
abx
ldx
bra
RegB
(Local)
#$4000
Timer_Init
#$FC
DDRT
#goN
OUT,x
PTT
WAIT,x
Timer_Wait10ms
PTT
#$03
NEXT,x
FSM
RegX
(Global)
RegY
(Local)
$FC
$FC
Output
Output
Output
Output
Input
Input
Input
Input
Pt
Pt
Pt
Pt
Pt
Pt
Pt
Pt
Pt
Pt
Pt
Pt
Pt
Wait
Wait
Program 7.1: FSM Controller
Ramesh Yerraballi
8-8
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
Ramesh Yerraballi
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-9
9S12 Stack
Empty Stack
Stack with 3 elements
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
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
Ramesh Yerraballi
8-13
…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
Ramesh Yerraballi
;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-14
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
Ramesh Yerraballi
;sum = sum+num
ldd
subd
std
bne
ldd
num,sp
#1
num,sp
loop
sum,sp
;num = num-1
;result
; 4) deallocate
leas 4,sp
rts
main lds
ldd
jsr
bra
org
fdb
#$4000
#100
calc
*
$FFFE
main
SP
-> sum
SP+2 -> num
SP+4 -> return address
8-15