ECEN 449 Microprocessor System Design. Review of C Programming

Similar documents
ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

ECE 650 Systems Programming & Engineering. Spring 2018

Operating systems. Lecture 9

Programming refresher and intro to C programming

Shared Memory Memory mapped files

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

PRINCIPLES OF OPERATING SYSTEMS

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

C introduction: part 1

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Final CSE 131B Spring 2004

Lecture 03 Bits, Bytes and Data Types

Lectures 5-6: Introduction to C

Lectures 5-6: Introduction to C

EC 413 Computer Organization

Page Which had internal designation P5

Lab 09 - Virtual Memory

Computer Systems Lecture 9

CS 61C: Great Ideas in Computer Architecture Introduction to C

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

P6 memory system P6/Linux Memory System October 31, Overview of P6 address translation. Review of abbreviations. Topics. Symbols: ...

A brief introduction to C programming for Java programmers

Topic 6: A Quick Intro To C

MMAP AND PIPE. UNIX Programming 2015 Fall by Euiseong Seo

OPERATING SYSTEMS: Lesson 2: Operating System Services

CSCI 171 Chapter Outlines

Technical Questions. Q 1) What are the key features in C programming language?

Lecture 3: C Programm

MPATE-GE 2618: C Programming for Music Technology. Syllabus

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

CMPSC 311 Exam 2. March 27, 2015

Goals of this Lecture

I/O Management! Goals of this Lecture!

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

I/O Management! Goals of this Lecture!

Intel P The course that gives CMU its Zip! P6/Linux Memory System November 1, P6 memory system. Review of abbreviations

CS C Primer. Tyler Szepesi. January 16, 2013

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

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Section 3: File I/O, JSON, Generics. Meghan Cowan

OBJECT ORIENTED PROGRAMMING

CSE 333 SECTION 3. POSIX I/O Functions

Intel P6 (Bob Colwell s Chip, CMU Alumni) The course that gives CMU its Zip! Memory System Case Studies November 7, 2007.

EL2310 Scientific Programming

TELE402. Internetworking. TELE402 Lecture 1 Protocol Layering 1

Advanced Pointer & Data Storage

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

POINTER AND ARRAY SUNU WIBIRAMA

BLM2031 Structured Programming. Zeyneb KURT

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

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Kurt Schmidt. October 30, 2018

Memory System Case Studies Oct. 13, 2008

Assist. Prof. Dr. Caner ÖZCAN

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

Computers Programming Course 6. Iulian Năstac

Pentium/Linux Memory System March 17, 2005

F28HS2 Hardware-Software Interface. Lecture 3 - Programming in C 3

CSCI 4061: Virtual Memory

EL2310 Scientific Programming

14. Memory API. Operating System: Three Easy Pieces

POSIX Shared Memory. Linux/UNIX IPC Programming. Outline. Michael Kerrisk, man7.org c 2017 November 2017

C programming basics T3-1 -

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32

CS 220: Introduction to Parallel Computing. Arrays. Lecture 4

What did we talk about last time? Selection Loops Lab 3

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

Data Type Fall 2014 Jinkyu Jeong

Operating systems. Lecture 7


Programming in C. What is C?... What is C?

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

EL6483: Brief Overview of C Programming Language

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Course Outline Introduction to C-Programming

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

CSE 333 SECTION 3. POSIX I/O Functions

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

Computational Methods of Scientific Programming Lecture 8. Today s lecture Start C/C++ Basic language features

Introduction to C. Systems Programming Concepts

A Quick Introduction to C Programming

More on C programming

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE

CS240: Programming in C

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

CSE 303 Lecture 8. Intro to C programming

Introduction to File Systems. CSE 120 Winter 2001

Introduction to C Final Review Chapters 1-6 & 13

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

CS111: PROGRAMMING LANGUAGE II

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

Final CSE 131B Spring 2005

Unit 7. Functions. Need of User Defined Functions

Transcription:

ECEN 449 Microprocessor System Design Review of C Programming 1 Objectives of this Lecture Unit Review C programming basics Refresh es programming g skills s 2 1

Basic C program structure # include <stdio.h> main() { printf ( Hello world\n ); } Compilation of a program: gcc o hello hello.c --- produces hello as the executable file --o specifies the name of the executable file Other flags: Wall turn on all warnings, useful for debugging purposes o2 turn optimization on ansi use ANSI C 3 Basic Program Structure C programs consist of at least one main() function Can have other functions as needed Functions can return values or return nothing void function1 (int arg1, float arg2) Takes an integer as first argument, float as second input Returns no output void function1 (arg1, arg2) int arg1, float arg2; Alternate description of the same function int function2 () Takes no input, but returns an integer 4 2

Libraries C employs many libraries Make it easy to use already written functions in standard d libraries stdio.h standard io used to input/output data to/from the program Other libraries such as math.h etc exist 5 Data types Int Default size integer Defined to be the natural size of the machine (with caveats) Can vary in number of bits, but now mostly 32-bits Short Short must be not be larger than an int Typically 16 bits Long Int must not be larger than a long Must be at least 32 bit integers, increasingly 64 bits Unsigned Placed before any other var type means the bits are used for positive numbers, 0 2 n - 1 Float IEEE standard 32-bit floating point number Double Double precision IEEE floating point number, 64 bits Char Used normally for 8-bit characters or numbers 6 3

Operators Assignment A = B; Makes A equal to B A-B, A+B, A*B have usual meanings A/B depends on the data types Actual division if A is float or double Integer division if A and B are integers (ignore the remainder) A%B = remainder of the division (A == B) Compares A and B and returns true (1) if A is equal to B else false (0) A++ --increment A i.e., A = A+1; A-- decrement A i.e., A = A 1; 7 Local vs. Global variables --Scope Declared variables have scope Scope defines where the declared variables are visible Global variables Defined outside any function Generally placed at the top of the file after include statements Visible everywhere Any function can read & modify them Local variables Visible only within the functions where declared Allows more control 8 4

Global vs. Local variables Safer to use local variables Easier to figure out who is modifying them and debug when problems arise Pass information to other functions through values when calling other functions function1 () { int arg1, arg2; /* local to function1 */ float arg3; /* local to function1 */ arg3 = function2 (arg1, arg2); } 9 Global vs. local variables int glob_var1, glob_var2; /* global variables, declared out of scope for all functions */ main () { glob_var1 = 10; glob_var2 = 20;. } function1() { glob_var1 = 30; } 10 5

define # define REDDY-PHONE-NUM 8457598 Just like a variable, but can t be changed Visible to everyone # define SUM(A, B) A+B Where ever SUM is used it is replaced with + For example SUM(3,4) = 3+4 Can be easier to read Macro function This is different from a function call #define string1 Aggies Rule 11 Loops for (i=0; i< 100; i++) a[i] = i; Another way of doing the same thing: i =0; while (i <100) { a[i] = i; i++; } 12 6

Arrays & Pointers int A[100]; Declares A as an array of 100 integers from A[0], A[1] A[99]. If A[0] is at location 4000 in memory, A[1] is at 4004, A[2] is at 4008 and so on When int is 4 bytes long Data is stored consecutively in memory This gives an alternate way of accessing arrays through pointers Pointer is the address where a variable is located &A[0] indicates the address of A[0] 13 Arrays and pointers int *A; This declares that A is a pointer and an integer can be accessed through this pointer There is no space allocated at this pointer i.e., no memory for the array. A = (int *) malloc(100 * sizeof(int)); malloc allocates memory space of 100 locations, each the size that can hold an int. Just to be safe, we cast the pointer to be of integer type by using (int *) Now *A gives the first value of the array *(A+4) gives the second value so on 14 7

Pointers Pointers are very general They can point to integers, floats, chars, arrays, structures, functions etc.. Use the following to be safe: int *A, *B; float *C; A = malloc (100 * sizeof(int)); B = (int *) (A + 1); /* B points to A[1] */ Casting (int *) is safer even though not needed here C = (float *) A[1]; /* what does this say? */ 15 Structures A lot of times we want to group a number of things together. Use struct.. Say we want a record of each student for this class Struct student { string name[30]; int uin; int score_hw[4]; int score_exam[2]; exam[2]; float score_lab[5]; }; Struct student 449_students[50]; Allows you to define your own datatype 16 8

Structures 449_students[0] is the structure holding the first students information. Individual elements can be accessed by 449_students[0].name, 449_students[0].uin etc.. If you have a pointer to the structure use 449_studentsp->name, 449_studentsp->uin etc 17 I/O getchar() putchar() FILE *fptr; fptr = fopen(filename, w); for (i= 0; i< 100; i++) fprintf(fptr, %d\n, i); fclose(fptr); fscanf(fptr, (p %d\n, i); 18 9

Function calls and control flow When a function is called a number of things happen Return address is saved on the stack Local variables saved on the stack Program counter loaded with called function address Function call executed On return, stack is popped to get return address, local variables Execution starts from the returned address 19 Open() Library: include <sys/types.h>; include <sys/stat.h>; include <fcntl.h>; Prototype: int open(char *Path, int Flags); Syntax: int fd; char *Path="/tmp/file"; int Flags= O_WRONLY; fd = open(path, Flags); Why two ways to do the same thing (open, fopen)? -- open: POSIX API fopen: ANSI C (better portability) 20 10

Mmap() Library: include <sys/mman.h> Syntax: void *mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off); Usage va = mmap(addr, len, prot, flags, fildes, off); Establishes a memory map of file given by fildes (file descriptor), starting from offset off, of length len, at memory address starting from va -- a method of memory-mapped file I/O 21 Mmap() prot controls read/write/execute accesses of memory PROT_READ: READ: readpermission PROT_WRITE: write permission PROT_EXECUTE: execute permission PROT_NONE: no permission Can use OR combination of these bits Flags filed controls the type of memory handling MAP_SHARED: changes are shared MAP_PRIVATE: changes are not shared (the initial write reference will create a private copy of the memory) 22 11