c prog for embedded prog.ppt

Upload: mohammad-gulam-ahamad

Post on 14-Apr-2018

243 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 C prog for embedded prog.ppt

    1/30

    1

  • 7/27/2019 C prog for embedded prog.ppt

    2/30

    Memory UsageCommon 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

    2

  • 7/27/2019 C prog for embedded prog.ppt

    3/30

    Memory Usagestatic char[] greeting=Hello world!;

    main()

    {int i;

    char bVal;

    LCD_init();

    LCD_PutString(greeting);

    }

    3

    Static Data

    Code

    Heap (grows up)

    Stack

    (grows down)

    Low end

    High endI/O addresses

  • 7/27/2019 C prog for embedded prog.ppt

    4/30

    Variable Scope and StorageGlobal data:

    Declared outside all C functions

    Visible to all C functions

    4

  • 7/27/2019 C prog for embedded prog.ppt

    5/30

    Variable Scope and StorageVariables with static storage

    Storage space allocated in data sections

    Life time is from the start to the end of programexecution

    5

  • 7/27/2019 C prog for embedded prog.ppt

    6/30

    Variable Scope and StorageVariables in stack:

    Local variables of a function go into stack storage

    Dynamically allocated and de-allocated every time thefunction is called

    Variable life time is from the allocation to thedeallocation

    6

  • 7/27/2019 C prog for embedded prog.ppt

    7/30

    Variable Scope and Storagescope: Where a variable is visible

    int m;

    int any_func(){

    int m;m = n = 5;

    }

    7

  • 7/27/2019 C prog for embedded prog.ppt

    8/30

    Variable Scope and StorageC 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;

    }

    8

  • 7/27/2019 C prog for embedded prog.ppt

    9/30

    Variable Scope and StorageExample: 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 */

    9

  • 7/27/2019 C prog for embedded prog.ppt

    10/30

    Data Memory LayoutAlignment: A scalar variable ofn bytes must be aligned

    on n-byte boundaries

    Examples:

    char: no restriction

    short: two-byte boundaries

    float: four-byte boundaries

    10

  • 7/27/2019 C prog for embedded prog.ppt

    11/30

    Data Memory LayoutExample: Whats the memory layout? Whats the address

    of each variable? Assume a starts at address0x20000000.

    char a[5];

    short b;

    int d;

    double e;short f;

    char *g;

    11

  • 7/27/2019 C prog for embedded prog.ppt

    12/30

    Data Memory LayoutByte-ordering: Big-endian vs. Little-endian

    Big-endian: Byte 0 of a n-byte variable is the MostSignificant Byte (MSB)

    Little-endian: Byte 0 of a n-byte variable is the Least

    Significant Byte (LSB)

    12

  • 7/27/2019 C prog for embedded prog.ppt

    13/30

    Data Memory LayoutWhats the value in string with the two endian types?

    static char string[8];* (int*) string = 0x41424344;

    string[4] = \0;

    Note: ASCII 0x41 is A.

    13

  • 7/27/2019 C prog for embedded prog.ppt

    14/30

    Data Memory LayoutAddress 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 a[5];

    short b;

    int d;

    double e;

    short f;

    char *g;14

  • 7/27/2019 C prog for embedded prog.ppt

    15/30

    Data Memory LayoutWhat 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;

    }

    15

  • 7/27/2019 C prog for embedded prog.ppt

    16/30

    Function and StackStack: LIFO structure (Last-in, First-out)

    Stack top and stack base to track usage

    Push operation: add an item at the top and update thetop

    Pop operation: remove an item from the top and updatethe top

    16

  • 7/27/2019 C prog for embedded prog.ppt

    17/30

    Function and StackConventional stack grows downwards

    17

    low address

    high address

    stack top

    stack growth

  • 7/27/2019 C prog for embedded prog.ppt

    18/30

    Function and StackFunction Frame: Local storage for a function

    Example: 1. A is called; 2. A calls B; 3. B calls C; 4. C

    returns

    18

  • 7/27/2019 C prog for embedded prog.ppt

    19/30

    Function and StackWhy stack?

    The LIFO order matches perfectly with functionscall/return order

    Efficient memory allocation and de-allocation

    19

  • 7/27/2019 C prog for embedded prog.ppt

    20/30

    Function and StackDetermine 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 szName[10]; /* statements start here */

    }

    20

  • 7/27/2019 C prog for embedded prog.ppt

    21/30

    Function and StackFunction 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) tothat address

    A function pointer variable stores the pointer to thestarting address of the block

    21

  • 7/27/2019 C prog for embedded prog.ppt

    22/30

    Function and StackFunction pointer: Use exampleint average(int x, int y){

    }

    int main(){

    int (*pFunc)(int x, int y) = average;int a = (*pFunc) (10, 20);}

    22

  • 7/27/2019 C prog for embedded prog.ppt

    23/30

    C Library FunctionsIn 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

    23

  • 7/27/2019 C prog for embedded prog.ppt

    24/30

    C Library FunctionsUse standard file I/O

    /* include the header file for I/O lib */

    #include

    main()

    {

    /* use the fprintf function */fprintf(stdout, %s\n, Hello World\n);

    }

    24

  • 7/27/2019 C prog for embedded prog.ppt

    25/30

    C Library FunctionsFormatted output: printf, fprintf, sprintf and more; useconversion 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 formata = , b =, c = , str = in a single line?

    int a;float b;int *c;char str[10];

    25

  • 7/27/2019 C prog for embedded prog.ppt

    26/30

    C Library FunctionsFormatted input: scanf, fscanf, sscanf and more

    Use similar conversion specifiers as the printf family

    How to read the following variables in formata = , b =, c = , str = in a single line? (Usescanf)

    int a, b;

    float c;

    char str[10];

    26

  • 7/27/2019 C prog for embedded prog.ppt

    27/30

    C Library FunctionsString operations: copy, compare, parse strings and more

    #include

    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

    27

  • 7/27/2019 C prog for embedded prog.ppt

    28/30

    C Library FunctionsError processing and reporting: use exit function#include #include

    ...void myfunc(int x){if (x < 0) {fprintf(stderr, %s

    \n,

    x is out of range);exit(-1);

    }}

    28

  • 7/27/2019 C prog for embedded prog.ppt

    29/30

    C Library FunctionsMath library functions

    #include

    n = round (x); /* FP round function */

    To build:

    gcc Wo myprogram

    lm myprogram.c

    29

  • 7/27/2019 C prog for embedded prog.ppt

    30/30

    C Library FunctionsHow to find more?

    On Linux machines: Use manman printf

    man string

    man string.h

    man math.h

    30