8-1 ee 319k introduction to microcontrollers lecture 8:fixed point numbers, local variables,...

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

Upload: ruby-ellis

Post on 18-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

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

8-1

EE 319KIntroduction to Microcontrollers

Lecture 8:Fixed Point Numbers, Local Variables, Binding,

Allocation, Access, Deallocation

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

8-2Ramesh Yerraballi

Fixed Point Numbers

Why? (wish to represent non-integer values)

Next lab measures distance from 0 to 3 cmE.g., 1.234 cm

When? (range is known, range is small)

Range is 0 to 3cmResolution 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?)

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

8-3Ramesh Yerraballi

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 = 3142Decimal Fixed-point numbers are human-friendly

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

8-4Ramesh Yerraballi

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 computer-friendly

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

8-5Ramesh Yerraballi

Output

Output an integer. Assume integer, n, is

between 0 and 9999. 1. OutChar($30+n/1000)

;thousand’s digit2. n = n%1000

OutChar($30+n/100);hundred’s digit

3. n = n%100OutChar($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%1000OutChar($2E);decimal pointOutChar($30+n/100);hundred’s digit

3. n = n%100OutChar($30+n/10);ten’s digit

4. OutChar ($30+n%10);one’s digit

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

8-6Ramesh Yerraballi

Local Variables - Terminology

TerminologyScope: 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

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

8-7Ramesh 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 relocatableThe number of variables is more than

registers (answer to, Why not registers?)

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

8-8Ramesh Yerraballi

Registers are Local VariablesLine Program RegB

(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

lslb

stab PTT

ldy WAIT,x

bsr Timer_Wait10ms

ldab PTT

andb #$03

lslb

abx

ldx NEXT,x

bra FSM

$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

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

8-9Ramesh 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 // onlyvoid 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++; }

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

8-10Ramesh Yerraballi

9S12 Stack

Empty Stack

SP

Stack with 3 elements

SP topnext

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

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

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

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

8-12Ramesh 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

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

8-13Ramesh 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 valueEquivalently:des ;allocate sumdesTo do the same but initialize:movw #0,2,-spAllocate 20 bytes for the

structure big[20]:leas -20,sp

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

8-14Ramesh 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 sumEquivalently:ins ins ;deallocate sum

Deallocate 20 bytes for the structure big[20]:

leas 20,sp

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

8-15Ramesh 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 -> sumSP+2 -> numSP+4 -> return address