1 of 28

EE 319K�Introduction to Embedded Systems

Lecture 9: Stack and Local Variables, Fixed-Point Numbers

Bard, Cuevas, Erez, Gerstlauer, Holt, Valvano, Yerraballi, Telang, Tiwari

9-1

2 of 28

Agenda

Agenda

  • Local Variables
  • Stack and Activation Records
    • Register allocation
    • SP-relative addressing
    • R11-relative addressing (stack frame pointer)
  • Fixed-point numbers�

Next time

  • Decimal to ASCII conversion
  • LCD Interfacing

Bard, Cuevas, Erez, Gerstlauer, Holt, Valvano, Yerraballi, Telang, Tiwari

9-2

3 of 28

LCD – Lab7

  • Lab assignment
    • Interface LCD to TI board
    • Develop device driver to serve as interface between TM4C123 and ST7735R display
    • Write the modules you are responsible for
    • Test on Simulator first
    • Build circuit and test on real board

Bard, Cuevas, Erez, Gerstlauer, Holt, Valvano, Yerraballi, Telang, Tiwari

9-3

4 of 28

Local Variables

  • Scope �=> from where can it be accessed?
    • Private means restricted to
      • function where it is defined
      • specific code block { … }
      • file where is it defined
    • Public means any software can access it
  • Allocation/Lifetime/Persistence �=> when is it created & destroyed?
    • Dynamic allocation using registers or stack
    • Permanent allocation assigned in RAM

Bard, Cuevas, Erez, Gerstlauer, Holt, Valvano, Yerraballi, Telang, Tiwari

9-4

5 of 28

  • Local or automatic variables �=> Private scope, dynamic allocation
    • Temporary information
    • Scope: used only inside the function
    • Lifetime
      • Allocated when entered,
      • Used in the body, then
      • Deallocated on exit
    • Implementation
      • Registers
      • Allocate on stack and use SP to access
      • Allocate on stack and use R11 (stack frame pointer)

Local Variables

Bard, Cuevas, Erez, Gerstlauer, Holt, Valvano, Yerraballi, Telang, Tiwari

9-5

6 of 28

Variables in C

  • Global
    • Public scope
    • Permanent allocation
    • Bad style�

��

  • File-private static
    • Private scope to file
    • Permanent allocation
    • Sharing: ISR ⬄ Functions

��

  • Local - Automatic
    • Private scope,
    • Dynamic allocation

��

  • Local - Static
    • Private scope to function
    • Permanent allocation��

Reduce the scope as much as possible: need to know basis

9-6

7 of 28

Globals in C

  • Global
    • Public scope
    • Permanent allocation
    • Bad style

��

��

��

// accessible by all modules �int16_t myGlobalVariable;

// accessible this file only�static int16_t myPrivateStaticVariable;

void MyFunction(void){� int16_t myLocalVariable;

static int16_t count=0;

count++; �}

9-7

8 of 28

Local-automatic in C

Local - Automatic

    • Private scope,
    • Dynamic allocation

// accessible by all modules �int16_t myGlobalVariable;

// accessible this file only�static int16_t myPrivateStaticVariable;

void MyFunction(void){� int16_t myLocalVariable;

static int16_t count=0;

count++; �}

9-8

9 of 28

Static in C

File-private static

    • Private scope to file
    • Permanent allocation
    • Sharing: ISR ⬄ Functions

��

// accessible by all modules �int16_t myGlobalVariable;

// accessible this file only�static int16_t myPrivateStaticVariable;

void MyFunction(void){� int16_t myLocalVariable;

static int16_t count=0;

count++; }

9-9

10 of 28

Static in C

  • Local - Static
    • Private scope to function
    • Permanent allocation
    • Initialized once at reset��

Reduce the scope as much as possible: need to know basis

// accessible by all modules �int16_t myGlobalVariable;

// accessible this file only�static int16_t myPrivateStaticVariable;

void MyFunction(void){� int16_t myLocalVariable;

static int16_t count=0;

count++; �}

9-10

11 of 28

Functions in C

  • Public Function
    • Place a prototype in header file
    • Module specified in name�

  • Private Function
    • No prototype in header file

// callable by other �// routines in this file only �void static MyPrivateFunction(void){…}

// callable by all modules �void SysTick_Init(void){…}

Bard, Cuevas Erez, Gerstlauer, Holt, Valvano, Yerraballi, Telang, Tiwari

9-11

12 of 28

  • Allocation/release allows reuse of memory
  • Limited scope provides for data protection
  • Only program that created it can access it

Why use Locals?

Why use Stack? Why use Registers?

  • Large number
  • Arrays
  • Simple
  • Fast

Bard, Cuevas, Erez, Gerstlauer, Holt, Valvano, Yerraballi, Telang, Tiwari

9-12

13 of 28

Recall Stack Rules

  • Program segments should have an equal number of pushes and pulls
  • Push with multiple registers will always put the lower numbered register’s contents in the lower address.
  • Pop with multiple registers will always get the lower numbered register’s contents from the lower address.

Push

  1. SP=SP-4
  2. Store 32 bits at SP

Pop

  1. Read 32 bits at SP
  2. SP=SP+4

9-13

14 of 28

Variables on the stack

  • Many inputs or arrays

void play_note(uint16_b pitch,

uint16_b duration,

uint16_b loud_left,

uint16_b loud_right,

uint16_b timber_idx,

uint16_b attack_rate,

uint16_b attack_type,

uint16_b decay_rate,

uint16_b decay_type);

void Function(void){char Buffer[100];

}

9-14

15 of 28

Local variables using Registers

; *****binding phase***********

sum RN 4 ;32-bit unsigned

n RN 5 ;32-bit unsigned

; 1)**** no allocation phase **

Calc PUSH {R4,R5}

MOV n,R0

; 2)******access phase ********

MOV sum,#0

loop ADD sum,sum,n ;sum+n

SUBS n,n,#1 ;n-1

BNE loop

; 3)***no deallocation phase **

MOV R0,sum

POP {R4,R5}

BX LR ;R0=sum

// input: n 32-bit number

// output: n+(n-1)+(n-2)+...+2+1

uint32_t Calc(uint32_t n) {

uint32_t sum;

sum = 0;

do{

sum=sum+n;

n--;

} while(n>0);

return sum;

}

R4 sum

R5 n

Use R4-R11 as locals when this function calls another, because the other function by AAPCS will preserve the values of R4-R11

Use R0-R3,R12 as locals when this function doesn’t call another function, because you do not need to preserve R0-R3,R12

The correct Lab 7 solutions have no PUSH and POP during access phase

9-15

16 of 28

Stack frame using SP

  • Binding using EQU
    • Draw a stack figure

  • Allocate by making space

  • Access using SP-relative addressing

  • Deallocate by freeing (balancing) stack

SUB SP,SP,#16

STR R0,[SP,#y]

ADD SP,SP,#16

y EQU 8

9-16

17 of 28

Stack frame using SP

; *****binding phase***************

sum EQU 0 ;32-bit unsigned number

n EQU 4 ;32-bit unsigned number

; 1)*****allocation phase *********

Calc PUSH {R0} ;allocate, init n

SUB SP,#4 ;allocate sum

; 2)******access phase ************

MOV R0,#0

STR R0,[SP,#sum] ;sum=0

loop LDR R1,[SP,#n] ;R1=n

LDR R0,[SP,#sum] ;R0=sum

ADD R0,R0,R1 ;R0=sum+n

STR R0,[SP,sum] ;sum=sum+n

LDR R1,[SP,#n] ;R1=n

SUBS R1,R1,#1 ;n-1

STR R1,[SP,#n] ;n=n-1

BNE loop

; 3)******deallocation phase *****

LDR R0,[SP,#sum] ;R0=sum

ADD SP,#8 ;deallocation

BX LR ;R0=sum

// input: n 32-bit number

// output: n+(n-1)+(n-2)+...+2+1

uint32_t Calc(uint32_t n) {

uint32_t sum;

sum = 0;

do{

sum=sum+n;

n--;

} while(n>0);

return sum;

}

Stack pointer implementation of a function with two local 32-bit variables.

9-17

18 of 28

Frame pointer using R11

; *****binding phase***************

sum EQU 0 ;32-bit unsigned number

n EQU 4 ;32-bit unsigned number

; 1)*****allocation phase *********

calc PUSH {R4,R5,R11,LR}

SUB SP,#8 ;allocate n,sum

MOV R11,SP ;frame pointer

; 2)******access phase ************

MOV R0,#0

STR R0,[R11,#sum] ;sum=0

MOV R1,#1000

STR R1,[R11,#n] ;n=1000

loop LDR R1,[R11,#n] ;R1=n

LDR R0,[R11,#sum] ;R0=sum

ADD R0,R1 ;R0=sum+n

STR R0,[R11,sum] ;sum=sum+n

LDR R1,[R11,#n] ;R1=n

SUBS R1,#1 ;n-1

STR R1,[R11,#n] ;n=n-1

BNE loop

; 3)******deallocation phase *****

ADD SP,#8 ;deallocation

POP {R4,R5,R11,PC} ;R0=sum

uint32_t calc(void){

uint32_t sum,n;

sum = 0;

for(n=1000;n>0;n--){

sum=sum+n;

}

return sum;

}

Frame pointer implementation of a function with two local 32-bit variables.

9-18

19 of 28

Push parameters on stack

; Inputs R0 is x

; R1 is y

; R2 is z

; Output R0 is return value

sum EQU 0 ;32-bit signed number

x EQU 4 ;32-bit signed number

y EQU 8 ;32-bit signed number

z EQU 12 ;32-bit signed number

Add3 PUSH {R0,R1,R2,LR}

SUB SP,#4 ;allocate sum

; body of the function

LDR R0,[SP,#x]

ADD R0,R0,[SP,#y]

ADD R0,R0,[SP,#z]

STR R0,[SP,#sum]

ADD SP,#16 ;deallocate

POP {PC}

int32_t Add3(int32_t x, int32_t y,

int32_t z) {

int32_t sum;

sum = x+y+z;

return sum;

}

Pushing parameters on stack makes them similar to local variables

9-19

20 of 28

Example using SP

  • Parameters are like local variables

uint32_t Example2(uint32_t x){

uint32_t z;

z = x+1;

return z;

}

9-20

21 of 28

Example using SP

  • Array local variables go on the stack

void Example3(void){

char buffer[16];

int i;

i=15;

do{

buffer[i]=0;

i--;

}while (i>=0);

}

9-21

22 of 28

Example using R11

  • Array local variables go on the stack

void Example3(void){

char buffer[16];

int i;

i=15;

do{

buffer[i]=0;

i--;

}while (i>=0);

}

9-22

23 of 28

Fixed-Point Numbers

Why? (wish to represent non-integer values)

  • Lab 7 measures distance from 0 to 2 cm�E.g., 1.234 cm

When? (range is known, range is small)

  • Range is 0 to 2cm
  • Resolution is 0.001 cm

Bard, Cuevas, Erez, Gerstlauer, Holt, Valvano, Yerraballi, Telang, Tiwari

9-23

24 of 28

Fixed-Point Numbers

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

Bard, Cuevas, Erez, Gerstlauer, Holt, Valvano, Yerraballi, Telang, Tiwari

9-24

25 of 28

Fixed-Point Numbers: Decimal

Decimal Fixed-Point

(Value = I*10m)

I is a 16-bit unsigned integer (variable integer)

Δ = 10m decimal fixed-point (fixed constant)

For example, with m=-3 (resolution of 0.001 or milli) the value range is 0.000 to 65.535 (with 16-bit)

What is 𝜋 represented as, in Decimal Fixed-Point?� 𝜋 (3.14159…) = I*10-3 � => I = Integer approximation of (3.14159…*103)

I = Integer approximation of (3141.59)

I = 3142

Decimal Fixed-Point numbers are human-friendly

-easy to input/output to humans

Bard, Cuevas, Erez, Gerstlauer, Holt, Valvano, Yerraballi, Telang, Tiwari

9-25

26 of 28

Fixed-Point Numbers: Binary

Binary Fixed-Point

(Value = I*2m)

I is a 16-bit unsigned integer (variable integer)

Δ = 2m binary fixed-point (fixed constant)

For example with m=-8 (resolution of 1/256)

What is 𝜋 represented as, in binary Fixed Point?

𝜋 (3.14159…)= I*2-8

=> I = Integer approximation of (3.14159…*28)

I = Integer approximation of (804.2477) � I = 804

Binary Fixed-Point numbers are computer-friendly

-runs very fast because shifting is fast

Bard, Cuevas, Erez, Gerstlauer, Holt, Valvano, Yerraballi, Telang, Tiwari

9-26

27 of 28

Examples

  • Lab 7 fixed-point resolution is 0.001cm

  • Fixed-point resolution is 0.01cm

  • Fixed-point resolution is 0.125cm

How do we store the value 1.2 cm?

How do we store the value 1.2 cm?

How do we store the value 1.2 cm?

9-27

28 of 28

Review

Range is 0 to 100 gal, resolution 0.0001 gal

If interfacing to computers, should we use decimal or binary?

What is Δ?

What C variable type?

Why?

If interfacing to humans, should we use decimal or binary?

What is Δ?

What C variable type?

Why?

9-28