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

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

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

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 61C: Great Ideas in Computer Architecture Lecture 2: Introduction to C, Part I

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

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

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

CS 61c: Great Ideas in Computer Architecture

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

CS 61c: Great Ideas in Computer Architecture

CS Programming In C

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

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

CS 61C: Great Ideas in Computer Architecture Intro to Assembly Language, MIPS Intro

CS 61C: Great Ideas in Computer Architecture Introduction to Assembly Language and RISC-V Instruction Set Architecture

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

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 3: Pointers. Krste Asanović & Randy Katz

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

CS 61C: Great Ideas in Computer Architecture Intro to Assembly Language, MIPS Intro

C - Basics, Bitwise Operator. Zhaoguo Wang

Inf2C - Computer Systems Lecture 2 Data Representation

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

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

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

But first, encode deck of cards. Integer Representation. Two possible representations. Two better representations WELLESLEY CS 240 9/8/15

Operations, Operands, and Instructions

CS61C : Machine Structures

CS61C : Machine Structures

CS61C : Machine Structures

PRINCIPLES OF OPERATING SYSTEMS

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

6.096 Introduction to C++ January (IAP) 2009

CS 261 Fall Mike Lam, Professor Integer Encodings

CS 61C: Great Ideas in Computer Architecture More RISC-V Instructions and How to Implement Functions

CS 61C: Great Ideas in Computer Architecture RISC-V Instruction Formats

9/14/17. Levels of Representation/Interpretation. Big Idea: Stored-Program Computer

A Fast Review of C Essentials Part I

Topics Introduction to Microprocessors

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

World Inside a Computer is Binary

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

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

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

CS61C Machine Structures. Lecture 8 - Introduction to the MIPS Processor and Assembly Language. 9/14/2007 John Wawrzynek

CS61C : Machine Structures

CS61C : Machine Structures

Introduction to C. Systems Programming Concepts

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

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

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

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

Data III & Integers I

Lectures 5-6: Introduction to C

Lecture 3. More About C

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Chapter 3. Fundamental Data Types

Lecture 3: Instruction Set Architecture

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Lecture 03 Bits, Bytes and Data Types

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

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

CS 31: Intro to Systems Binary Arithmetic. Martin Gagné Swarthmore College January 24, 2016

Data Type Fall 2014 Jinkyu Jeong

Computer Components. Software{ User Programs. Operating System. Hardware

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

CS3157: Advanced Programming. Outline

Programming. Data Structure

Jin-Soo Kim Systems Software & Architecture Lab. Seoul National University. Integers. Spring 2019

AGENDA Binary Operations CS 3330 Samira Khan

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

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

Number Systems for Computers. Outline of Introduction. Binary, Octal and Hexadecimal numbers. Issues for Binary Representation of Numbers

Lecture 7: Examples, MARS, Arithmetic

Programming refresher and intro to C programming

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

Bits, Bytes, and Integers Part 2

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

C-string format with scanf/printf

DEPARTMENT OF MATHS, MJ COLLEGE

Fundamental of Programming (C)

Computer Organization & Systems Exam I Example Questions

CS 33. Data Representation, Part 1. CS33 Intro to Computer Systems VII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

THE FUNDAMENTAL DATA TYPES

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Data III & Integers I

CSCI 171 Chapter Outlines

M1 Computers and Data

Survey. Motivation 29.5 / 40 class is required

CS 253. January 14, 2017

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

BLM2031 Structured Programming. Zeyneb KURT

The C Programming Language Guide for the Robot Course work Module

Lectures 5-6: Introduction to C

CS 61C: Great Ideas in Computer Architecture Introduction to Assembly Language and MIPS Instruction Set Architecture

Transcription:

CS 61C: Great Ideas in Computer Architecture Lecture 2: Numbers & C Language Krste Asanović & Randy Katz http://inst.eecs.berkeley.edu/~cs61c Numbers wrap-up This is not on the exam! Break C Primer Administrivia, Break C Type declarations And in Conclusion, 2 Recap: Binary Number Conversion Binary à Decimal 1001010two =?ten Binary Digit Decimal Value 0 0 x 2 0 = 0 1 1 x 2 1 = 2 0 0 x 2 2 = 0 1 1 x 2 3 = 8 0 0 x 2 4 = 0 0 0 x 2 5 = 0 1 1 x 2 6 = 64 S = 4ten Decimal à Binary 4ten Decimal =?two Binary (odd?) 4 0 /2 = 3 1 /2 = 18 0 /2 = 9 1 /2 = 4 0 /2 = 2 0 /2 = 1 1 Collect à 1001010two 3 Signed Integer Representation How to represent -5? Sign & magnitude (8-bit example): sign -bit magnitude (0 12) Rules for addition, a + b: If (a>0 and b>0): add b to a If (a>0 and b<0): subtract b from a +0, -0 à are they equal? If so, comparator must handle special case! (If not equal???) Cumbersome Complicated hardware: reduced speed / increased power Is there a better way? 4 Going backwards by going forwards How to add -1 to a four-bit number? 0111 two = ten???? two + = -1 ten 0110 two = 6 ten Going backwards by going forwards Add 10000two and throw away fifth bit, result unchanged Result wraps around to same value 0111 two = ten 10000 two + = 16 ten 10111 two = ten +16 ten 0111 two = ten 5 6 1

Going backwards by going forwards Add (10000 two 1 two = 01111 two ) and throw away fifth bit One less than original value 0111 two = ten 01111 two + = 16-1 ten 10110 two = 16 + 6 ten 0110 two = 6 ten + -3 4 Decimal + -3 + 16 4 + 16 4-bit Example + 13 4 + 16 Binary 0111 + 1101 1 0100 0100 + 1 0000 Map negative à positive numbers Example for N=4-bit: -3 à 2 4 3 = 13 Two s complement No special rules for adding positive and negative numbers Extra bit is ignored as doesn t fit in 4 bits. -8 - -1 0 1 0 1 8 9 15 + 2 4 = 16 8/29/1 8 Two s Complement (8-bit example) As Signed Decimal As Unsigned Decimal Binary Number -128 128 1000 0000-12 +256 129 1000 0001-2 254 1111 1110-1 255 1111 1111 0 +0 0 0000 0000 1 1 0000 0001... 12 12 0111 1111 Most-significant bit (MSB) equals sign when interpreted as a signed number Signed versus Unsigned Binary Numbers Both are stored as a collection of bits The same set of bits can be used to represent a signed number or an unsigned number (or a string, or a picture, or a.) It s up to the operation interpreting the bits 1101 two = +13 ten (as an unsigned number) 1101 two = -3 ten (as a signed number) 1101 two = orange (as a color) 1101 two = cat (as a type of animal) 1101 two = whatever you want it to mean 9 10 Unary Negation (Two s Complement) 4-bit Example (-8 ten + ten ) Brute Force & Tedious largest 4-bit number + 1 16 ten 10000 two - 3 ten - 0011 two 13 ten 1101 two 16 ten 10000 two - 13 ten - 1101 two Clever & Elegant 15 ten 01111 two - 3 ten - 0011 two 12 ten 1100 two invert + 1 ten + 0001 two 13 ten 1101 two Your Turn What is the decimal value of the following binary 8-bit 2 s complement number? 1110 0001 two Answer RED ORANGE GREEN YELLOW Value 33 ten -31 ten 225 ten -33 ten 11 12 2

Addition 4-bit Example Overflow 4-bit Example Unsigned Signed (Two s Complement) Unsigned addition Signed addition (Two s Complement) + 4 ten + 0100 two + 4 ten + 0100 two 13 ten 1101 two + 14 ten + 1110 two -3 ten 1101 two + -2 ten + 1110 two ten 0111 two ten 0111 two 2 ten 1 1011 two carry-out and overflow -5 ten 1 1011 two carry-out but no overflow + 11 ten + 1011 two + -5 ten + 1011 two ten 0111 two + 1 ten + 0001 two ten 0111 two + 1 ten + 0001 two 14 ten 1110 two -2 ten 1110 two No special rules for two s complement signed addition 13 8 ten 0 1000 two no carry-out and no overflow Carry-out à Overflow -8 ten 0 1000 two no carry-out but overflow Carry-out à Overflow 14 Unsigned addition Carry-out indicates overflow Addition Overflow Detection 4-bit Example Signed addition (Two s Complement) Overflow if Signs of operands are equal AND Sign of result differs from sign of operands No overflow is possible when signs of operands differ Overflow rules depend on the operation (signed vs unsigned addition) Adding Numbers of Different Bit Widths Unsigned addition 1101 (4-bit, 13 ten ) 10100101 + (8-bit, 165 ten )????0010 (8-bit,??? ten ) Zero extension 00001101 (4-bit, 13 ten ) 10100101 + (8-bit, 165 ten ) 10110010 (8-bit, 18 ten ) What should we assume for upper four bits of first operand? Treat as zeros! Zero-extension 15 16 Adding Signed Numbers of Different Bit Widths Sign bit Signed addition 1101 (4-bit, -3 ten ) 10100101 + (8-bit, -91 ten )????0010 (8-bit,??? ten ) Sign extension 11111101 (4-bit, -3 ten ) 10100101 + (8-bit, -91 ten ) 10100010 (8-bit, -94 ten ) What should we assume for upper four bits of first operand? Copy sign bit (MSB) of operand into upper bits If sign is 1, copy 1s, if sign is 0 copy 0s Sign-extension Decimal Sign Extension Binary 4-bit 8-bit 32-bit 0000 0011 two 0000 0000 0000 0011 two -3 ten 1101 two 1111 1101 two 1111 1111 1111 1101 two When is this relevant? Any time you need a wider version of a narrower operand e.g., adding two integers of different widths 1 18 3

8/29/1 Your Turn Which range of decimals can be expressed with a 6-bit two s complement number? Answer Range RED -32 32 GREEN -64 63 ORANGE -31 32 YELLOW -32 31 Numbers wrap-up This is not on the exam! Break C Primer Administrivia, Break C Type declarations And in Conclusion, 19 20 HotChips 201: Brains & Dinosaurs Top conference introducing new processor chips 21 22 Z14, IBM Mainframe, 50+ years on 23 24 4

Break! 8/29/1 Fall 201 - Lecture #2 25 Numbers wrap-up This is not on the exam! Break C Primer Administrivia, Break C Type declarations And in Conclusion, 26 Levels of Representation High Level Language Program (e.g., C) Compiler Assembly Language Program (e.g., RISC-V) Assembler Machine Language Program (RISC-V) Machine Interpretation Hardware Architecture Description (e.g., block diagrams) Architecture Implementation Logic Circuit Description (Circuit Schematic Diagrams) temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; lw t0, 0(s2) lw t1, 4(s2) sw t1, 0(s2) sw t0, 4(s2) Current Focus Anything can be represented as a number, i.e., data or instructions 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 2 Introduction to C The Universal Assembly Language Languages used in 61C: C Assembly Python used in two labs Prior programming experience helpful, e.g. Java C++ 28 Survey Hello World C Who has never written a program in Java or C, C++, Objective-C? Java 29 30 5

Compilation & Running $ gcc HelloWorld.c $./a.out Hello World! $ 31 C Compilation Simplified Overview (more later in course) foo.c Compiler foo.o Linker a.out bar.c Compiler bar.o C source files (text) Machine code object files lib.o Pre-built object file libraries Machine code executable file Compiler/assembler combined here 32 Compilation versus Interpretation C (compiled) Compiler (& linker) translates source into machine language Machine language program is loaded by OS and directly executed by the hardware Python (interpreted) Interpreter is written in some high-level language (e.g. C) and translated into machine language Loaded by OS and directly executed by processor Interpreter reads source code (e.g. Python) and interprets it 33 Java Byte Code Java compiler (javac) translates source to byte code Byte code is a particular assembly language - Just like i86, RISC-V, ARM, - Can be directly executed by appropriate machine implementations exist(ed), not commercially successful - More typically, byte code is interpreted on target machine (e.g. i86) by java program compiled to target machine code (e.g. by JIT) - Program runs on any computer with a byte code interpreter (more or less) 34 Compilation Excellent run-time performance: - Much faster than interpreted code (e.g. Python) - Usually faster than Java (even with JIT) Note: Computers only run machine code - Compiled application program, or - Interpreter (that interprets source code) Compilation: Disadvantages Compiled files, including the executable, are - architecture-specific, depending on processor type e.g., RISC-V vs. ARM - and the operating system e.g., Windows vs. Linux Executable must be rebuilt on each new system - I.e., porting your code to a new architecture Change Compile Run [repeat] iteration cycle can be slow during development - Recompile only parts of program that have changed - Tools (e.g. make) automate this 35 36 6

8/29/1 C Dialects C Pre-Processor (CPP) $ gcc x.c foo.c $ gcc ansi Wpedantic x.c 3 38 Administratrivia Numbers wrap-up This is not on the exam! Break C Primer Administrivia, Break C Type declarations And in Conclusion, Compiler C source files pass through macro processor, CPP, before compilation CPP replaces comments with a single space CPP commands begin with # #include file.h /* Inserts file.h */ #include <stdio.h> /* Loads from standard loc */ #define M_PI (3.14159) /* Define constant */ #if/#endif /* Conditional inclusion of text */ Use save-temps option to gcc to see result of preprocessing Full documentation at: http://gcc.gnu.org/onlinedocs/cpp/ foo.i CPP Notify us of all known variances - DSP special accommodations: Email head TA (Steven Ho) - Exam conflicts: Fill out the form on Piazza (special accommodations can be made only in exceptional cases) - If you cannot get an instructional account, email head TA Notify head TA by Tuesday, 9/5/201 - see http://www-inst.eecs.berkeley.edu/~cs61c/fa1/ 39 Break! 40 Numbers wrap-up This is not on the exam! Break C Primer Administrivia, Break C Type declarations And in Conclusion, 8/29/1 Fall 201 - Lecture #2 41 42

int a = 4; float f = 1.38e; char c = x ; Typed Variables in C Declare before use Type cannot change Like Java Type Description Examples int integers, positive or negative 0, 82, -, 0xAB8 unsigned int ditto, no negatives 0, 8, 3 float (single precision) floating point 3.2, -.9e-10 char text character or symbol x, F,? double high precision/range float 1.3e100 long integer with more bits 42943 Constants and Enummerations in C Constants - Assigned in typed declaration, cannot change - E.g. const float pi = 3.1415; const unsigned long addr = 0xaf460; Enumerations 43 44 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 - integer type that target processor works with most efficiently Only guarantee: - sizeof(long long) sizeof(long) sizeof(int) sizeof(short) - Also, short >= 16 bits, long >= 32 bits - All could be 64 bits Impacts portability between architectures 45 Variable Sizes: Machine Dependent! sizeof... (bytes) char: 1 short: 2 int: 4 unsigned int: 4 long: 8 long long: 8 float: 4 double: 8 46 Boolean No boolean datatype in C - Declare if you wish: typedef int boolean; const boolean false = 0; const boolean true = 1; What evaluates to FALSE in C? - 0 (integer) - NULL (a special kind of pointer: more on this later) What evaluates to TRUE in C? - Anything that isn t false is true - Similar to Python: only 0 s or empty sequences are false, everything else is true! 4 int number_of_people() { } return 3; void news() { } printf( no news ); int sum(int x, int y) { } return x + y; Functions in C Like Java Declare return & argument types void for no value returned Functions MUST be declared before they are used 48 8

Code Uninitialized Variables Output $ gcc test.c $./a.out x = 0 x = 1680 x = -4 x = 28245249 x = -16 Struct s in C Struct s are structured groups of variables A bit like Java classes, but no methods E.g. 49 50 More C And In Conclusion, Lecture does not cover C completely - You ll still need your C reference for this course - K&R, The C Programming Language - & other references on the course website Next few lectures focus: - Pointers & Arrays - Memory management Signed integers represented in 2 s complement C Programming Language - Popular (still!) - Similar to Java, but no classes explicit pointers (next lecture) - Beware variables not initialized variable size (# of bits) is machine & compiler dependent C is compiled to machine code - Unlike Python or Java, which are interpreted - Compilation is faster than interpretation 51 52 9