CS 61C L03 Introduction to C (1)Harvey / Wawrzynek Fall 2003 © UCB 8/29/2003  Brian Harvey (www.cs.berkeley.edu/~bh) John Wawrzynek  (Warznek) (www.cs.berkeley.edu/~johnw)

Slides:



Advertisements
Similar presentations
1/1/ / faculty of Electrical Engineering eindhoven university of technology Introduction Part 2: Data types and addressing modes dr.ir. A.C. Verschueren.
Advertisements

Assembly Language for Intel-Based Computers, 4 th Edition Chapter 1: Basic Concepts (c) Pearson Education, All rights reserved. You may modify and.
CS61C L03 Introduction to C (pt 1) (1) Garcia, Fall 2006 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
CS 61C L02 Number Representation (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
COMP3221: Microprocessors and Embedded Systems--Lecture 1 1 COMP3221: Microprocessors and Embedded Systems Lecture 3: Number Systems (I)
COMP3221 lec05-numbers-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 5: Number Systems – I
CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT.
CS 61C L02 Number Representation (1)Harvey / Wawrzynek Fall 2003 © UCB 8/27/2003  Brian Harvey ( John Wawrzynek  (Warznek) (
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
COMP3221 lec06-numbers-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 6: Number Systems - II
CS 61C L09 Introduction to MIPS: Data Transfer & Decisions I (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS 61C L03 Introduction to C (pt 1) (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
CSCE 121, Sec 200, 507, 508 Fall 2010 Prof. Jennifer L. Welch.
CS 61C L02 Number Representation (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
Assembly Language for Intel-Based Computers, 4th Edition
Machine Representation/Numbers Lecture 3 CS 61C Machines Structures Fall 00 David Patterson U.C. Berkeley
COMP3221: Microprocessors and Embedded Systems--Lecture 4 1 COMP3221: Microprocessors and Embedded Systems Lecture 4: Number Systems (II)
CS61C L2 Number Representation & Introduction to C (1) Chae, Summer 2008 © UCB Albert Chae Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures.
CS 61C L2 Introduction to C (1) A Carle, Summer 2006 © UCB inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 2: Introduction To C
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
CS 61C L03 C Arrays (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #3: C Pointers & Arrays
1 Binary Arithmetic, Subtraction The rules for binary arithmetic are: = 0, carry = = 1, carry = = 1, carry = = 0, carry =
CS61C L2 Number Representation & Introduction to C (1) Garcia, Fall 2005 © UCB Lecturer PSOE, new dad Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS61C L03 Introduction to C (pt 1) (1) Garcia, Spring 2008 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
CS 61C L03 C Pointers (1)Garcia / Patterson Fall 2002 © UCB CS61C - Machine Structures Lecture 3 C pointers  Dan Garcia (
1 Lecture 7: Computer Arithmetic Today’s topics:  Chapter 2 wrap-up  Numerical representations  Addition and subtraction Reminder: Assignment 3 will.
CS61C L03 Introduction to C (pt 1) (1) Garcia, Fall 2011 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
Number Representation (1) Fall 2005 Lecture 12: Number Representation Integers and Computer Arithmetic.
COMP3221 lec07-numbers-III.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 7: Number Systems - III
CS 61C L03 Introduction to C (pt 1) (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
+ CS 325: CS Hardware and Software Organization and Architecture Integers and Arithmetic Part 4.
CS61C L2 Number Representation & Introduction to C (1) Beamer, Summer 2007 © UCB Scott Beamer Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine.
CS 61C L03 C Arrays (1) A Carle, Summer 2006 © UCB inst.eecs.berkeley.edu/~cs61c/ CS61C : Machine Structures Lecture #3: C Pointers & Arrays
CS61C L03 Introduction to C (pt 1) (1) Garcia, Fall 2014 © UCB Senior Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
1 Binary Numbers Again Recall that N binary digits (N bits) can represent unsigned integers from 0 to 2 N bits = 0 to 15 8 bits = 0 to bits.
Binary Arithmetic Math For Computers.
Lecture 2: Number Representation
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
CENG 311 Machine Representation/Numbers
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
CSU0014 Assembly Languages Homepage: Textbook: Kip R. Irvine, Assembly Language for Intel-Based Computers,
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
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
IT 251 Computer Organization and Architecture Introduction to the C Programming Language Part 1 Chia-Chi Teng.
BR 8/99 Binary Numbers Again Recall than N binary digits (N bits) can represent unsigned integers from 0 to 2 N bits = 0 to 15 8 bits = 0 to 255.
Chapter 19 Number Systems. Irvine, Kip R. Assembly Language for Intel-Based Computers, Translating Languages English: Display the sum of A times.
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
Cs641 pointers/numbers.1 Overview °Stack and the Heap °malloc() and free() °Pointers °numbers in binary.
Monday, January 14 Homework #1 is posted on the website Homework #1 is posted on the website Due before class, Jan. 16 Due before class, Jan. 16.
CS1Q Computer Systems Lecture 2 Simon Gay. Lecture 2CS1Q Computer Systems - Simon Gay2 Binary Numbers We’ll look at some details of the representation.
1 IKI20210 Pengantar Organisasi Komputer Kuliah No. 23: Aritmatika 18 Desember 2002 Bobby Nazief Johny Moningka
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
EE204 L03-ALUHina Anwar Khan EE204 Computer Architecture Lecture 03- ALU.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Based on slides from D. Patterson and www-inst.eecs.berkeley.edu/~cs152/ COM 249 – Computer Organization and Assembly Language Chapter 3 Arithmetic For.
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
The Machine Model Memory
A bit of C programming Lecture 3 Uli Raich.
Morgan Kaufmann Publishers
CS 61C: Great Ideas in Computer Architecture Introduction to C
March 2006 Saeid Nooshabadi
C Basics.
Arrays in C.
Instructor Paul Pearce
Lecture 2: Bits, Bytes, Ints
Presentation transcript:

CS 61C L03 Introduction to C (1)Harvey / Wawrzynek Fall 2003 © UCB 8/29/2003  Brian Harvey ( John Wawrzynek  (Warznek) ( www-inst.eecs.berkeley.edu/~cs61c/ CS61C – Machine Structures Lecture 3 – Introduction to the C Programming Language

CS 61C L03 Introduction to C (2)Harvey / Wawrzynek Fall 2003 © UCB Review °We represent “things” in computers as particular bit patterns: N bits  2 N numbers, characters,... °Decimal for human calculations, binary to understand computers, hexadecimal to understand binary °2’s complement universal in computing: cannot avoid, so learn °Computer operations on the representation correspond to real operations on the real thing

CS 61C L03 Introduction to C (3)Harvey / Wawrzynek Fall 2003 © UCB 2’s Complement Number “line”: N = 5 °2 N-1 non- negatives °2 N-1 negatives °one zero °how many positives?

CS 61C L03 Introduction to C (4)Harvey / Wawrzynek Fall 2003 © UCB Two’s Complement for N= two = 0 ten two = 1 ten two = 2 ten two = 2,147,483,645 ten two = 2,147,483,646 ten two = 2,147,483,647 ten two = –2,147,483,648 ten two = –2,147,483,647 ten two = –2,147,483,646 ten two =–3 ten two =–2 ten two =–1 ten °One zero; 1st bit called sign bit °1 “extra” negative:no positive 2,147,483,648 ten

CS 61C L03 Introduction to C (5)Harvey / Wawrzynek Fall 2003 © UCB Two’s Complement Formula °Can represent positive and negative numbers in terms of the bit value times a power of 2: d 31 x d 30 x d 2 x d 1 x d 0 x 2 0 °Example: two = 1x x2 8 +1x x2 2 +0x2 1 +0x2 0 = = = = -4 ten

CS 61C L03 Introduction to C (6)Harvey / Wawrzynek Fall 2003 © UCB Two’s Complement shortcut: Negation °Change every 0 to 1 and 1 to 0 (invert or complement), then add 1 to the result °Proof: Sum of number and its (one’s) complement must be two However, two = -1 ten Let x’  one’s complement representation of x Then x + x’ = -1  x + x’ + 1 = 0  x’ + 1 = -x °Example: -4 to +4 to -4 x : two x’: two +1: two ()’: two +1: two

CS 61C L03 Introduction to C (7)Harvey / Wawrzynek Fall 2003 © UCB Two’s comp. shortcut: Sign extension °Convert 2’s complement number rep. using n bits to more than n bits °Simply replicate the most significant bit (sign bit) of smaller to fill new bits 2’s comp. positive number has infinite 0s 2’s comp. negative number has infinite 1s Binary representation hides leading bits; sign extension restores some of them 16-bit -4 ten to 32-bit: two two

CS 61C L03 Introduction to C (8)Harvey / Wawrzynek Fall 2003 © UCB Signed vs. Unsigned Variables °Java just declares integers int Uses two’s complement °C has declaration int also Declares variable as a signed integer Uses two’s complement °Also, C declaration unsigned int Declares a unsigned integer Treats 32-bit number as unsigned integer, so most significant bit is part of the number, not a sign bit

CS 61C L03 Introduction to C (9)Harvey / Wawrzynek Fall 2003 © UCB Numbers represented in memory °Memory is a place to store bits °A word is a fixed number of bits (eg, 32) at an address °Addresses are naturally represented as unsigned numbers in C = 2 k

CS 61C L03 Introduction to C (10)Harvey / Wawrzynek Fall 2003 © UCB Signed v. Unsigned Comparisons °X = two °Y = two °Is X > Y? unsigned:YES signed:NO

CS 61C L03 Introduction to C (11)Harvey / Wawrzynek Fall 2003 © UCB What if too big? °Binary bit patterns above are simply representatives of numbers. Strictly speaking they are called “numerals”. °Numbers really have an infinite number of digits with almost all being same (00…0 or 11…1) except for a few of the rightmost digits Just don’t normally show leading digits °If result of add (or -,*,/) cannot be represented by these rightmost HW bits, overflow is said to have occurred unsigned

CS 61C L03 Introduction to C (12)Harvey / Wawrzynek Fall 2003 © UCB And in Conclusion... °We represent “things” in computers as particular bit patterns: N bits  2 N numbers, characters,... °Decimal for human calculations, binary to understand computers, hexadecimal to understand binary °2’s complement universal in computing: cannot avoid, so learn °Computer operations on the representation correspond to real operations on the real thing °Overflow: numbers infinite but computers finite, so errors occur

CS 61C L03 Introduction to C (13)Harvey / Wawrzynek Fall 2003 © UCB Administrivia : Who gets lab seat? Rank order of seating priority 1. 61c registered for that section 2. 61c registered for another section 3. 61c waitlisted for that section 4. 61c waitlisted for another section 5. Concurrent enrollment

CS 61C L03 Introduction to C (14)Harvey / Wawrzynek Fall 2003 © UCB Administrivia : You have a question? Tips on getting an answer to your question: 1. Ask a classmate 2. Ask Brian/John after or before lecture 3. The newsgroup, ucb.class.cs61c a) Read it : Has your Q been answered already? b) If not, ask it and check back 4. Ask TA in section, lab or OH 5. Ask Brian/John in OH 6. Ask Brian/John in lecture 7. Send your TA 8. Send Brian/John

CS 61C L03 Introduction to C (15)Harvey / Wawrzynek Fall 2003 © UCB Administrivia : Near term °Get cardkeys from CS main office Soda Hall 3rd floor °Reading for weekend: K&R Ch 5 and 6 °Monday holiday °Wed lecture C pointers and arrays in detail °HW HW1 due Wednesday 9am. HW2 will be posted soon.

CS 61C L03 Introduction to C (16)Harvey / Wawrzynek Fall 2003 © UCB Disclaimer °Important: You will not learn how to fully code in C in these lectures! You’ll still need your C reference for this course. K&R is a must-have reference. -Check online for more sources. “JAVA in a Nutshell,” O’Reilly. -Chapter 2, “How Java Differs from C”.

CS 61C L03 Introduction to C (17)Harvey / Wawrzynek Fall 2003 © UCB Compilation : Overview C compilers take C and convert it into an 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. Generally a 2 part process of compiling.c files to.o files, then linking the.o files into executables

CS 61C L03 Introduction to C (18)Harvey / Wawrzynek Fall 2003 © UCB Compilation : Advantages °Great run-time performance: generally much faster than Scheme or Java for comparable code (because it optimizes for a given architecture) °OK compilation time: enhancements in compilation procedure (Makefiles) allow only modified files to be recompiled

CS 61C L03 Introduction to C (19)Harvey / Wawrzynek Fall 2003 © UCB Compilation : Disadvantages °All compiled files (including the executable) are architecture specific, depending on both the CPU type and the operating system. °Executable must be rebuilt on each new system. Called “porting your code” to a new architecture. °The “change-compile-run [repeat]” iteration cycle is slow

CS 61C L03 Introduction to C (20)Harvey / Wawrzynek Fall 2003 © UCB C vs. Java™ Overview (1/2) Java Object-oriented (OOP) “Methods” Class libraries of data structures Automatic memory management C No built-in object abstraction. Data separate from methods. “Functions” C libraries are lower-level Manual memory management Pointers

CS 61C L03 Introduction to C (21)Harvey / Wawrzynek Fall 2003 © UCB C vs. Java™ Overview (2/2) Java High memory overhead from class libraries Relatively Slow Arrays initialize to zero Syntax: // comment System.out.print C Low memory overhead Relatively Fast Arrays initialize to garbage Syntax: /* comment */ printf

CS 61C L03 Introduction to C (22)Harvey / Wawrzynek Fall 2003 © UCB C Syntax: Variable Declarations °Very similar to Java, but with a few minor but important differences °All variable declarations must go before they are used (at the beginning of the block). °A variable may be initialized in its declaration. °Examples of declarations: correct : { int a = 0, b = 10;... incorrect: for (int i = 0; i < 10; i++)

CS 61C L03 Introduction to C (23)Harvey / Wawrzynek Fall 2003 © UCB C Syntax: True or False? °What evaluates to FALSE in C? 0 (integer) NULL (pointer: more on this later) no such thing as a Boolean °What evaluates to TRUE in C? everything else… (same idea as in scheme: only #f is false, everything else is true!)

CS 61C L03 Introduction to C (24)Harvey / Wawrzynek Fall 2003 © UCB C syntax : flow control ° Within a function, remarkably close to Java constructs in methods (shows its legacy) in terms of flow control if-else switch while and for do-while

CS 61C L03 Introduction to C (25)Harvey / Wawrzynek Fall 2003 © UCB C Syntax: main °To get the main function to accept arguments, use this: int main (int argc, char *argv[]) °What does this mean? argc will contain the number of strings on the command line (the executable counts as one, plus one for each argument). -Example: unix% sort myFile argv is a pointer to an array containing the arguments as strings (more on pointers later).

CS 61C L03 Introduction to C (26)Harvey / Wawrzynek Fall 2003 © UCB 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 °Don’t confuse the address referring to a memory location with the value stored in that location.

CS 61C L03 Introduction to C (27)Harvey / Wawrzynek Fall 2003 © UCB Pointers °An address refers to a particular memory location. In other words, it points to a memory location. °Pointer: A variable that contains the address of a variable x y Location (address) name z 104

CS 61C L03 Introduction to C (28)Harvey / Wawrzynek Fall 2003 © UCB Pointers °How to create a pointer: & operator: get address of a variable int *x, y; x?y? y = 3; x?y3 x = &y; xy3 °How get a value pointed to? * “ dereference operator”: get value pointed to printf(“x points to %d\n”,*x); Note the “*” gets used 2 different ways in this example. In the declaration to indicate that x is going to be a pointer, and in the printf to get the value pointed to by x.

CS 61C L03 Introduction to C (29)Harvey / Wawrzynek Fall 2003 © UCB Pointers °How to change a variable pointed to? Use dereference * operator on left of = xy5 *x = 5 ; xy3

CS 61C L03 Introduction to C (30)Harvey / Wawrzynek Fall 2003 © UCB Pointers and Parameter Passing °Java and C pass a parameter “by value” procedure/function 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 is still = 3

CS 61C L03 Introduction to C (31)Harvey / Wawrzynek Fall 2003 © UCB Pointers and Parameter Passing °How to get a function to change a value? void addOne (int *x) { *x = *x + 1; } int y = 3; addOne(&y); y is now = 4

CS 61C L03 Introduction to C (32)Harvey / Wawrzynek Fall 2003 © UCB Pointers °Normally a pointer can only point 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!

CS 61C L03 Introduction to C (33)Harvey / Wawrzynek Fall 2003 © UCB And in conclusion… °All declarations go at the beginning of each function. °Only 0 and NULL evaluate to FALSE. °All data is in memory. Each memory location has an address to use to refer to it and a value stored in it. °A pointer is a C version of the address. * “follows” a pointer to its value & gets the address of a value