CS 61C: Great Ideas in Computer Architecture Introduc=on to C, Part I. We are here!

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

CS 110 Computer Architecture. Lecture 2: Introduction to C, Part I. Instructor: Sören Schwertfeger.

CS 61C: Great Ideas in Computer Architecture Lecture 2: Introduction to C, Part I

CS 61C: Great Ideas in Computer Architecture (Machine Structures) Introduc)on to C (Part I) We are here!

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

CS 110 Computer Architecture. Lecture 2: Introduction to C, Part I. Instructor: Sören Schwertfeger.

CS 61C: Great Ideas in Computer Architecture Lecture 2: Introduction to C, Part I

Number Representation & I See The C

CS 102 Computer Architecture. Lecture 2: Introduction to C, Part I. Instructor: Sören Schwertfeger.

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

CS 61C: Great Ideas in Computer Architecture Lecture 2: Introduc)on to C, Part I

CS 61c: Great Ideas in Computer Architecture

C: Introduction, Pointers Instructors: Steven Ho, Nick Riasanovsky

Instructor: Randy H. Katz hbp://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #3. Agenda

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

CS 61C: Great Ideas in Computer Architecture Strings and Func.ons. Anything can be represented as a number, i.e., data or instruc\ons

CS61C : Machine Structures

Instructor: Randy H. Katz hap://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #7. Warehouse Scale Computer

CS61C : Machine Structures

CS61C : Machine Structures

Agenda. Address vs. Value Consider memory to be a single huge array. Review. Pointer Syntax. Pointers 9/9/12

CS61C : Machine Structures

CS 61C: Great Ideas in Computer Architecture Func%ons and Numbers

Number review... Lecture 3 Introduction to the C Programming Language (pt 1) Has there been an update to ANSI C?

Processor. Lecture #2 Number Rep & Intro to C classic components of all computers Control Datapath Memory Input Output

CS61C : Machine Structures

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 3. Aykut Erdem, Erkut Erdem, Fuat Akal

Processor. Lecture #2 Number Rep & Intro to C classic components of all computers Control Datapath Memory Input Output }

Instructors: Randy H. Katz David A. PaHerson hhp://inst.eecs.berkeley.edu/~cs61c/fa10. Fall Lecture #1. Agenda

Agenda. Old School CS61c. Agenda. New School CS61c 8/26/10. CS 61C: Great Ideas in Computer Architecture (Machine Structures)

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

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

C Programming Primer 8/31/15 1

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Objec+ves. Review. Basics of Java Syntax Java fundamentals. What are quali+es of good sooware? What is Java? How do you compile a Java program?

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

CS 61c: Great Ideas in Computer Architecture

Introduction to C Language

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

Welcome to CS 449: Introduc3on to System So6ware. Instructor: Wonsun Ahn

CS 61C: Great Ideas in Computer Architecture (Machine Structures) More MIPS Machine Language

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

Instructor: Randy H. Katz hbp://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #4. Agenda

Topics Introduction to Microprocessors

Lecture 10: Potpourri: Enum / struct / union Advanced Unix #include function pointers

Lectures 5-6: Introduction to C

CS 61C: Great Ideas in Computer Architecture Compilers and Floa-ng Point. Today s. Lecture

Fall, $ cat welcome.c #include <stdio.h>

History of Java. Java was originally developed by Sun Microsystems star:ng in This language was ini:ally called Oak Renamed Java in 1995

9/9/12. New- School Machine Structures (It s a bit more complicated!) CS 61C: Great Ideas in Computer Architecture IntroducMon to Machine Language

CS Programming In C

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

CS133 C Programming. Instructor: Jialiang Lu Office: Information Center 703

just a ((somewhat) safer) dialect.

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

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

CS 61C: Great Ideas in Computer Architecture (Machine Structures) Introduc)on to Machine Language

edunepal_info

Programming. Data Structure

Lectures 5-6: Introduction to C

Outline. Compiling process Linking libraries Common compiling op2ons Automa2ng the process

EC 413 Computer Organization

CS 61C: Great Ideas in Computer Architecture Direct- Mapped Caches. Increasing distance from processor, decreasing speed.

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

CS 113: Introduction to

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

CSC231 C Tutorial Fall 2018 Introduction to C

CMPE-013/L. Introduction to C Programming

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

IT 252 Computer Organization and Architecture Introduction to the C Programming Language

PRINCIPLES OF OPERATING SYSTEMS

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

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

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

Introduction to Computing Lecture 01: Introduction to C

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

A Short Course for REU Students Summer Instructor: Ben Ransford

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

CSE 303 Lecture 8. Intro to C programming

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

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

Basic C Program: Print to stdout. Basic C Program. Basic C Program: Print to stdout. Header Files. Read argument and print. Read argument and print

Programming in C week 1 meeting Tiina Niklander

Final CSE 131B Spring 2004

Brought to you by CalLUG (UC Berkeley GNU/Linux User Group). Tuesday, September 20, 6-8 PM in 100 GPB.

Lecture #6 Intro MIPS; Load & Store Assembly Variables: Registers (1/4) Review. Unlike HLL like C or Java, assembly cannot use variables

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

EE 209: Programming Structures for Electrical Engineering

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

Programming in C First meeting

Presented By : Gaurav Juneja

CS61, Fall 2012 Section 2 Notes

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

CS 11 java track: lecture 1

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

Programming in C UVic SEng 265

Algorithms, Data Structures, and Problem Solving

Transcription:

Agenda CS 61C: Great Ideas in Computer Architecture Introduc=on to C, Part I Instructors: Krste Asanovic Randy H. Katz hgp://inst.eecs.berkeley.edu/~cs61c/f12 Review Compile vs. Interpret Python vs. Java vs. C Administrivia Quick Start Introduc=on to C Technology Break More C Summary 8/30/12 Fall 2012 - - Lecture #4 1 8/30/12 Fall 2012 - - Lecture #4 2 Review Request- Level Parallelism High request volume, each largely independent of other Use replica=on for beger request throughput, availability MapReduce Data Parallelism Map: Divide large data set into pieces for independent parallel processing Reduce: Combine and process intermediate results to obtain final result 8/30/12 Fall 2012 - - Lecture #4 3 New- School Machine Structures (It s a bit more complicated!) Parallel Requests Assigned to computer e.g., Search Katz Parallel Threads Assigned to core e.g., Lookup, Ads So#ware Parallel Instruc=ons >1 instruc=on @ one =me e.g., 5 pipelined instruc=ons Parallel Data >1 data item @ one =me e.g., Add of 4 pairs of words Hardware descrip=ons All gates @ one =me Programming Languages Harness Parallelism & Achieve High Performance Hardware Warehouse Scale Computer Core Memory Input/Output Instruc=on Unit(s) Cache Memory Core Smart Phone Today s Logic Gates 8/30/12 Lecture Fall 2012 - - Lecture #4 4 Computer (Cache) Core Func=onal Unit(s) A 0 +B 0 A 1 +B 1 A 2 +B 2 A 3 +B 3 Big Idea #1: Levels of Representa=on/ Interpreta=on Machine Interpreta4on High- Level Language Program (e.g., C) Compiler Assembly Language Program (e.g., MIPS) Assembler Machine Language Program (MIPS) Hardware Architecture DescripDon (e.g., block diagrams) Architecture Implementa4on temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; lw $t0, 0($2) lw $t1, 4($2) sw $t1, 0($2) sw $t0, 4($2) We are here! Anything can be represented as a number, i.e., data or instruc=ons 0000 1001 1100 0110 1010 1111 0101 1000 1010 1111 0101 1000 0000 1001 1100 0110 1100 0110 1010 1111 0101 1000 0000 1001 0101 1000 0000 1001 1100 0110 1010 1111! Logic Circuit DescripDon 8/30/12 (Circuit SchemaDc Diagrams) Fall 2012 - - Lecture #4 5 Introduc=on to C The Universal Assembly Language Some experience is required before CS61C C++ or Java OK Class pre- req included classes teaching Java Java used in two labs and one project C used for everything else 8/30/12 Fall 2012 - - Lecture #4 6 1

Flash Card Language Poll! Please raise card for first one of following you can say yes to I have programmed in C,C++,C#, or Objective-C I have programmed in Java I have programmed in FORTRAN, Cobol, Algol- 68, Ada, Pascal, or Basic 7 Disclaimer You will not learn how to fully code in C in these lectures! Can only learn a language by using it! You ll need your C reference for this course K&R is a must- have Check online for more sources Java in a Nutshell, O Reilly Chapter 2, How Java Differs from C hgp://oreilly.com/catalog/javanut/excerpt/index.html Brian Harvey s helpful transi=on notes On CS61C class website: pages 3-19 hgp://inst.eecs.berkeley.edu/~cs61c/resources/ HarveyNotesC1-3.pdf Key C concepts: Pointers, Arrays, Implica=ons for Memory management 8/30/12 Fall 2012 - - Lecture #4 8 Intro to C C is not a very high level language, nor a big one, and is not specialized to any paricular area of applicaion. But its absence of restricions and its generality make it more convenient and effecive for many tasks than supposedly more powerful languages. Kernighan and Ritchie Enabled first opera=ng system not wrigen in assembly language: UNIX - A portable OS! C and deriva=ves (C++/Obj- C/C#) s=ll one of the most popular applica=on programming languages ater >40 years! TIOBE Index of Language Popularity 8/30/12 Fall 2012 - - Lecture #4 9 8/30/12 Fall 2012 - - Lecture #4 10 Compiler Typed variables Typed funcions Header files (.h) Structs Enums Pointers Basic C Concepts Creates useable programs from C source Kind of data that a variable contains The kind of data returned from a func=on Declare func=ons and variables in a separate file Groups of related values Lists of predefined values Aliases to other variables These concepts dis=nguish C from other languages you may know 8/30/12 Fall 2012 - - Lecture #4 11 Integers: Python vs. Java vs. C Language sizeof(int) Python >=32 bits (plain ints), infinite (long ints) Java 32 bits C Depends on computer; 16 or 32 or 64 C: int should be integer type that target processor is most efficient working with Only guarantee: sizeof(long long) sizeof(long) sizeof(int) sizeof(short) All could be 64 bits 8/30/12 Fall 2012 - - Lecture #4 12 2

Type of Language Programming Unit Compilation Execution hello, world C Function Oriented Function gcc hello.c creates machine language code a.out loads and executes program C vs. Java #include<stdio.h> int main(void) printf("hello\n"); return 0; Object Oriented Java Class = Abstract Data Type javac Hello.java creates Java virtual machine language bytecode java Hello interprets bytecodes public class HelloWorld public static void main (String[] args) System.out.printl("Hello"); Storage Manual (malloc, free) Automatic (garbage collection) 8/30/12 Fall Lecture #4 2012 - - 13 From hgp://www.cs.princeton.edu/introcs/faq/c2java.html C C vs. Java Java Comments /* */! /* */ or // end of line! Constants const, #define! final! Preprocessor Yes! Variable declaration Variable naming conventions At beginning of a block sum_of_squares! No! Before you use it sumofsquares! Accessing a library #include <stdio.h>! import java.io.file;! 8/30/12 Fall 2012 - - Lecture #4 14 From hgp://www.cs.princeton.edu/introcs/faq/c2java.html Compila=on: Overview C compilers map C programs into architecture- specific machine code (string of 1s and 0s) Unlike Java, which converts to architecture- independent bytecode Unlike Python environments, which interpret the code These differ mainly in exactly when your program is converted to low- level machine instruc=ons ( levels of interpreta=on ) For C, generally a two part process of compiling.c files to.o files, then linking the.o files into executables; Assembling is also done (but is hidden, i.e., done automa=cally, by default); we ll talk about that later Compila=on: Advantages Excellent run- =me performance: generally much faster than Scheme or Java for comparable code (because it op=mizes for a given architecture) Fair compila=on =me: enhancements in compila=on procedure (Makefiles) allow only modified files to be recompiled Why C?: we can write programs that allow us to exploit underlying features of the architecture memory management, special instrucions, parallelism 8/30/12 Fall 2012 - - Lecture #4 15 8/30/12 Fall 2012 - - Lecture #4 16 Compila=on: Disadvantages Compiled files, including the executable, are architecture- specific, depending on processor type and the opera=ng system Executable must be rebuilt on each new system I.e., por=ng your code to a new architecture Change Compile Run [repeat] itera=on cycle can be slow, during the development cycle Typed Variables in C int variable1 = 2; float variable2 = 1.618; char variable3 = 'A'; Must declare the type of data a variable will hold Types can't change Type DescripDon Examples int integer numbers, including nega=ves 0, 78, - 1400 unsigned int integer numbers (no nega=ves) 0, 46, 900 float floa=ng- point numbers 0.0, 1.618, - 1.4 char single text character or symbol 'a', 'D', '? double greater precision/big FP number 10E100 long larger signed integer 6,000,000,000 8/30/12 Fall 2012 - - Lecture #4 17 8/30/12 Fall 2012 - - Lecture #4 18 3

int number_of_people () return 3; Typed Func=ons in C float dollars_and_cents () return 10.33; char first_letter () return 'A'; You have to declare the type of data you plan to return from a func=on Return type can be any C variable type, and is placed to the let of the func=on name You can also specify the return type as void Just think of this as saying that no value will be returned Also necessary to declare types for values passed into a func=on Variables and func=ons MUST be declared before they are used 8/30/12 Fall 2012 - - Lecture #4 19 Structs are structured groups of variables, e.g., typedef struct int length_in_seconds; int yearrecorded; Song; Song song1; Song1.length_in_seconds= 213; song1.yearrecorded = 1994; Song song2; Song2.length_in_seconds = 248; song2.yearrecorded = 1988; Structs in C Dot nota=on: x.y = value 8/30/12 Fall 2012 - - Lecture #4 20 Consts and Enums in C Constant is assigned a value once in the declara=on; value can't change during en=re execu=on of program const float golden_ratio = 1.618; const int days_in_week = 7; You can have a constant version of any of the standard C variable types Enums: a group of related integer constants used to parameterize libraries: enum cardsuit CLUBS,DIAMONDS,HEARTS,SPADES; Ques=on: Which statement is TRUE regarding C and Java? short, int, and long are in both languages and they have the same meaning As Java was derived from C, it has the same names of data types C programs use compilers to produce executable code but Java does not 8/30/12 Fall 2012 - - Lecture #4 21 22 Administrivia CS61c is relentless! Next week: Lab #2, HW #2 Lab #2, Amazon EC2 HW #2 will soon be posted Monday is Labor Day Holiday no lecture! Wonderful to see the valuable discussion and help going on in Piazza! Agenda Review Compile vs. Interpret Scheme vs. Java vs. C Administrivia Quick Start Introduc=on to C Technology Break More C Summary 8/30/12 Fall 2012 - - Lecture #4 23 8/30/12 Fall 2012 - - Lecture #4 25 4

A First C Program: Hello World Original C: main() printf("\nhello World\n"); ANSI Standard C: #include <stdio.h> int main(void) printf("\nhello World\n"); return (0); C Syntax: main! When C program starts, 1 st runs ini=aliza=on code to set up process for your program Then calls your procedure named main()! To get arguments to the main func=on, use: int main (int argc, char *argv[])! What does this mean? argc contains the number of strings on the command line (the executable counts as one, plus one for each argument). Here argc is 2: unix% sort myfile argv is a pointer to an array containing the arguments as strings (more on pointers later) 8/30/12 Fall 2012 - - Lecture #4 26 8/30/12 Fall 2012 - - Lecture #4 27 Example! foo hello 87! argc = 3 /* number arguments */ argv[0] = "foo", argv[1] = "hello", argv[2] = "87"! Array of pointers to strings (cover later) #include <stdio.h> #include <math.h> int main(void) int angle_degree; double angle_radian, pi, value; /* Print a header */ printf("\ncompute a table of the sine function\n\n"); A Second C Program: Compute Table of Sines /* obtain pi once for all */ /* or just use pi = M_PI, where */ /* M_PI is defined in math.h */ pi = 4.0*atan(1.0); printf("value of PI = %f \n\n", pi); printf("angle Sine \n"); angle_degree = 0; /* initial angle value */ /* scan over angle */ while (angle_degree <= 360) /* loop until angle_degree > 360 */ angle_radian = pi*angle_degree/180.0; value = sin(angle_radian); printf (" %3d %f \n ", angle_degree, value); angle_degree = angle_degree + 10; /* increment the loop index */ 8/30/12 Fall 2012 - - Lecture #4 28 8/30/12 Fall 2012 - - Lecture #4 29 Compute a table of the sine function Value of PI = 3.141593 angle Sine 0 0.000000 10 0.173648 20 0.342020 30 0.500000 40 0.642788 50 0.766044 60 0.866025 70 0.939693 80 0.984808 90 1.000000 100 0.984808 110 0.939693 120 0.866025 130 0.766044 140 0.642788 150 0.500000 160 0.342020 170 0.173648 180 0.000000 Second C Program Sample Output 190-0.173648 200-0.342020 210-0.500000 220-0.642788 230-0.766044 240-0.866025 250-0.939693 260-0.984808 270-1.000000 280-0.984808 290-0.939693 300-0.866025 310-0.766044 320-0.642788 330-0.500000 340-0.342020 350-0.173648 360-0.000000 C Syntax: Variable Declara=ons Similar to Java, but with a few minor but important differences All variable declara=ons must appear before they are used (e.g., at the beginning of the block) A variable may be ini=alized in its declara=on; if not, it holds garbage! Examples of declara=ons: Correct:!!!!! int a = 0, b = 10;!!!!!!...! Incorrect: for (int i = 0; i < 10; i++)!! 8/30/12 Fall 2012 - - Lecture #4 30 8/30/12 Fall 2012 - - Lecture #4 31 5

C Syntax : Control Flow (1/2) Within a func=on, remarkably close to Java constructs (shows Java s legacy) in terms of control flow if-else if (expression) statement if (expression) statement1 else statement2 while while (expression) statement do statement while (expression); C Syntax : Control Flow (2/2) for for (initialize; check; update) statement switch switch (expression) case const1: statements case const2: statements default: statements break 8/30/12 Fall 2012 - - Lecture #4 32 8/30/12 Fall 2012 - - Lecture #4 33 C Syntax: True or False C and Java operators nearly iden=cal What evaluates to FALSE in C? 0 (integer) NULL (a special kind of pointer: more on this later) No explicit Boolean type What evaluates to TRUE in C? Anything that isn t false is true Same idea as in Python: only 0s or empty sequences are false, anything else is true! arithme=c: +, -, *, /, % assignment: = augmented assignment: +=, - =, *=, /=, %=, &=, =, ^=, <<=, >>= bitwise logic: ~, &,, ^ bitwise shits: <<, >> boolean logic:!, &&, equality tes=ng: ==,!= subexpression grouping: ( ) order rela=ons: <, <=, >, >= increment and decrement: ++ and - - member selec=on:., - > condi=onal evalua=on:? : 8/30/12 Fall 2012 - - Lecture #4 34 8/30/12 Fall 2012 - - Lecture #4 35 And In Conclusion, All data C is an efficient compiled language, widely used for systems programming (opera=ng systems) and applica=on development C successors (C++, Objec=ve- C, C#) are also popular Java syntax based on C, due to C s popularity BUT C can be more difficult to use than more modern programming languages 8/30/12 Fall 2012 - - Lecture #4 36 6