Instructions and Memory C code Why are these 16 bits? a = b + c Assembly Code ldr r0, [sp, #4] ldr adds r1, [sp] r0, r0, r1 str r0, [sp, #8] Machine Code 09801 09900 01840 09002 Memory 0 0 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 0 0 0 0 Memory Address 00000 336A 00000 336C 00000 336E 00000 3370 2 tj
Program Flow Variables and Memory int foo; char initial1 = t ; float rate = 2.5; char initial2; int boo = 255; reserved for foo has garbage in it initial 1 t he 074 initial 2 - garbage unused - alignment 2.5 10.1 binary 1.01 2 1 0 sign 01 mantissa 1000000 eponent boo 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Memory Address 00001 1000 foo references this memory address 00001 1004 intial1 references this memory address 00001 1008 00001 100C 00001 1010 3 tj
Variables and Memory uint32_t a = 1; char e = 'a'; uint32_t aa = 1; uint16_t b = 2; char f = 'a'; uint16_t bb = 2; uint8_t c = 3; uint8_t cc = 3; float d = 1.123; char g = 'a'; float dd = 1.123; char h = 'a'; char ee = 'a'; uint32: char: uint32: uint16: char: uint16: uint8: uint8: float: char: float: char: char: 2000ffd8 2000ffdc 2000ffe0 2000ffe4 2000ffe6 2000ffe8 2000ffea 2000ffeb 2000ffec 2000fff0 2000fff4 2000fff8 2000fff9 word aligned word aligned word aligned word aligned 2000 FFD8 2000 FFDC 2000 FFE0 2000 FFE4 2000 FFE6 01 00 00 00 61 01 00 00 00 02 00 61 2000 FFEC 2000 FFF0 2000 FFF4 2000 FFF8 2000 FFF9 77 BE 8F 3F 61 77 BE 8F 3F 61 61 2000 FFE8 2000 FFEA 2000 FFEB 4 tj 02 00 03 03
Linear Flow Series of instructions No decisions no change in program flow foo = a + b; soo = foo++; a = foo + soo; Eecuting instructions in line Program Memory Eception Vectors Linear Flow Tet Eecutable Code Read Only - Data Copy of RAM Data section Boot Loader 5 tj
Conditional Flow Series of instructions Decisions change in program flow if (a==b){ foo = a + b; soo = foo++; } else { a = foo + soo; } // end if Eecuting instructions in line Program Memory Eception Vectors if( else } // end if Tet Eecutable Code Read Only - Data Copy of RAM Data section Boot Loader 6 tj
Logic in C NUMBER LINE -2-1 0 1 2 TRUE FALSE TRUE 1 1 0 7 tj
Logical Operators int foo = 1; float boo = -2.3; char soo = a ;!foo!boo foo && boo!foo && soo boo soo foo!soo!(foo &&!boo) 0 0 1 0 1 1 1 8 tj
Evaluating Logical Epressions Short circuit evaluation foo boo foo boo++ stop evaluating if foo is true boo never gets incremented if foo is true True anything true False && anything false 9 tj
Two way decisions if else if (epression) statement 1 else statement 2 False Action(s) False Decision True True Action(s) 10 tj
Two way decisions if (j == 1) a++; else a--; if (j <= 5){ a++; b = a + 3; } else a--; // compound statement 11 tj
While loop while(epression) statement; while(epression){ statements; } eecute statements while epression is true 12 tj
For loop for(ep1; ep2; ep3) statement; ep1 -> initialization ep2 -> test ep3 -> update Typically used in counter controlled loops 13 tj
Do-While Loop do statement; while(epression); do{ statements; } while(epression); Often used in data validation situations 14 tj
Switch Statement If else allows a 2 way decision Switch allows for n-way decisions switch(variable){ case val1: statement; case val2: statement; case val3: statement; default: statement; } variable must be an integral value case val1 false case val2 false case val3 false true true true Case val1 Action(s) Case val2 Action(s) Case val3 Action(s) Default Action(s) 15 tj
Switch Statement Switch with break switch(variable){ case val1: statement; statement; break; case val1: statement; statement; break; default: statement; statement; break; } Case 1 Action(s) Decision Case 2 Action(s) Default Action(s) 16 tj
else if Actually not a new command Special case of nested if if(epr1){ } else if(epr2){ } else if(epr3){ } else{ } if(epr1){ } else if(epr2){ } else if(epr3){ } else{ } } } eprx should be different cases of the same test 17 tj
Functions 18 tj
Function Structure Chart Calling Module Called Module Main Module Module 1 Module 2 Module 3 Module 1a Module 1b Module 1c Module 2a Module 3a Module 3b Allows solutions to be broken into manageable, understandable and logical pieces 19 tj
Function Structure Chart All communication must go through the Calling/Called Module path Main Module Module 1 Module 2 Module 3 Module 1a Module 1b Module 1c Module 2a Module 3a Module 3b 20 tj
Function Structure Chart All communication must go through the Calling/Called Module path Calling function has control Calling function calls a function The called function receives control When done the called function returns control to the calling function 21 tj
Functions and Memory Function call transfers eecution to a separate section of code (function) Places current PC on the stack When done, the function returns to the net line of code Using the PC previously stored on the stack Multiple calls to the same function transfer eecution to the same location One copy of the function Must be able to separate data from different calls Eecuting instructions in line Program Memory Eception Vectors Function Call Tet Eecutable Code Function Code Return Read Only - Data Copy of RAM Data section Boot Loader 22 tj
Functions and Memory Function call creates a space in the heap copy of the actual parameters any function variables (local variables) Function operates in this newly created space (scope) When the function returns, the space is reclaimed (not necessarily erased but no longer available) Data Memory Stack Return Address (PC) Function memory Copy of parameters Local variables Heap Bss Data 23 tj
Pointer Review variables in memory address for myvar1 02000 0020 address for myvar2 02000 0024 address for myvar3 02000 0028 4 Byte word 02000 0020 02000 0024 02000 0028 24 tj
Pointer A special Type A variable that holds the memory location of another variable Holds an address in our case 32 bits Each pointer must be tied to a specific data type int, float, char, 25 tj
Pointer To find the memory location of a variable use the address of operator: & &myvar1 02000 0020 &myvar2 02000 0024 &myvar3 02000 0028 Precedence Operator Description Associativity ++ -- Prefi increment and Right-to-left decrement + - Unary plus and minus! ~ Logical NOT and bitwise NOT 2 (type) Type cast * Indirection (dereference) & Address-of sizeof Size-of _Alignof Alignment requirement(c11) 26 tj
Pointer To declare a pointer variable follow the type declaration with a * Precedence Operator Description Associativity ++ -- Prefi increment and Right-to-left decrement + - Unary plus and minus! ~ Logical NOT and bitwise NOT 2 (type) Type cast * Indirection (dereference) & Address-of sizeof Size-of _Alignof Alignment requirement(c11) int* myvar1_ptr; // declare a pointer variable with name myvar1_ptr // that points to an integer variable float* myvar2_ptr; // declare a pointer variable with name myvar2_ptr // that points to a float variable 27 tj
Pointer Precedence Operator Description Associativity ++ -- Prefi increment and Right-to-left decrement + - Unary plus and minus! ~ Logical NOT and bitwise NOT 2 (type) Type cast * Indirection (dereference) & Address-of sizeof Size-of _Alignof Alignment requirement(c11) To determine the value of a variable pointed to by a pointer variable precede the pointer variable with * (dereference operator) *myvar1_ptr; // provides the value held in the memory location // pointed to by myvar1_ptr as an int *myvar2_ptr; // provides the value held in the memory location // pointed to by myvar2_ptr as a float 28 tj
Pointer int var1 = 5; // stored in memory location 02000 1010 float var2 = 12.0; // stored in memory location 02000 0220 int foo1; float foo2; int* ptr1; float* ptr2; // define a pointer to a variable of type int // define a pointer to a variable of type float ptr1 = &var1; // set ptr1 to 02000 1010 ptr2 = &var2; // set ptr2 to 02000 0220 foo1 = *ptr1; // set foo1 to 5 foo2 = *ptr2; // set foo2 to 12.0 29 tj
Pointers and functions Pointers allow us to use called functions to change values in the calling function Instead of passing variables in the parameter list (remember copies are made and then destroyed) we can pass pointers Pointers allow us to modify the passed variables by memory reference 30 tj
Pointers and functions Declaration Indicate that a pointer is being passed in the Formal Parameter List void update_acct(float* balance_ptr, float int_rate); 31 tj
Pointers and functions Definition Indicate that a pointer is being passed in the Formal Parameter List Operate on the variables pointed to by the pointers via the dereference operator void update_acct(float* balance_ptr, float int_rate){ *balance_ptr += *balance_ptr * int_rate; return; } 32 tj
Pointers and functions Usage Pass a pointer variable in the Actual Parameter List Pass the address to the variable in the Actual Parameter List int main(void){ float checking; float savings; float int_rate; float* check_ptr = &checking; update_acct(check_ptr, int_rate); update_acct(&savings, int_rate); return 0; } // ptr variable to a float variable 33 tj