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 2006-06-27.

Slides:



Advertisements
Similar presentations
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 1: Basic Concepts (c) Pearson Education, All rights reserved. You may modify and.
Advertisements

HEXADECIMAL NUMBERS Code
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
CS 61C L03 Introduction to C (1)Harvey / Wawrzynek Fall 2003 © UCB 8/29/2003  Brian Harvey ( John Wawrzynek  (Warznek) (
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.
Data Representation Computer Organization &
COMP3221 lec06-numbers-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 6: Number Systems - II
The slides are modified from Dan Garcia,
CSCE 121, Sec 200, 507, 508 Fall 2010 Prof. Jennifer L. Welch.
Introduction to Programming with Java, for Beginners
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
8 November Forms and JavaScript. Types of Inputs Radio Buttons (select one of a list) Checkbox (select as many as wanted) Text inputs (user types text)
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 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.
CS61C L03 Introduction to C (pt 1) (1) Garcia, Fall 2011 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 1: Basic Concepts (c) Pearson Education, All rights reserved. You may modify.
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 61C L04 C Pointers (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C : Machine.
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
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Integer Data representation Addition and Multiplication.
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
CMPT 120 How computers run programs Summer 2012 Instructor: Hassan Khosravi.
Computers Organization & Assembly Language
High-Level Programming Languages: C++
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
Lec 3: Data Representation Computer Organization & Assembly Language Programming.
May 9, 2002Serguei A. Mokhov, 1 Kickstart Intro to Java Part I COMP346/ Operating Systems Revision 1.6 February 9, 2004.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
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.
CS61C L02 Number Representation (1) Garcia, Spring 2010 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Cs641 pointers/numbers.1 Overview °Stack and the Heap °malloc() and free() °Pointers °numbers in binary.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
1 IKI20210 Pengantar Organisasi Komputer Kuliah No. 23: Aritmatika 18 Desember 2002 Bobby Nazief Johny Moningka
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
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.
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.
Chapter 10 Programming Fundamentals with JavaScript
The Machine Model Memory
CS 61C: Great Ideas in Computer Architecture Introduction to C
March, 2006 Saeid Nooshabadi
March 2006 Saeid Nooshabadi
C Basics.
Chapter 10 Programming Fundamentals with JavaScript
Instructor Paul Pearce
Homework Reading Programming Assignments Finish K&R Chapter 1
Presentation transcript:

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 Andy Carle

CS 61C L2 Introduction to C (2) A Carle, Summer 2006 © UCB Review Two’s Complement ….

CS 61C L2 Introduction to C (3) A Carle, Summer 2006 © UCB 2’s Complement Properties As with sign and magnitude, leading 0s  positive, leading 1s  negative xxx is ≥ 0, xxx is < 0 -except 1…1111 is -1, not -0 (as in sign & mag.) Only 1 Zero!

CS 61C L2 Introduction to C (4) A Carle, Summer 2006 © UCB 2’s Complement Number “line”: N = 5 2 N-1 non- negatives 2 N-1 negatives one zero how many positives?

CS 61C L2 Introduction to C (5) A Carle, Summer 2006 © 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 -(2 31 ) + d 30 x d 2 x d 1 x d 0 x 2 0 Example: 1101 two = 1x-(2 3 ) + 1x x x2 0 = = = = -3 ten

CS 61C L2 Introduction to C (6) A Carle, Summer 2006 © 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: -3 to +3 to -3 x : two x’: two +1: two ()’: two +1: two * Check out

CS 61C L2 Introduction to C (7) A Carle, Summer 2006 © 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 L2 Introduction to C (8) A Carle, Summer 2006 © UCB What if too big? Binary bit patterns above are simply representatives of numbers. Strictly speaking they are called “numerals”. Numbers really have an  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 L2 Introduction to C (9) A Carle, Summer 2006 © UCB Number Summary We represent “things” in computers as particular bit patterns: N bits  2 N Decimal for human calculations, binary for computers, hex to write binary more easily 1’s complement - mostly abandoned 2’s complement universal in computing: cannot avoid, so learn Overflow: numbers  ; computers finite, errors!

CS 61C L2 Introduction to C (10) A Carle, Summer 2006 © UCB Preview: 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 L2 Introduction to C (11) A Carle, Summer 2006 © UCB Big Idea Next Topic: Numbers can Be Anything!

CS 61C L2 Introduction to C (12) A Carle, Summer 2006 © UCB BIG IDEA: Bits can represent anything!! REMEMBER: N digits in base B  B N values For binary in particular: N bits  2 N values Characters? 26 letters  5 bits (2 5 = 32) upper/lower case + punctuation  7 bits (in 8) (“ASCII”) standard code to cover all the world’s languages  16 bits (“Unicode”) Logical values? 0  False, 1  True colors ? Ex: locations / addresses? commands? Red (00)Green (01)Blue (11)

CS 61C L2 Introduction to C (13) A Carle, Summer 2006 © UCB Example: 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 0xdeadbeef

CS 61C L2 Introduction to C (14) A Carle, Summer 2006 © UCB Moving Along Next Topic: Intro to C

CS 61C L2 Introduction to C (15) A Carle, Summer 2006 © 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 great reference. -But… check online for more sources. “JAVA in a Nutshell,” O’Reilly. -Chapter 2, “How Java Differs from C”.

CS 61C L2 Introduction to C (16) A Carle, Summer 2006 © 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 L2 Introduction to C (17) A Carle, Summer 2006 © 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 L2 Introduction to C (18) A Carle, Summer 2006 © 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 L2 Introduction to C (19) A Carle, Summer 2006 © 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 L2 Introduction to C (20) A Carle, Summer 2006 © UCB C vs. Java™ Overview (2/2) Java High memory overhead from class libraries Relatively Slow Arrays initialize to zero Syntax: /* comment */ // comment System.out.print C Low memory overhead Relatively Fast Arrays initialize to garbage Syntax: /* comment */ printf

CS 61C L2 Introduction to C (21) A Carle, Summer 2006 © 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 L2 Introduction to C (22) A Carle, Summer 2006 © 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 L2 Introduction to C (23) A Carle, Summer 2006 © 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 L2 Introduction to C (24) A Carle, Summer 2006 © 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 L2 Introduction to C (25) A Carle, Summer 2006 © UCB Administrivia First labs today (“lab is where the learning happens”) Office hours are still being arranged Class Newsgroup ucb.class.cs61c

CS 61C L2 Introduction to C (26) A Carle, Summer 2006 © 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 L2 Introduction to C (27) A Carle, Summer 2006 © 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 another variable x y Location (address) name p 104

CS 61C L2 Introduction to C (28) A Carle, Summer 2006 © UCB Pointers How to create a pointer: & operator: get address of a variable int *p, x; p?x? x = 3; p?x3 p = &x; px3 How get a value pointed to? * “ dereference operator”: get value pointed to printf(“p points to %d\n”,*p); Note the “*” gets used 2 different ways in this example. In the declaration to indicate that p is going to be a pointer, and in the printf to get the value pointed to by p.

CS 61C L2 Introduction to C (29) A Carle, Summer 2006 © UCB Pointers How to change a variable pointed to? Use dereference * operator on left of = px5 *p = 5 ; px3

CS 61C L2 Introduction to C (30) A Carle, Summer 2006 © 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 L2 Introduction to C (31) A Carle, Summer 2006 © UCB Pointers and Parameter Passing How to get a function to change a value? void addOne (int *p) { *p = *p + 1; } int y = 3; addOne(&y); y is now = 4

CS 61C L2 Introduction to C (32) A Carle, Summer 2006 © 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… and security issues… and a lot of other bad things!

CS 61C L2 Introduction to C (33) A Carle, Summer 2006 © UCB Peer Instruction A proven method for increasing student understanding The steps: I ask you a question You silently contemplate your answer -Here, we’re supposed to vote… I’m working on a mechanism to make that happen in this room When I tell you to, talk to your neighbors about your answer and settle on a new answer as a group -Here we should vote again. I’ll probably just ask someone random for their answer

CS 61C L2 Introduction to C (34) A Carle, Summer 2006 © UCB Question 1 What is in: Base 8 Base 10 Base 16 Base 18 What is ten in base 2?

CS 61C L2 Introduction to C (35) A Carle, Summer 2006 © UCB Question 2 void main(); { int *p, x=5, y; // init y = *(p = &x) + 10; int z; flip-sign(p); printf("x=%d,y=%d,p=%d\n",x,y,p); } flip-sign(int *n){*n = -(*n)} How many errors?

CS 61C L2 Introduction to C (36) A Carle, Summer 2006 © UCB My Answer

CS 61C L2 Introduction to C (37) A Carle, Summer 2006 © 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