Introduction to C Programming for Embedded Programming - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Introduction to C Programming for Embedded Programming

Description:

int d; double e; short f; char *g; 12. Data Memory Layout ... double c; char d; 16. Function and Stack. Stack: LIFO structure (Last-in, First-out) ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 31
Provided by: dianet4
Category:

less

Transcript and Presenter's Notes

Title: Introduction to C Programming for Embedded Programming


1
Introduction to C Programming for Embedded
Programming
  • Data memory layout, Function and stack, and
    library functions

2
Memory Usage
  • Common memory sections
  • Code holds binary instructions of each function
  • Static data stores static data accessible to all
    functions
  • Stack stores run-time data local to each
    function
  • Heap holds dynamically allocated data
  • I/O does not really hold anything

3
Memory Usage
High end
  • static char greeting Hello world!
  • main()
  • int i
  • char bVal
  • LCD_init()
  • LCD_PutString(greeting)

I/O addresses
Stack(grows down)
Heap (grows up)
Static Data
Code
Low end
4
Variable Scope and Storage
  • Global data
  • Declared outside all C functions
  • Visible to all C functions
  • http//class.ece.iastate.edu/CprE211/labs/lectures
    /elevator.htm

5
Variable Scope and Storage
  • Variables with static storage
  • Storage space allocated in data sections
  • Life time is from the start to the end of program
    execution

6
Variable Scope and Storage
  • Variables in stack
  • Local variables of a function go into stack
    storage
  • Dynamically allocated and de-allocated every time
    the function is called
  • Variable life time is from the allocation to the
    deallocation

7
Variable Scope and Storage
  • scope Where a variable is visible
  • int m
  • int any_func()
  • int m
  • m n 5

8
Variable Scope and Storage
  • C global variable (visible to all program files)
  • int global_var
  • C file-wide static variables (visible only in
    this file)
  • static int static_var
  • Local static variables
  • any_func()
  • static int static_var

9
Variable Scope and Storage
  • Example How to define and use global variables
  • In header file myvar.h
  • extern int global_var
  • In program file myvar.c
  • include myvar.h
  • int global_var
  • In program file usevar.c
  • include myvar.h
  • / use myvar /

10
Data Memory Layout
  • Alignment A scalar variable of n bytes must be
    aligned on n-byte boundaries
  • Examples
  • char no restriction
  • short two-byte boundaries
  • float four-byte boundaries

11
Data Memory Layout
  • Example Whats the memory layout? Whats the
    address of each variable? Assume a starts at
    address 0x20000000.
  • char a5
  • short b
  • int d
  • double e
  • short f
  • char g

12
Data Memory Layout
  • Byte-ordering Big-endian vs. Little-endian
  • Big-endian Byte 0 of a n-byte variable is the
    Most Significant Byte (MSB)
  • Little-endian Byte 0 of a n-byte variable is the
    Least Significant Byte (LSB)

13
Data Memory Layout
  • Whats the value in string with the two endian
    types?
  • static char string8
  • (int) string 0x41424344
  • string4 \0
  • Note ASCII 0x41 is A.

14
Data Memory Layout
  • Address Memory Contents________
  • 30001FF8 25 24 5B 1A AC 57 9C 8D
  • 30002000 00 FE 25 24 5B 1A EE 05
  • 30002008 AC AE 81 83 45 67 89 25
  • 30002010 01 23 55 3F AC CB F0 8D
  • What are the variable contents on MPC555?
  • char a5
  • short b
  • int d
  • double e
  • short f
  • char g

15
Data Memory Layout
  • What is the size of the following structure?
  • Each component must be aligned
  • Total size must be a multiple of eight
  • struct mystruct
  • int a
  • short b
  • double c
  • char d

16
Function and Stack
  • Stack LIFO structure (Last-in, First-out)
  • Stack top and stack base to track usage
  • Push operation add an item at the top and update
    the top
  • Pop operation remove an item from the top and
    update the top

17
Function and Stack
  • Conventional stack grows downwards

high address
stack top
stack growth
low address
18
Function and Stack
  • Function Frame Local storage for a function
  • Example 1. A is called 2. A calls B 3. B calls
    C 4. C returns

19
Function and Stack
  • Why stack?
  • The LIFO order matches perfectly with functions
    call/return order
  • Efficient memory allocation and de-allocation

20
Function and Stack
  • Determine frame layout
  • Each variable is aligned
  • Each frame is on eight-byte boundaries
  • Example What is the frame size? (Add 8 bytes for
    return address and frame linking)
  • int myfunc()
  • char byVal
  • long lVal
  • char szName10
  • / statements start here /

21
Function and Stack
  • Function pointer How does it work?
  • A function is translated into a block of machine
    instructions
  • Calling the function is to transfer the program
    control to the starting address of the block
  • At hardware level this is done by setting the
    program counter
  • At assembly programming level this is to branch
    (jump) to that address
  • A function pointer variable stores the pointer to
    the starting address of the block

22
Function and Stack
  • Function pointer Use example
  • int average(int x, int y)
  • int main()
  • int (pFunc)(int x, int y) average
  • int a (pFunc) (10, 20)

23
C Library Functions
  • In C many things are carried out by library
    functions
  • Simple language, rich libraries
  • Commonly used libraries
  • File I/O (include user input/output)
  • String manipulations
  • Mathematical functions
  • Process management
  • Networking

24
C Library Functions
  • Use standard file I/O
  • / include the header file for I/O lib /
  • include ltstdio.hgt
  • main()
  • / use the fprintf function /
  • fprintf(stdout, s\n, Hello World\n)

25
C Library Functions
  • Formatted output printf, fprintf, sprintf and
    more use conversion specifiers as follows
  • s string
  • d signed decimal
  • u unsigned decimal
  • x hex
  • f floating point (float or double)
  • How to output the following variables in format
  • a , b , c , str in a single line?
  • int a
  • float b
  • int c
  • char str10

26
C Library Functions
  • Formatted input scanf, fscanf, sscanf and more
  • Use similar conversion specifiers as the printf
    family
  • How to read the following variables in format
  • a , b , c , str in a single line?
    (Use scanf)
  • int a, b
  • float c
  • char str10

27
C Library Functions
  • String operations copy, compare, parse strings
    and more
  • include ltstring.hgt
  • strcpy copy one string to another
  • strcmp compare two strings
  • strlen calculate the length of a string
  • strstr search a string for the occurrence of
    another string

28
C Library Functions
  • Error processing and reporting use exit function
  • include ltstdio.hgt
  • include ltstdlib.hgt
  • ...
  • void myfunc(int x)
  • if (x lt 0)
  • fprintf(stderr, s\n,
  • x is out of range)
  • exit(-1)

29
C Library Functions
  • Math library functions
  • include ltmath.hgt
  • n round (x) / FP round function /
  • To build
  • gcc W o myprogram lm myprogram.c

30
C Library Functions
  • How to find more?
  • On Linux machines Use man
  • man printf
  • man string
  • man string.h
  • man math.h
Write a Comment
User Comments (0)
About PowerShow.com