Chapter 11 - The C Language. BYU CS/ECEn 124Chapter 9 - Interrupts2 Dennis Ritchie (1940-2011) Dennis Ritchie, the software developer who brought the.

Slides:



Advertisements
Similar presentations
Chapter 11 Introduction to Programming in C
Advertisements

CSE 105 Structured Programming Language (C)
Agenda Definitions Evolution of Programming Languages and Personal Computers The C Language.
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Chapter 11 - The C Language. BYU CS/ECEn 124The C Language2 Topics to Cover… ISR’s High Level Languages Compilers vs. Interpreters The C Language 1 st.
Introduction to C Programming CE Lecture 1 Introduction to C.
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
Unix Continuum of Tools Do something once: use the command line Do something many times: –Use an alias –Use a shell script Do something that is complex.
1 Lecture 1  Getting ready to program  Hardware Model  Software Model  Programming Languages  The C Language  Software Engineering  Programming.
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Chapter 2: Impact of Machine Architectures What is the Relationship Between Programs, Programming Languages, and Computers.
CS100A, Fall 1997, Lectures 221 CS100A, Fall 1997 Lecture 22, Tuesday 18 November Introduction To C Goal: Acquire a reading knowledge of basic C. Concepts:
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
Guide To UNIX Using Linux Third Edition
C programming Language and Data Structure For DIT Students.
CHAPTER 1: INTORDUCTION TO C LANGUAGE
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
S07: The C Language Required:PM: Ch 6, pgs PM: Ch 8.4, pgs Recommended: K&R, Chapters 1-4C program C++ Compiler 6.5Variables Global VariablesExpressions.
Computer Science 210 Computer Organization Introduction to C.
“C” Programming Language CIS 218. Description C is a procedural languages designed to provide lowlevel access to computer system resources, provide language.
CS102 Introduction to Computer Programming
“C” Programming Language What is language ? Language is medium of communication. If two persons want to communicate with each other, they have to use.
CS 355 – Programming Languages
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
Summer 2014 Chapter 1: Basic Concepts. Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, Chapter Overview Welcome to Assembly Language.
Assembly Language for x86 Processors 7th Edition
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming 1.
1 Computing Software. Programming Style Programs that are not documented internally, while they may do what is requested, can be difficult to understand.
The C Language.
Programming With C.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages.
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
Copyright © 2007 Addison-Wesley. All rights reserved.1-1 Reasons for Studying Concepts of Programming Languages Increased ability to express ideas Improved.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
C Language: Introduction
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
1 Text Reference: Warford. 2 Computer Architecture: The design of those aspects of a computer which are visible to the programmer. Architecture Organization.
Overview of Previous Lesson(s) Over View  A program must be translated into a form in which it can be executed by a computer.  The software systems.
Programming in C++ Dale/Weems/Headington Chapter 1 Overview of Programming and Problem Solving.
Agenda Computer Languages How to Write a Simple C Program
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
CMPE13Cyrus Bazeghi 1 Chapter 11 Introduction to Programming in C.
Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.
An overview of C Language. Overview of C C language is a general purpose and structured programming language developed by 'Dennis Ritchie' at AT &T's.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
Hello world !!! ASCII representation of hello.c.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
小型系統 心情 vs. 古典樂 心情 vs. 古典樂 浪漫求籤系統 美食導航系統 季潔亭雅鈺熒岱芸 美食導航系統 楊氏音樂模擬大會考人瑋 若維 芷萱 伽倩 楊氏音樂模擬大會考 麥當勞熱量計算系統 火星文困擾你嗎 ? 火星文困擾你嗎 ? 歌詞知多少 - 挑戰你的腦容量英琪 日馨 青雪 鈺娟.
Introduction to Computer Programming Concepts M. Uyguroğlu R. Uyguroğlu.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
INTRODUCTION TO PROGRAMING System Development Mansoura October 2015.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 1.
Computer Science 210 Computer Organization
CSCI-235 Micro-Computer Applications
C Programming Hardik H. Maheta.
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Introduction to C Topics Compilation Using the gcc Compiler
C Basics.
Introduction to C Programming Language
Computer Science 210 Computer Organization
Chapter 11 Introduction to Programming in C
Chapter 11 Introduction to Programming in C
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Chapter 11 Introduction to Programming in C
Introduction to Computer Programming
C programming Language
An Overview of C.
Chapter 11 Introduction to Programming in C
Presentation transcript:

Chapter 11 - The C Language

BYU CS/ECEn 124Chapter 9 - Interrupts2 Dennis Ritchie ( ) Dennis Ritchie, the software developer who brought the world the C programming language and Unix operating system, has died at the age of 70. Ritchie (known by the username "dmr") was part of a dynamic software development duo with Ken Thompson at Bell Labs,, which they joined in 1967 and 1966, respectively. Ritchie created the C programming language, which replaced the B programming language Thompson invented. Two years later in 1969, they created Unix, initially designed for minicomputers. Unix was initially written in 1969 in assembly language and later in C. Unix went on to become key software for critical computing infrastructure around the world. “UNIX is very simple, it just needs a genius to understand its simplicity.” --Dennis Ritchie

BYU CS/ECEn 124The C Language3 Topics to Cover… High Level Languages Compilers vs. Interpreters The C Language 1 st C Program C Style C Preprocessor printf Function RBX430-1 Header Files 2 nd C Program

BYU CS/ECEn 124The C Language4 Levels of Abstraction Problems Algorithms Language Machine (ISA) Architecture Microarchitecture Circuits Devices Transistors Logic gates, multiplexers, memory, etc. MSP430 Architecture Machine code Assembly code High Level Languages

BYU CS/ECEn 124The C Language5 High Level Languages The closer a language is to your original specification, the easier the program is to write. Many, many programming languages LISP - LISt Processing PROLOG - logic programming MATLAB - matrix and vector manipulations BASIC – interpreter for small computers APL – matrix and vectors FORTRAN – formula translation COBOL – business and accounting PASCAL – procedural Ada – DOD large systems Java – Internet C, C++ …. High Level Languages

BYU CS/ECEn 124The C Language6 High Level Languages Allow us to use symbolic names for values Programmer simply assigns each value a name Allow us to ignore many memory details, the compiler takes care of … register usage variable allocation loads and stores from memory callee/caller protocol stack management for subroutine calls Provide abstraction of underlying hardware Hide low level details (ISA) from programmer Uniform interface (not tied to ISA) to program Portable software (works on different ISAs) The compiler generates the machine code High Level Languages numberOfDays = 30; myCurrentPayPerHour = 10.75; switch_A = ON; numberOfDays = 30; myCurrentPayPerHour = 10.75; switch_A = ON;

BYU CS/ECEn 124The C Language7 High Level Languages Provide expressiveness Human-friendly orientation Express complex tasks with smaller amount of code English-like and human constructs if-then-else… while… for... Enhance code readability Can read like a novel… Readability.. is very important life cycle costs are more important than initial programming costs Easier to debug Easier to maintain High Level Languages if(isCloudy) get(umbrella); else get(sunglasses); if(isCloudy) get(umbrella); else get(sunglasses); main() { readInput(); checkForErrors(); doCalculation(); writeOutput(); } main() { readInput(); checkForErrors(); doCalculation(); writeOutput(); }

BYU CS/ECEn 124The C Language8 High Level Languages Provide safeguards against bugs Rules can lead to well-formed programs structured programming (no GOTO statements) Compilers can generate checks array bounds checking data type checking Many languages provide explicit support for assertions something that should be true - if it isn’t, then error High Level Languages assert(accountBalance >= 0); High-level languages make complex programming simpler, while low-level languages tend to produce more efficient code However, well-designed compilers frequently produce code comparable in efficiency to what most low-level programmers can produce by hand with better overall results

BYU CS/ECEn 124The C Language9 Execution Models Interpreted Interpreted languages are read and then executed directly, with no compilation stage. A program called an interpreter reads the program line by line and executes the lines as they are read. Compiled Compiled languages are transformed into an executable form before running. There are two types of compilation: Machine code generation Intermediate representations Translated A language may be translated into a low-level programming language for which native code compilers are already widely available. The C programming language is a common target for such translators. Execution Models

BYU CS/ECEn 124The C Language10 Compilers vs Interpreters temp=v[i]; v[i]=v[i+1]; v[i+1]=temp; High-level language statements Compiler MOV.B 0x0001(SP),R14 MOV.W SP,R15 INCD.W R15 ADD.W R15,R14 MOV.B 0x0001(SP),R14 INC.W R14 Assembly Assembler 415E F 532F 5F0E 4EE E E Object code Application = Executable = Data Path temp=v[i]; v[i]=v[i+1]; v[i+1]=temp; Source code Interpreter Compilers vs Interpreters

BYU CS/ECEn 124The C Language11 Compilation Compilers convert high-level code to machine code compile once, execute many times resulting machine code is optimized may include intermediate step (assembly) slower translation, but higher performance when executed If the compiled program can run on a computer whose CPU is different from the one on which the compiler runs, the compiler is known as a cross-compiler. A program that translates from a low level language to a higher level one is a de-compiler. Is an assembler considered a compiler? assemblers do convert higher level code to machine code, but… they are usually in a class by themselves Compilers

BYU CS/ECEn 124The C Language12 The C Programming Language Developed between 1969 and 1973 by Dennis Ritchie at Bell Labs. C first developed for use in writing compilers and operating systems (UNIX). A low-level high-level language Many variants of C 1989, the American National Standards Institute standardized C (ANSI C, most commonly used C) “The C Programming Language” by Kernighan and Ritchie is the C “Bible” (Also called the “White Book”.) C is one of the most popular programming languages of all time – very few computer architectures exist for which there is no C. C is predecessor to most of today’s procedural languages such as C++ and Java. The C Language

BYU CS/ECEn 124The C Language13 The C Programming Language Developed between 1969 and 1973 by Dennis Ritchie at Bell Labs. C first developed for use in writing compilers and operating systems (UNIX). A low-level high-level language Many variants of C 1989, the American National Standards Institute standardized C (ANSI C, most commonly used C) “The C Programming Language” by Kernighan and Ritchie is the C “Bible” (Also called the “White Book”.) C is one of the most popular programming languages of all time – very few computer architectures exist for which there is no C. The C Language

More Comments About C BYU CS/ECEn 124The C Language14 “C is a terse and unforgiving abstraction of silicon.” Although C was designed for implementing system software, C remains without rival in programming embedded systems. Learning C imparts a deep understanding of the dominant von Neumann architecture in a way that no other language can. C is the predecessor to most of today’s procedural languages such as C++ and Java. Since poor C programming plays in the prevalence of the buffer overflow security vulnerabilities, it is critical that programmers learn how to program C properly. The C Language

BYU CS/ECEn 124The C Language15 Compiling a C Program The C Language Object Code Assembler Code C/C++ Code Machine Code

BYU CS/ECEn 124The C Language16 Compiling a C Program C Source Code C Preprocessor Library & Object Files Executable Image C Compiler The C Language Preprocessed source code Source Code Analysis 1 st Pass Symbol Table Code Generation 2 nd Pass Linker Object module

BYU CS/ECEn 124The C Language17 A First Program //************************************ // blinky.c: Software Toggle P1.0 //************************************ #include "msp430x22x4.h" void main(void) { int i = 0; WDTCTL = WDTPW + WDTHOLD; // stop watchdog P4DIR |= 0x40; // P4.6 output for (;;) // loop { P4OUT ^= 0x40; // toggle P4.6 while (--i); // delay } Tells compiler to use all the definitions found in the msp430x22x4.h library. A.h file is called a header file and contains definitions and declarations. All programs must have a main() routine. This one takes no arguments (parameters). Set P4.6 as output Loop forever Toggle P4.6 Delay 65,536 1 st C Program Stop WD w/Password

BYU CS/ECEn 124The C Language18 Comments Use lots of comments /* This is a comment */ // This is a single line comment Comment each procedure telling: /* * * ProcedureName – what it does * * Parameters: * * Param1 – what param1 is * * Param2 – what param2 is * * Returns: * * What is returned, if anything * * */ Use lots of white space (blank lines) C Style

BYU CS/ECEn 124The C Language19 Indenting Style Each new scope is indented 2 spaces from previous Put { on end of previous line, or start of next line Line matching } up below Style is something of a personal matter. Everyone has their own opinions… What is presented here is similar to that in common use and a good place to start... if(a < b) { b = a; a = 0; } else { a = b; b = 0; } if(a < b) { b = a; a = 0; } else { a = b; b = 0; } Style 1 if(a < b) { b = a; a = 0; } else { a = b; b = 0; } if(a < b) { b = a; a = 0; } else { a = b; b = 0; } Style 2 C Style

BYU CS/ECEn 124The C Language20 More On Indenting Style For very long clauses, you may want to add a comment to show what the brace is for: if(a < b) { /* Lots of code here... */ } // end if(a < b) else { /* Lots of code here... */ } // end else if(a < b) { /* Lots of code here... */ } // end if(a < b) else { /* Lots of code here... */ } // end else C Style

BYU CS/ECEn 124The C Language21 The C Preprocessor #define symbol code The preprocessor replaces symbol with code everywhere it appears in the program below #define NUMBER_OF_MONKEYS 259 #define MAX_LENGTH 80 #define PI #include filename.h The preprocessor replaces the #include directive itself with the contents of header file filename.h #include /* a system header file */ #include "myheader.h" /* a user header file */ Macros Pass arguments #define add(x,y) x+y #define concatenate(x,y) x##y C Preprocessor

C Header Files

BYU CS/ECEn 124The C Language23 RBX430-1 System Functions RBX430-1.h and RBX430-1.c uint8 RBX430_init(enum _430clock clock); // init board void ERROR2 (int error); // fatal error Setting system clock RBX430-1 Header Files #include "msp430x22x4.h" enum _430clock {_16MHZ, _12MHZ, _8MHZ, _1MHZ}; #define myClock _8MHZ #define SMCLK // SMCLK = ~8 mhz void main(void) { RBX430_init(myClock); // init board ERROR2(5); }

Peripheral I/O in C

BYU CS/ECEn 124The C Language25 Switches on Port 1 C Peripheral I/O MSP430F2274

BYU CS/ECEn 124The C Language26 Speaker on P4.5 (TB2) MSP430F2274 C Peripheral I/O

BYU CS/ECEn 124The C Language27 LEDs on Ports 3 & 4 MSP430F2274 C Peripheral I/O

Stream I/O in C

BYU CS/ECEn 124The C Language29 C I/O I/O facilities are not part of the C language itself Nonetheless, programs that do not interact with their environment are useless Most digital I/O handled directly by C program #include "msp430x22x4.h" SPR’s, Ports, A/D, transponder, switches, LED’s, etc The ANSI standard defines a set of I/O library functions for portability Programs that confine their system interactions to facilities provided by the standard library can be moved from one system to another without change. The properties of the C I/O library functions are specified in header files (C standard library) “RBX430-1.h", “RBX430_lcd.h" C Stream I/O

BYU CS/ECEn 124The C Language30 I/O Data Streams All C character based I/O is performed on streams. In standard C there are 3 streams automatically opened upon program execution: stdin is the input stream stdout is the output stream stderr stream for error messages The printf function outputs formatted values to the stdout stream printf( "format string...", parameters... ); The format string contains two object types: Ordinary characters that are copied to the output stream Conversion specifications which cause conversion and printing of the next argument in the argument list. C Stream I/O

BYU CS/ECEn 124The C Language31 Output in C printf( format_string, parameters ) printf("Hello World"); printf("%d plus %d is %d", x, y, x+y); printf("In hex it is %x", x+y); printf("Hello, I am %s. ", myname); printf("In ascii, 65 is %c. ", 65); Output: Hello world 5 plus 6 is 11 In hex it is b Hello, I am Bambi. In ascii, 65 is A. String literal Decimal Integer Hex Integer String Character Newline C Stream I/O

BYU CS/ECEn 124The C Language32 LCD on Ports 2,3, & 4 MSP430F2274 C Stream I/O

BYU CS/ECEn 124The C Language33 RBX430_lcd.h Prototypes uint8 lcd_init(void); void lcd_clear(uint8 value); void lcd_backlight(uint8 backlight); void lcd_volume(uint8 volume); uint16 lcd_display(int16 mode); uint8 lcd_cursor(uint16 x, uint16 y); void lcd_printf(const char* fmt,...); uint8 lcd_image(const uint8* image, uint16 x, uint16 y); uint8 lcd_blank(uint16 x, uint16 y, uint16 w, uint16 h); uint8 lcd_point(uint16 x, uint16 y, uint8 flag); void lcd_circle(uint16 x, uint16 y, uint16 r, uint8 pen); void lcd_square(uint16 x, uint16 y, uint16 s, uint8 pen); void lcd_rectangle(uint16 x0, uint16 y0, uint16 x1, uint16 y1, uint8 pen); C Stream I/O

BYU CS/ECEn 124The C Language34 LCD – 160 x 160 x 5 Pixels Y (0-159)  Hello World! // 5 x 8 pixel Characters lcd_cursor(40, 60); lcd_printf("Hello World!"); X (0-159)  C Stream I/O

BYU CS/ECEn 124The C Language35 Quiz 11.1 Pair up Person “A” explain C I/O to Person “B” Person “B” explain (using different terms) C I/O to Person “A” Write a C program to Initialize the RBX430-1 board to 8 mHz Initialize the lcd Write the word “Success” in the middle of the display #include "msp430x22x4.h" #include "eZ430X.h" #include "lcd.h" int main(void) { WDTCTL = WDTPW + WDTHOLD;// Stop WDT RBX430_init(_8MHZ);// init board lcd_init();// init the lcd lcd_cursor(80, 80);// position to middle of display lcd_printf("Success"); }

BYU CS/ECEn 124The C Language36 // File: f_to_c.c // Date: 02/15/2010 // Author: Joe Coder // Description: Output a table of Fahrenheit and Celsius temperatures. #include "msp430x22x4.h" #include "eZ430X.h" #include "lcd.h" #define LOW 0 // Starting temperature #define HIGH 100 // Ending temperature #define STEP 10// increment int main(void) { int fahrenheit; // Temperature in fahrenheit float celsius; // Temperature in celsius WDTCTL = WDTPW + WDTHOLD;// Stop WDT eZ430X_init(_1MHZ);// init board lcd_init(); // Loop through all the temperatures, printing the table for(fahrenheit = LOW; fahrenheit <= HIGH; fahrenheit += STEP) { celsius = (fahrenheit - 32) / 1.8; lcd_printf("\nf=%d, c=%.1f", fahrenheit, celsius); } Use #define’s for magic numbers A Second Program 1 digit to the right of the decimal point. #include the lcd functions Use meaningful names for variables 2 nd C Program Quiet the dog and init the system, lcd

BYU CS/ECEn 124The C Language37