Vladimir Stojanovic & Nicholas Weaver

Slides:



Advertisements
Similar presentations
Chapter 11 Introduction to Programming in C
Advertisements

Assembly Language for Intel-Based Computers, 4 th Edition Chapter 1: Basic Concepts (c) Pearson Education, All rights reserved. You may modify and.
4/23/2015Engineering Problem Solving with C++ second edition, J. ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 1.
General information Course web page: html Office hours:- Prof. Eyal.
Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
1 Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
CSCE 121, Sec 200, 507, 508 Fall 2010 Prof. Jennifer L. Welch.
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
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#1) By Dr. Syed Noman.
CEG 320/520: Computer Organization and Assembly Language Programming1 CEG 320/520 Computer Organization and Assembly Language Programming.
1 Chapter-01 Introduction to Computers and C++ Programming.
1 CS/COE0447 Computer Organization & Assembly Language Pre-Chapter 2.
Chapter 1: Basic Concepts
Computer Systems Organization CS 1428 Foundations of Computer Science.
Programming With C.
CPU Internal memory I/O interface circuit System bus
Arithmetic Logic Unit (ALU) Anna Kurek CS 147 Spring 2008.
 Lecture 2 Processor Organization  Control needs to have the  Ability to fetch instructions from memory  Logic and means to control instruction sequencing.
Processor Structure and Function Chapter8:. CPU Structure  CPU must:  Fetch instructions –Read instruction from memory  Interpret instructions –Instruction.
Simple ALU How to perform this C language integer operation in the computer C=A+B; ? The arithmetic/logic unit (ALU) of a processor performs integer arithmetic.
CS 61C: Great Ideas in Computer Architecture Finite State Machines, Functional Units 1 Instructors: Vladimir Stojanovic and Nicholas Weaver
New-School Machine Structures Parallel Requests Assigned to computer e.g., Search “Katz” Parallel Threads Assigned to core e.g., Lookup, Ads Parallel Instructions.
1 Chapter 1: Introduction Appendix A: Binary and Hexadecimal Tutorial Assembly Language for Intel-Based Computers, 3rd edition Kip R. Irvine.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
1 Chapter 1: Basic Concepts Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine 9/6/2003.
Unit 1 Introduction Number Systems and Conversion.
MIPS Arithmetic is 32 bits
Computer Architecture & Operations I
DDC 2223 SYSTEM SOFTWARE DDC2223 SYSTEM SOFTWARE.
Microprocessor and Microcontroller Fundamentals
The Machine Model Memory
CS/COE 0447 (term 2181) Jarrett Billingsley
ECE 103 Engineering Programming Chapter 5 Programming Languages
Engineering Problem Solving With C An Object Based Approach
Assembly Language (CSW 353)
Control Unit Lecture 6.
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
Microprocessor Systems Design I
Data Representation in Computer Systems
Microprocessor Systems Design I
A Closer Look at Instruction Set Architectures
March 2006 Saeid Nooshabadi
The University of Adelaide, School of Computer Science
Choice of Programming Language
Binary Numbers Material on Data Representation can be found in Chapter 2 of Computer Architecture (Nicholas Carter) CSC 370 (Blum)
Computer Science I CSC 135.
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
Chapter 11 Introduction to Programming in C
CSCE Fall 2013 Prof. Jennifer L. Welch.
Programming Languages
The Von Neumann Model Basic components Instruction processing
CSCE 121: Simple Computer Model Spring 2015
Chapter 11 Introduction to Programming in C
CS 110 Computer Architecture (a. k. a
A primer on Computers and Programs
CS/COE0447 Computer Organization & Assembly Language
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Prof. Yuanqing Cheng CS 61C: Great Ideas in Computer Architecture Lecture 2: Numbers & C Language Prof. Yuanqing Cheng.
Recall: ROM example Here are three functions, V2V1V0, implemented with an 8 x 3 ROM. Blue crosses (X) indicate connections between decoder outputs and.
CSCE Fall 2012 Prof. Jennifer L. Welch.
Chapter 11 Introduction to Programming in C
Introduction to Computer Programming
Introduction to Microprocessor Programming
Instructions in Machine Language
CISC101 Reminders Labs start this week. Meet your TA! Get help with:
Computer Architecture
Review In last lecture, done with unsigned and signed number representation. Introduced how to represent real numbers in float format.
Part I Data Representation and 8086 Microprocessors
CS/COE0447 Computer Organization & Assembly Language
Presentation transcript:

Vladimir Stojanovic & Nicholas Weaver CS 61C: Great Ideas in Computer Architecture Lecture 2: Introduction to C, Part I Instructors: Vladimir Stojanovic & Nicholas Weaver http://inst.eecs.berkeley.edu/~cs61c/

Agenda Everything is a Number Computer Organization Compile vs. Interpret

Key Concepts Inside computers, everything is a number But numbers usually stored with a fixed size 8-bit bytes, 16-bit half words, 32-bit words, 64-bit double words, … Integer and floating-point operations can lead to results too big/small to store within their representations: overflow/underflow

Number Representation Value of i-th digit is d × Basei where i starts at 0 and increases from right to left: 12310 = 110 x 10102 + 210 x 10101 + 310 x 10100 = 1x10010 + 2x1010 + 3x110 = 10010 + 2010 + 310 = 12310 Binary (Base 2), Hexadecimal (Base 16), Decimal (Base 10) different ways to represent an integer We’ll use 1two, 5ten, 10hex to be clearer (vs. 12, 48, 510, 1016 )

Number Representation Hexadecimal digits: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F FFFhex = 15tenx 16ten2 + 15tenx 16ten1 + 15tenx 16ten0 = 3840ten + 240ten + 15ten = 4095ten 1111 1111 1111two = FFFhex = 4095ten May put blanks every group of binary, octal, or hexadecimal digits to make it easier to parse, like commas in decimal

Signed and Unsigned Integers C, C++, and Java have signed integers, e.g., 7, -255: int x, y, z; C, C++ also have unsigned integers, which are used for addresses 32-bit word can represent 232 binary numbers Unsigned integers in 32 bit word represent 0 to 232-1 (4,294,967,295)

Unsigned Integers 0000 0000 0000 0000 0000 0000 0000 0000two = 0ten 0000 0000 0000 0000 0000 0000 0000 0001two = 1ten 0000 0000 0000 0000 0000 0000 0000 0010two = 2ten ... ... 0111 1111 1111 1111 1111 1111 1111 1101two = 2,147,483,645ten 0111 1111 1111 1111 1111 1111 1111 1110two = 2,147,483,646ten 0111 1111 1111 1111 1111 1111 1111 1111two = 2,147,483,647ten 1000 0000 0000 0000 0000 0000 0000 0000two = 2,147,483,648ten 1000 0000 0000 0000 0000 0000 0000 0001two = 2,147,483,649ten 1000 0000 0000 0000 0000 0000 0000 0010two = 2,147,483,650ten ... ... 1111 1111 1111 1111 1111 1111 1111 1101two = 4,294,967,293ten 1111 1111 1111 1111 1111 1111 1111 1110two = 4,294,967,294ten 1111 1111 1111 1111 1111 1111 1111 1111two = 4,294,967,295ten

Signed Integers and Two’s-Complement Representation Signed integers in C; want ½ numbers <0, want ½ numbers >0, and want one 0 Two’s complement treats 0 as positive, so 32-bit word represents 232 integers from -231 (–2,147,483,648) to 231-1 (2,147,483,647) Note: one negative number with no positive version Book lists some other options, all of which are worse Every computer uses two’s complement today Most-significant bit (leftmost) is the sign bit, since 0 means positive (including 0), 1 means negative Bit 31 is most significant, bit 0 is least significant

Two’s-Complement Integers Sign Bit 0000 0000 0000 0000 0000 0000 0000 0000two = 0ten 0000 0000 0000 0000 0000 0000 0000 0001two = 1ten 0000 0000 0000 0000 0000 0000 0000 0010two = 2ten ... ... 0111 1111 1111 1111 1111 1111 1111 1101two = 2,147,483,645ten 0111 1111 1111 1111 1111 1111 1111 1110two = 2,147,483,646ten 0111 1111 1111 1111 1111 1111 1111 1111two = 2,147,483,647ten 1000 0000 0000 0000 0000 0000 0000 0000two = –2,147,483,648ten 1000 0000 0000 0000 0000 0000 0000 0001two = –2,147,483,647ten 1000 0000 0000 0000 0000 0000 0000 0010two = –2,147,483,646ten 1111 1111 1111 1111 1111 1111 1111 1101two = –3ten 1111 1111 1111 1111 1111 1111 1111 1110two = –2ten 1111 1111 1111 1111 1111 1111 1111 1111two = –1ten 11/12/2018

Ways to Make Two’s Complement For N-bit word, complement to 2tenN For 4 bit number 3ten=0011two, two’s complement (i.e. -3ten) would be 16ten-3ten=13ten or 10000two – 0011two = 1101two Here is an easier way: Invert all bits and add 1 Computers actually do it like this, too 0011two 1100two + 1two 3ten 1101two Bitwise complement -3ten

Binary Addition Example Carry 1 3 +2 0011 0010 1 5 1

Two’s-Complement Examples Assume for simplicity 4 bit width, -8 to +7 represented 3 +2 0011 0010 3 + (-2) 0011 1110 -3 + (-2) 1101 1110 5 0101 1 1 0001 -5 1 1011 Overflow when magnitude of result too big to fit into result representation Carry into MSB = Carry Out MSB 7 +1 0111 0001 -8 + (-1) 1000 1111 -8 1000 +7 1 0111 Overflow! Carry into MSB = Carry Out MSB Overflow! Carry in = carry from less significant bits Carry out = carry to more significant bits

Suppose we had a 5-bit word Suppose we had a 5-bit word. What integers can be represented in two’s complement? -32 to +31 ☐ VertLeftWhiteCheck1 B: Print 4 …because ints in this system are 4-bytes long and the actual address increments by 4 even though it appears to only incrememt 1. 0 to +31 ☐ -16 to +15 ☐ -15 to +16 ☐

Suppose we had a 5 bit word Suppose we had a 5 bit word. What integers can be represented in two’s complement? -32 to +31 ☐ VertLeftWhiteCheck1 B: Print 4 …because ints in this system are 4-bytes long and the actual address increments by 4 even though it appears to only incrememt 1. 0 to +31 ☐ -16 to +15 ☐ -15 to +16 ☐

Agenda Everything is a Number Computer Organization Compile vs. Interpret

ENIAC (U.Penn., 1946) First Electronic General-Purpose Computer Blazingly fast (multiply in 2.8ms!) 10 decimal digits x 10 decimal digits But needed 2-3 days to setup new program, as programmed with patch cords and switches -Electronic Numerical Integrator and Computer -Digital (electronic), capable of being reprogrammed to solve a variety of mathematical problems -Dev began at the end of WWII, was designed and primarily used to calculate artillery firing tables for the US Army’s -Very slow to program -Story about 2 architects who designed it and Von Neumann, how it led to EDSAC -Slow program entry, I/O

EDSAC (Cambridge, 1949) First General Stored-Program Computer Programs held as numbers in memory 35-bit binary 2’s complement words Programming EDSAC

Components of a Computer Memory Processor Control Datapath Input Enable? Read/Write Address Write Data ReadData Processor-Memory Interface Bytes Program 7:00 PC Registers Arithmetic & Logic Unit (ALU) Data Output I/O-Memory Interfaces

Great Idea: Levels of Representation/Interpretation temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; High Level Language Program (e.g., C) We are here! Compiler lw $t0, 0($2) lw $t1, 4($2) sw $t1, 0($2) sw $t0, 4($2) Anything can be represented as a number, i.e., data or instructions Assembly Language Program (e.g., MIPS) Assembler 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 Machine Language Program (MIPS) Machine Interpretation 9:00 -Programming EDSAC in binary Hardware Architecture Description (e.g., block diagrams) Architecture Implementation Logic Circuit Description (Circuit Schematic Diagrams)

Introduction to C “The Universal Assembly Language” “Some” experience is required before CS61C C++ or Java OK Class pre-req included classes teaching Java Python used in two labs C used for everything else

Language Poll! I have programmed in C, C++, C#, or Objective-C Please raise hand 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 ☐ None of the above ☐

Intro to C C is not a “very high-level” language, nor a “big” one, and is not specialized to any particular area of application. But its absence of restrictions and its generality make it more convenient and effective for many tasks than supposedly more powerful languages. Kernighan and Ritchie Enabled first operating system not written in assembly language: UNIX - A portable OS!

Intro to C Why C?: we can write programs that allow us to exploit underlying features of the architecture – memory management, special instructions, parallelism C and derivatives (C++/Obj-C/C#) still one of the most popular application programming languages after >40 years!

TIOBE Index of Language Popularity http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html The ratings are based on the number of skilled engineers world-wide, courses and third party vendors. http://www.tiobe.com

TIOBE Programming Community Index

Disclaimer 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 Check online for more sources “JAVA in a Nutshell,” O’Reilly Chapter 2, “How Java Differs from C” http://oreilly.com/catalog/javanut/excerpt/index.html Brian Harvey’s helpful transition notes On CS61C class website: pages 3-19 http://inst.eecs.berkeley.edu/~cs61c/resources/HarveyNotesC1-3.pdf Key C concepts: Pointers, Arrays, Implications for Memory management

Agenda Everything is a Number Computer Organization Compile vs. Interpret

Compilation: 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 instructions (“levels of interpretation”) 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 automatically, by default); we’ll talk about that later

C Compilation Simplified Overview (more later in course) foo.c bar.c C source files (text) Compiler Compiler Compiler/assembler combined here foo.o bar.o Machine code object files Linker lib.o Pre-built object file libraries a.out Machine code executable file

Compilation: Advantages Excellent run-time performance: generally much faster than Scheme or Java for comparable code (because it optimizes for a given architecture) Reasonable compilation time: enhancements in compilation procedure (Makefiles) allow only modified files to be recompiled

Compilation: Disadvantages Compiled files, including the executable, are architecture-specific, depending on processor type (e.g., MIPS vs. RISC-V) 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 but Make tool only rebuilds changed pieces, and can do compiles in parallel (linker is sequential though -> Amdahl’s Law) 28:07

C Pre-Processor (CPP) foo.c foo.i Compiler CPP C source files first pass through macro processor, CPP, before compiler sees code CPP replaces comments with a single space CPP commands begin with “#” #include “file.h” /* Inserts file.h into output */ #include <stdio.h> /* Looks for file in standard location */ #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/ show this on term