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

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

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

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 Introduc=on to C, Part I. We are here!

CS61C : Machine Structures

CS61C : Machine Structures

CS61C : Machine Structures

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

CS61C : Machine Structures

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

CS 61c: Great Ideas in Computer Architecture

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

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

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

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

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

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

Number Representation & I See The C

Intro to C: Pointers and Arrays

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

Topics Introduction to Microprocessors

CS61C : Machine Structures

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

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

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. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

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

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

HW1 due Monday by 9:30am Assignment online, submission details to come

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

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down

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

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 hbp://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #4. Agenda

Arrays and Pointers in C. Alan L. Cox

CS 61C: Great Ideas in Computer Architecture Introduc)on to C, Part II

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

CS 61c: Great Ideas in Computer Architecture

More C Pointer Dangers

Lectures 5-6: Introduction to C

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

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

CS61C : Machine Structures

CS61C : Machine Structures

Kurt Schmidt. October 30, 2018

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

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

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

Lecture 2, September 4

PRINCIPLES OF OPERATING SYSTEMS

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

CS 61C: Great Ideas in Computer Architecture Introduc)on to C, Part II. Control. Consider memory to be a single huge array

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

CMPE-013/L. Introduction to C Programming

Lectures 5-6: Introduction to C

377 Student Guide to C++

Introduction to C. Zhiyuan Teo. Cornell CS 4411, August 26, Geared toward programmers

Lecture #6 Intro MIPS; Load & Store

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

Final CSE 131B Spring 2004

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

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

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

Lecture 03 Bits, Bytes and Data Types

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

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

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

CSE 374 Programming Concepts & Tools

CS Programming In C

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

CS61CL : Machine Structures

Pointers, Arrays, Memory: AKA the cause of those Segfaults

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

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012

18-642: Code Style for Compilers

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

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

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

Review! * follows a pointer to its value! & gets the address of a variable! Pearce, Summer 2010 UCB! ! int x = 1000; Pearce, Summer 2010 UCB!

Class Information ANNOUCEMENTS

C Arrays, Strings, More Pointers Instructor: Steven Ho

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

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

CSCI 2132 Software Development. Lecture 29: Dynamic Memory Allocation

CS61C : Machine Structures

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

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

CS3157: Advanced Programming. Announcement

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

CS61C : Machine Structures

CS61, Fall 2012 Section 2 Notes

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

CSC 1600 Memory Layout for Unix Processes"

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

CS3157: Advanced Programming. Outline

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

ELEC 377 C Programming Tutorial. ELEC Operating Systems

Transcription:

CS 61C: Great Ideas in Computer Architecture (Machine Structures) Introduc)on to C (Part I) Instructors: Randy H. Katz David A. PaGerson hgp://inst.eecs.berkeley.edu/~cs61c/sp11 Summary Spring 2011 - - Lecture #3 2 Machine Interpreta4on Levels of RepresentaOon/ InterpretaOon High Level Language Program (e.g., C) Compiler Assembly Language Program (e.g., MIPS) Assembler Machine Language Program (MIPS) Hardware Architecture DescripCon (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 instrucoons 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 DescripCon (Circuit SchemaCc Diagrams) Spring 2011 - - Lecture #3 3 Summary Spring 2011 - - Lecture #3 4 IntroducOon to C the Universal Assembly Language Some C experience is required before CS61C C++ or Java OK Based on pre- semester survey: 83% already know JAVA 54% already know C++ 34% already know C 7% already know C# About 10% have not taken 61B or equivalent If you have no experience in these languages, then start early and ask a lot of quesoons in discussion! Spring 2011 - - Lecture #3 5 Disclaimer You will not learn how to fully code in C in these lectures! You ll soll 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/ Brian Harvey s helpful transioon notes On CS61C class website hgp://inst.eecs.berkeley.edu/~cs61c/resources/ HarveyNotesC1-3.pdf Key C concepts: Pointers, Arrays, ImplicaOons for Memory management Spring 2011 - - Lecture #3 6 1

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 bytecode public class HelloWorld public static void main (String[] args) System.out.printl("Hello"); Storage Manual (malloc, free) Automatic (garbage collection) Spring Lecture #3 2011 - - 7 From hgp://www.cs.princeton.edu/introcs/faq/c2java.html Compiler Typed variables Typed func)ons 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 funcoon Declare funcoons and variables in a separate file Groups of related values Lists of predefined values Aliases to other variables These concepts disonguish C from other languages you may know Spring 2011 - - Lecture #3 8 CompilaOon: 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 most Scheme environments, which interpret the code These differ mainly in exactly when your program is converted to low- level machine instrucoons ( levels of interpretaoon ) 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 automaocally, by default) CompilaOon: Advantages Excellent run- Ome performance: generally much faster than Scheme or Java for comparable code (because it opomizes for a given architecture) Fair compilaoon Ome: enhancements in compilaoon 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 instruc)ons, parallelism Spring 2011 - - Lecture #3 9 Spring 2011 - - Lecture #3 10 CompilaOon: Disadvantages Compiled files, including the executable, are architecture- specific, depending on CPU type and the operaong system Executable must be rebuilt on each new system I.e., porong your code to a new architecture Change Compile Run [repeat] iteraoon cycle can be slow, during the development cycle Typed Variables in C int variable1 = 2; float variable2 = 1.618; char variable3 = 'A'; You have to declare the type of data a variable will hold Types can't change Type DescripCon Examples int integer numbers, including negaoves 0, 78, - 1400 unsigned int integer numbers (no negaoves) 0, 46, 900 float floaong point decimal numbers 0.0, 1.618, - 1.4 char single text character or symbol 'a', 'D', '? double greater precision FP number long larger signed integer Spring 2011 - - Lecture #3 11 Spring 2011 - - Lecture #3 12 2

Typed FuncOons in C Structs in C int numberofpeople () return 3; float dollarsandcents () return 10.33; char firstletter () return 'A'; You have to declare the type of data you plan to return from a funcoon Return type can be any C variable type, and is placed to the ley of the funcoon 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 define types for values passed into a funcoon Variables and funcoons MUST be defined before they are used Structs are structured groups of variables, e.g., typedef struct int lengthinseconds; int yearrecorded; Song; Song song1; song1.lengthinseconds = 213; song1.yearrecorded = 1994; Song song2; song2.lengthinseconds = 248; song2.yearrecorded = 1988; Dot notaoon: x.y = value Spring 2011 - - Lecture #3 13 Spring 2011 - - Lecture #3 14 Consts and Enums in C Constant is assigned a value once in the declaraoon; value can't change unol the program is restarted const float goldenratio = 1.618; const int daysinweek = 7; You can have a constant version of any of the standard C variable types Enums: a group of related constants used to parameterize libraries Spring 2011 - - Lecture #3 15 Spring 2011 - - Lecture #3 16 Administrivia We promised you that CS61c would be relentless! This week: Lab #2, HW #2 Lab #2, Amazon EC2, will soon be posted HW #2 will soon be posted TAs have their office hours late in the week don t wait for last minute to get quesoons answered on HWs and Labs Take a quick look when posted and come to discussion with quesoons We are amazed that there students who soll haven t signed up for the class discussion/announcements group! Wonderful to see the valuable discussion and help going on there! Spring 2011 - - Lecture #3 17 Spring 2011 - - Lecture #3 18 3

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); #include <stdio.h> #include <math.h> void main() 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 */ Spring 2011 - - Lecture #3 19 Spring 2011 - - Lecture #3 20 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: main! To get arguments to the main funcoon, 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) Spring 2011 - - Lecture #3 21 Spring 2011 - - Lecture #3 22 C Syntax: Variable DeclaraOons Similar to Java, but with a few minor but important differences All variable declaraoons must appear before they are used (e.g., at the beginning of the block) A variable may be inioalized in its declaraoon; if not, it holds garbage! Examples of declaraoons: Correct:!!!!! int a = 0, b = 10;!!!!!!...! Incorrect: for (int i = 0; i < 10; i++)!! C Syntax : Flow Control (1/2) Within a funcoon, remarkably close to Java constructs in methods (shows its legacy) in terms of flow control if-else if (expression) statement if (expression) statement1 else statement2 while while (expression) statement do statement while (expression); Spring 2011 - - Lecture #3 23 Spring 2011 - - Lecture #3 24 4

C Syntax : Flow Control (2/2) Within a funcoon, remarkably close to Java constructs in methods (shows its legacy) in terms of flow control for for (initialize; check; update) statement switch switch (expression) case const1: statements case const2: statements default: statements break C Syntax: True or False 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 scheme: only #f is false, anything else is true! Spring 2011 - - Lecture #3 25 Spring 2011 - - Lecture #3 26 Spring 2011 - - Lecture #3 27 Spring 2011 - - Lecture #3 28 Pointers in C A pointer is just another kind of value A basic type in C int *ptr; Variable ptr is a pointer to an int Some of these slides come from Arrays and Pointers in C by Alan Cox and T.S. Ng, Rice University, www.clear.rice.edu/comp221/html/ppt/03- arrays- pointers.ppt Spring 2011 - - Lecture #3 29 Address vs. Value Consider memory to be a single huge array Each cell of the array has an address associated with it Each cell also stores some value Do you think they use signed or unsigned numbers? NegaOve address?! Don t confuse the address referring to a memory locaoon with the value stored there... 101 102 103 104 105... 23 42... Spring 2011 - - Lecture #3 30 5

Pointers An address refers to a parocular memory locaoon; e.g., it points to a memory locaoon Pointer: A variable that contains the address of a variable LocaOon (address) name... 101 102 103 104 105... 104 23 42 p... x y Spring 2011 - - Lecture #3 31 Pointers in C If T is a type, T *p declares p a pointer to that type You can use p as a pointer to a T You can use *p as a T p++ increments p by the size of a T Important because of the way arrays are treated You can make a pointer to any variable If x is any variable, then &x is its address Spring 2011 - - Lecture #3 32 Pointer OperaOons in C CreaOon & variable Returns variable s memory address Dereference * pointer Returns contents stored at address Indirect assignment *pointer = val Stores value at address Of course, soll have assignment pointer = ptr Stores pointer in another variable Spring 2011 - - Lecture #3 33 How to create a pointer: & operator: get address of a variable int *p, x; p? x? x = 3; p? x 3 p = &x; p x 3 How get a value pointed to? * dereference operator : get the value that the pointer points to!printf( p points to %d\n,*p);! Note the * gets used 2 different ways in this example. In the declaraoon to indicate that p is going to be a pointer, and in the printf to get the value pointed to by p. Spring 2011 - - Lecture #3 34 How to change a variable pointed to? Use the dereference operator * on ley of assignment operator = *p = 5; p x 3 p x 5 int i1; int i2; int *ptr1; int *ptr2; i1 = 1; i2 = 2; ptr1 = &i1; ptr2 = ptr1; *ptr1 = 3; i2 = *ptr2; 0x1014 0x1010 0x100C 0x1008 0x1004 0x1000 ptr2: ptr1: i2: i1: 0x1000 0x1000 23 31 Spring 2011 - - Lecture #3 35 Spring 2011 - - Lecture #3 36 6

int1 becomes 8 int int1 = 1036; /* some data to point to */ int int2 = 8; int *int_ptr1 = &int1; /* get addresses of data */ int *int_ptr2 = &int2; *int_ptr1 = int_ptr2; *int_ptr1 = int2; What happens? Type check warning: int_ptr2 is not an int int int1 = 1036; /* some data to point to */ int int2 = 8; int *int_ptr1 = &int1; /* get addresses of data */ int *int_ptr2 = &int2; int_ptr1 = *int_ptr2; int_ptr1 = int_ptr2; What happens? Type check warning: *int_ptr2 is not an int * Changes int_ptr1 doesn t change int1 Spring 2011 - - Lecture #3 37 Spring 2011 - - Lecture #3 38 Pointers and Parameter Passing Java and C pass parameters by value Procedure/funcOon/method gets a copy of the parameter, so changing the copy cannot change the original void addone (int x)! x = x + 1;! int y = 3;! addone(y);! y remains equal to 3 Pointers and Parameter Passing How can we get a funcoon to change a value? void addone (int *p)!*p = *p + 1;! int y = 3;! addone(&y);! y is now equal to 4 Spring 2011 - - Lecture #3 39 Spring 2011 - - Lecture #3 40 Pass By Reference Ge~ng Pass- by- Reference In C, the default passing strategy is pass by copy To pass by reference, we use pass by copy because in C, everything is pass by copy Value that we pass by copy is address of the actual argument: we achieve the through the address operator & In C, pass by reference is actually pass by copy because you copy the address void set_x_and_y(int *x, int *y) *x = 1001; *y = 1002; void f(void) int a = 1; int b = 2; set_x_and_y(&a,&b); a b x y 1001 1 1002 2 Spring 2011 - - Lecture #3 41 Spring 2011 - - Lecture #3 42 7

Pointers are used to point to any kind of data (int, char, a struct, etc.) Normally a pointer only points to one type (int, char, a struct, etc.). void * is a type that can point to anything (generic pointer) Use sparingly to help avoid program bugs, and security issues, and other bad things! Spring 2011 - - Lecture #3 43 Peer InstrucOon QuesOon!void main(); int *p, x=5, y; // init y = *(p = &x) + 1; int z; flip-sign(p); printf("x=%d,y=%d,p=%d\n",x,y,p); flip-sign(int *n)*n = -(*n) How many syntax + logic errors in this C code? #Errors Red: 1 Orange: 2 Green: 3 Yellow: 4 Pink: 5 Spring 2011 - - Lecture #3 44 More C Pointer Dangers Declaring a pointer just allocates space to hold the pointer it does not allocate the thing being pointed to! Local variables in C are not inioalized, they may contain anything (aka garbage ) What does the following code do? void f() int *ptr; *ptr = 5; Spring 2011 - - Lecture #3 46 Pointers in C Why use pointers? If we want to pass a large struct or array, it s easier / faster / etc. to pass a pointer than the whole thing In general, pointers allow cleaner, more compact code So what are the drawbacks? Pointers are probably the single largest source of bugs in C, so be careful anyome you deal with them Most problemaoc with dynamic memory management which you will to know by the end of the semester, but not for the projects (there will be a lab later in the semester) Dangling references and memory leaks Spring 2011 - - Lecture #3 47 Why Pointers in C? At Ome C was invented (early 1970s), compilers oyen didn t produce efficient code Computers 25,000 Omes faster today, compilers beger C designed to let programmer say what they want code to do without compiler ge~ng in way Even give compilers hints which registers to use! Today s compilers produce much beger code, so may not need to use pointers Compilers even ignore hints since they do it beger! FYI Update to ANSI C C99 or C9x standard gcc -std=c99 to compile References http://en.wikipedia.org/wiki/c99! http://home.tiscalinet.ch/t_wolf/tw/c/ c9x_changes.html! Highlights DeclaraOons in for loops, like Java Java- like // comments (to end of line) Variable- length non- global arrays <inttypes.h>: explicit integer types <stdbool.h>: for boolean logic types and definioons Spring 2011 - - Lecture #3 48 Spring 2011 - - Lecture #3 49 8

And In Conclusion, All data is in memory Each memory locaoon has an address to use to refer to it and a value stored in it Pointer is a C version (abstracoon) of a data address * follows a pointer to its value & gets the address of a value Arrays and strings are implemented as variaoons on pointers C is an efficient language, but leaves safety to the programmer Array bounds not checked Variables not automaocally inioalized Use pointers with care: they are a common source of bugs in programs Spring 2011 - - Lecture #3 50 9