Program Flow. Instructions and Memory. Why are these 16 bits? C code. Memory. a = b + c. Machine Code. Memory. Assembly Code.

Similar documents
Function I/O. Function Input and Output. Input through actual parameters. Output through return value. Input/Output through side effects

Types. C Types. Floating Point. Derived. fractional part. no fractional part. Boolean Character Integer Real Imaginary Complex

Function I/O. Last Updated 10/30/18

Expressions and Precedence. Last updated 12/10/18

Types, Variables, and Constants

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Type Conversion. and. Statements

Variation of Pointers

Embedded Controller Programming 2

a data type is Types

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Review of the C Programming Language for Principles of Operating Systems

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Infix to Postfix Conversion

A flow chart is a graphical or symbolic representation of a process.

Introduction to Programming Using Java (98-388)

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Midterm CSE 131B Spring 2005

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

Computers Programming Course 6. Iulian Năstac

Variables and Operators 2/20/01 Lecture #

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Operators in C. Staff Incharge: S.Sasirekha

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

CS 61c: Great Ideas in Computer Architecture

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Review of the C Programming Language

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

ECE 15B COMPUTER ORGANIZATION

Tail Recursion: Factorial. Begin at the beginning. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial

Informatics Ingeniería en Electrónica y Automática Industrial

Programming Languages

Side note: Tail Recursion. Begin at the beginning. Side note: Tail Recursion. Base Types. Base Type: int. Base Type: int

INTRODUCTION TO COMPUTER SCIENCE - LAB

DECLARAING AND INITIALIZING POINTERS

Object-Oriented Programming. Topic 2: Fundamental Programming Structures in Java

Fundamentals of Programming Session 20

Variables and literals

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

Lab 3. Pointers Programming Lab (Using C) XU Silei

.Net Technologies. Components of.net Framework

Final CSE 131B Spring 2004

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

Dynamic Data Structures. CSCI 112: Programming in C

Operators & Expressions

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

UNIT- 3 Introduction to C++

CMPE-013/L. Introduction to C Programming

First of all, it is a variable, just like other variables you studied

Programming for Engineers Iteration

Computational Physics Operating systems

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

Final CSE 131B Spring 2005

CSCI 171 Chapter Outlines

Programming Languages

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Intermediate Programming, Spring 2017*

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

M1-R4: Programing and Problem Solving using C (JULY 2018)

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

CSC 1214: Object-Oriented Programming

Programming Fundamentals - A Modular Structured Approach using C++ By: Kenneth Leroy Busbee

Chapter 12 Variables and Operators

Computer Hardware Engineering

CS 11 C track: lecture 5

Introduction to Computer Science Midterm 3 Fall, Points

Fall, 2015 Prof. Jungkeun Park

Operators in java Operator operands.

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

CS61C : Machine Structures

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

EE 382 Style Guide. March 2, 2018

Final CSE 131 Winter 2010

Information Science 1

Unit-2 (Operators) ANAND KR.SRIVASTAVA

SECTION II: LANGUAGE BASICS

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

C-LANGUAGE CURRICULAM

UNIVERSITY OF LIMERICK OLLSCOIL LUIMNIGH COLLEGE OF INFORMATICS & ELECTRONICS DEPARTMENT OF ELECTRONIC & COMPUTER ENGINEERING

Introduction to C. Systems Programming Concepts

Variables and Bindings

CS61C Midterm Review on C & Memory Management

Pointers, Dynamic Data, and Reference Types

Chapter 4 Homework Individual/Team (1-2 Persons) Assignment 15 Points

CSI33 Data Structures

DECISION MAKING STATEMENTS

Structures, Unions Alignment, Padding, Bit Fields Access, Initialization Compound Literals Opaque Structures Summary. Structures

POINTER & REFERENCE VARIABLES

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Transcription:

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