Introduction to Computing Systems · inputs: data, description of computation (other TMs) U a,b,c...

22
1 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 CS270 – Spring 2012 - Colorado State University Introduction to Computing Systems: From Bits and Gates to C and Beyond 2 nd Edition Yale N. Patt Sanjay J. Patel Original slides from Gregory Byrd, North Carolina State University Modified by Chris Wilcox, S. Rajopadhye Colorado State University Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 2 CS270 – Spring 2012 - Colorado State University Lecture Goals Review course logistics Assignments Policies Organization Grading Criteria Introduce key concepts Role of Abstraction Software versus Hardware Universal Computing Devices Layered Model of Computing

Transcript of Introduction to Computing Systems · inputs: data, description of computation (other TMs) U a,b,c...

Page 1: Introduction to Computing Systems · inputs: data, description of computation (other TMs) U a,b,c c(a+b) Universal Turing Machine T add, T mul Universal machine is programmable –

1

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

1 CS270 – Spring 2012 - Colorado State University

Introduction to Computing Systems: From Bits and Gates to C and Beyond

2nd Edition

Yale N. Patt Sanjay J. Patel

Original slides from Gregory Byrd, North Carolina State University

Modified by Chris Wilcox, S. Rajopadhye Colorado State University

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

2 CS270 – Spring 2012 - Colorado State University

Lecture Goals

! Review course logistics n  Assignments n  Policies n  Organization n  Grading Criteria

! Introduce key concepts n  Role of Abstraction n  Software versus Hardware n  Universal Computing Devices n  Layered Model of Computing

Page 2: Introduction to Computing Systems · inputs: data, description of computation (other TMs) U a,b,c c(a+b) Universal Turing Machine T add, T mul Universal machine is programmable –

2

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

3 CS270 – Spring 2012 - Colorado State University

Logistics ! Lectures: Tue and Thu from 9:30-10:45 in

Glover 130 ! Recitations: Tue 11:00-11:50, 1:00-1:50, Wed

11:00-11:50 in CSB 215 ! Exams:

n  One midterm in class (1 page notes, no calculator, no electronic aid)

n  final on Wednesday May 9 6:20-8:20 in class (same rules)

! Materials on the website and RamCT: n  http://www.cs.colostate.edu/~cs270 n  http://ramct.colostate.edu

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

4 CS270 – Spring 2012 - Colorado State University

Name cards ! Fold your card LENGTHWISE so that it can

be placed on your desk

! Neatly write your name (how you want to be addressed) in BIG, BOLD LETTERS, using the markers that are circulating

! Prop it up on your desk so that it can be seen from the front of the classroom

Page 3: Introduction to Computing Systems · inputs: data, description of computation (other TMs) U a,b,c c(a+b) Universal Turing Machine T add, T mul Universal machine is programmable –

3

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

5 CS270 – Spring 2012 - Colorado State University

Instructor ! Name: Sanjay Rajopadhye ! Pronunciation (optional): In Indian names, “a” is

almost always pronounced as a short “u” sound as in gun, fun, etc., or a long “aa” sound as in calm, bard, etc.

! Sanjay is pronounced as Sun-juy ! Rajopadhye Raaj-Oh-paath-yay (in the “paath” make

the t sound like a d). Don’t worry if you don’t get it right, it’s almost always mispronounced, even in India).

! These pronunciation rules are used in many parts of Asia, e.g., pronounce “Bagdad?”

! Interesting fact: Everyone in my family was born in a different country

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

6 CS270 – Spring 2012 - Colorado State University

TeachingAssistant ! Name: Nirmal Prajapati ! Call me: Nirmal (like normal, but with an i)

! Pronunciation (optional): nearly normal Nirmal ! Major: Computer Science (Ph.D., 1st year)

! Interesting fact:

Page 4: Introduction to Computing Systems · inputs: data, description of computation (other TMs) U a,b,c c(a+b) Universal Turing Machine T add, T mul Universal machine is programmable –

4

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

7 CS270 – Spring 2012 - Colorado State University

Assignments Assignments and quizzes are posted on RamCT:

! Weekly assignments combination of written and programming assignments

! Written (hardcopy) assignments are due Thursday at 9:30 pm (start of class)

! Programming assignments are submitted in electronic form Mondays at 5:00 PM.

! Reading Quizzes are online, the due date is Monday at 11:59pm

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

8 CS270 – Spring 2012 - Colorado State University

Policies ! Grading Criteria

n  Homework Assignments (45%) n  Reading Quizzes (5%) n  Recitations (5%) n  Midterm Exam (20%) n  Final Exam (25%)

! Late Policy n  Accepted up to 48 hours after posted date with

10% penalty per 24 hrs delay n  Not accepted after 48 hr delay

! Academic Integrity n  http://www.cs.colostate.edu/~info/student-info.html n  Be smart about Internet resources

Page 5: Introduction to Computing Systems · inputs: data, description of computation (other TMs) U a,b,c c(a+b) Universal Turing Machine T add, T mul Universal machine is programmable –

5

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

9 CS270 – Spring 2012 - Colorado State University

Organization ! 1/3 computer hardware: numbers and bits,

transistors, gates, digital logic, state machines, von Neumann model, instruction sets, LC-3 architecture

! 1/3 assembly code: instruction formats, branching, control, LC-3 programming, I/O, subroutines, memory model

! 1/3 C programming: data types, language syntax, variables and operators, control structures, functions, pointers and arrays, memory model, recursion, I/O, data structures

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

10 CS270 – Spring 2012 - Colorado State University

Grading Criteria How to be successful in this class: 1)  Attend all classes and recitations, information

will presented that you can’t get anywhere else.

2)  Do all the homework assignments, ask questions (early!) if you run into trouble.

3)  Read the textbook, take the quizzes, work through the end of chapter problems.

4)  Use office hours

Page 6: Introduction to Computing Systems · inputs: data, description of computation (other TMs) U a,b,c c(a+b) Universal Turing Machine T add, T mul Universal machine is programmable –

6

Chapter 1 Welcome Aboard

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

12 CS270 – Spring 2012 - Colorado State University

Introduction to the World of Computing

! Computer: electronic genius? n  NO! Electronic idiot! n  Does exactly what we tell it to, nothing more.

! Goal of the course: n  You will be able to understand how computers are

built, and write programs in C and understand what’s going on underneath.

! Approach: n  Build understanding from the bottom up. n  Bits ➨ Transistors ➨ Gates ➨ Logic ➨ Processor ➨

Instructions ➨ Assembly Code ➨ C Programming

Page 7: Introduction to Computing Systems · inputs: data, description of computation (other TMs) U a,b,c c(a+b) Universal Turing Machine T add, T mul Universal machine is programmable –

7

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

13 CS270 – Spring 2012 - Colorado State University

Two Recurring Themes

! Abstraction n  Productivity enhancer – don’t need to worry about

details… Can drive a car without knowing how the internal combustion engine works.

n  …until something goes wrong! Where’s the dipstick? What’s a spark plug?

n  Important to understand the components and how they work together.

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

14 CS270 – Spring 2012 - Colorado State University

Two Recurring Themes

! Hardware vs. Software n  It’s not either/or – both are components of a

computer system that cooperate. n  Even if you specialize in one, you should understand

capabilities and limitations of both. n  The best programmers understand the computer

systems which run their programs. n  Computers are an entire ecosystem with multiple

levels of abstraction.

Page 8: Introduction to Computing Systems · inputs: data, description of computation (other TMs) U a,b,c c(a+b) Universal Turing Machine T add, T mul Universal machine is programmable –

8

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

15 CS270 – Spring 2012 - Colorado State University

Big Idea #1: Universal Computing Devices

! All computers, given enough time and memory, are capable of computing exactly the same things.

= = PDA

Workstation Supercomputer

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

16 CS270 – Spring 2012 - Colorado State University

Turing Machine

! Mathematical model of a device that can perform any computation – Alan Turing (1937) n  ability to read/write symbols on an infinite “tape” n  state transitions, based on current state and symbol

! Every computation can be performed by some Turing machine. (Turing’s thesis)

Tadd a,b a+b

Turing machine that adds

Tmul a,b ab

Turing machine that multiplies For more info about Turing machines, see http://www.wikipedia.org/wiki/Turing_machine/

For more about Alan Turing, see http://www.turing.org.uk/turing/

Page 9: Introduction to Computing Systems · inputs: data, description of computation (other TMs) U a,b,c c(a+b) Universal Turing Machine T add, T mul Universal machine is programmable –

9

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

17 CS270 – Spring 2012 - Colorado State University

Universal Turing Machine

! A machine that can implement all Turing machines -- this is also a Turing machine! n  inputs: data, description of computation (other TMs)

U a,b,c c(a+b)

Universal Turing Machine

Tadd, Tmul

Universal machine is programmable – so is a computer! •  instructions are part of the input data •  a computer can emulate a Universal Turing Machine

A computer is a universal computing device.

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

18 CS270 – Spring 2012 - Colorado State University

From Theory to Practice ! In theory, computer can compute anything ! that’s possible to compute

n  given enough memory and time

! In practice, solving problems involves computing under constraints. n  time

! weather forecast, next frame of animation, ...

n  cost ! cell phone, automotive engine controller, ...

n  power ! cell phone, handheld video game, ...

Page 10: Introduction to Computing Systems · inputs: data, description of computation (other TMs) U a,b,c c(a+b) Universal Turing Machine T add, T mul Universal machine is programmable –

10

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

19 CS270 – Spring 2012 - Colorado State University

Big Idea #2: Transformations Between Layers

Problems

Language

Instruction Set Architecture

Microarchitecture

Circuits

Devices

Algorithms

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

20 CS270 – Spring 2012 - Colorado State University

How do we solve a problem using a computer?

! A systematic sequence of transformations between layers of abstraction.

Problem

Algorithm

Program

Software Design: choose algorithms and data structures

Programming: use language to express design

Instr Set Architecture

Compiling/Interpreting: convert language to machine instructions

Page 11: Introduction to Computing Systems · inputs: data, description of computation (other TMs) U a,b,c c(a+b) Universal Turing Machine T add, T mul Universal machine is programmable –

11

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

21 CS270 – Spring 2012 - Colorado State University

Deeper and Deeper…

Instr Set Architecture

Microarch

Circuits

Processor Design: choose structures to implement ISA

Logic/Circuit Design: gates and low-level circuits to implement components

Devices

Process Engineering & Fabrication: develop and manufacture lowest-level components

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

22 CS270 – Spring 2012 - Colorado State University

Descriptions of Each Level ! Problem Statement

n  stated using "natural language" n  may be ambiguous, imprecise

! Algorithm n  step-by-step procedure, guaranteed to finish n  definiteness, effective computability, finiteness

! Program n  express the algorithm using a computer language n  high-level language, low-level language

! Instruction Set Architecture (ISA) n  specifies the set of instructions the computer can perform n  data types, addressing mode

Page 12: Introduction to Computing Systems · inputs: data, description of computation (other TMs) U a,b,c c(a+b) Universal Turing Machine T add, T mul Universal machine is programmable –

12

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

23 CS270 – Spring 2012 - Colorado State University

Descriptions of Each Level (cont.)

! Microarchitecture n  detailed organization of a processor implementation n  different implementations of a single ISA

! Logic Circuits n  combine basic operations to realize microarchitecture n  many different ways to implement a single function

(e.g., addition) ! Devices

n  properties of materials, manufacturability

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

24 CS270 – Spring 2012 - Colorado State University

Many Choices at Each Level Solve a system of equations

Gaussian elimination

Jacobi iteration Red-black SOR Multigrid

FORTRAN C C++ Java

Intel x86 PowerPC Atmel AVR

Centrino Pentium 4 Xeon

Ripple-carry adder Carry-lookahead adder

CMOS Bipolar GaAs

Tradeoffs: cost speed power (etc.)

Page 13: Introduction to Computing Systems · inputs: data, description of computation (other TMs) U a,b,c c(a+b) Universal Turing Machine T add, T mul Universal machine is programmable –

13

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

25 CS270 – Spring 2012 - Colorado State University

Book Outline ! Bits and Bytes

n  How do we represent information using electrical signals? ! Digital Logic

n  How do we build circuits to process information? ! Processor and Instruction Set

n  How do we build a processor out of logic elements? n  What operations (instructions) will we implement?

! Assembly Language Programming n  How do we use processor instructions to implement algorithms? n  How do we write modular, reusable code? (subroutines)

! I/O, Traps, and Interrupts n  How does processor communicate with outside world?

! C Programming n  How do we write programs in C?

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

26 CS270 – Spring 2012 - Colorado State University

Our Outline (CSU tweak) ! First C programming

n  You already know how to program in Java n  Learning C will be much faster than the book’s speed (Ch 11-13)

! Follow the text sequentially n  Digital Logic n  Processor & Instruction Set n  Assembly Language Programming n  I/O, Traps, Interrupts n  Functions, procedures, activations/frames, etc.

! Write a LC3 simulator and LC3 assembler in C n  Reinforcement of ideas n  Second look at the same ideas

Page 14: Introduction to Computing Systems · inputs: data, description of computation (other TMs) U a,b,c c(a+b) Universal Turing Machine T add, T mul Universal machine is programmable –

14

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

27 CS270 – Spring 2012 - Colorado State University

How to succeed in this class 1)  Most university classes require two to three

times the contact hours. 2)  So a 15 credit student should spend 60 hours

in school related activity. 1)  Yeah right!!! 2)  BUT IT’S TRUE ABOUT THIS CLASS

3)  Time management: 1)  30 mins – Quiz 2)  4 hrs – contact hours 3)  30 mins – review class notes (15 x 2) 4)  8-11 hrs – assignments

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

28 CS270 – Spring 2012 - Colorado State University

Before next lecture Read the notes at:

http://www.cs.colostate.edu/~cs270/.Spring12/Notes/NumberSystems.pdf

Spend 1 hour on it.

Page 15: Introduction to Computing Systems · inputs: data, description of computation (other TMs) U a,b,c c(a+b) Universal Turing Machine T add, T mul Universal machine is programmable –

15

Chapter 2 Bits, Data Types, and Operations

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

30 CS270 – Spring 2012 - Colorado State University

How do we represent data in a computer?

! At the lowest level, a computer is an electronic machine.

n  works by controlling the flow of electrons ! Easy to recognize two conditions:

1.  presence of a voltage – we’ll call this state “1” 2.  absence of a voltage – we’ll call this state “0”

! Could base state on value of voltage, but control and detection circuits more complex.

n  compare turning on a light switch to measuring or regulating voltage

Page 16: Introduction to Computing Systems · inputs: data, description of computation (other TMs) U a,b,c c(a+b) Universal Turing Machine T add, T mul Universal machine is programmable –

16

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

31 CS270 – Spring 2012 - Colorado State University

Computer is a binary digital system.

! Basic unit of information is the binary digit, or bit. ! Values with >2 states require multiple bits.

n  A collection of two bits has four possible states: 00, 01, 10, 11

n  A collection of three bits has eight possible states: 000, 001, 010, 011, 100, 101, 110, 111

n  A collection of n bits has 2n possible states.

Binary (base two) system: •  has two states: 0 and 1

Digital system: •  finite number of symbols

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

32 CS270 – Spring 2012 - Colorado State University

What kinds of data do we need to represent?

n  Numbers – signed, unsigned, integers, floating point, complex, rational, irrational, …

n  Text – characters, strings, … n  Logical – true, false n  Images – pixels, colors, shapes, … n  Sound – waveforms n  Instructions n  …

! Data type: n  representation and operations within the computer

! We’ll start with numbers…

Page 17: Introduction to Computing Systems · inputs: data, description of computation (other TMs) U a,b,c c(a+b) Universal Turing Machine T add, T mul Universal machine is programmable –

17

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

33 CS270 – Spring 2012 - Colorado State University

Unsigned Integers

! Non-positional notation n  could represent a number (“5”) with a string of ones

(“11111”) n  problems?

! Weighted positional notation n  like decimal numbers: “329” n  “3” is worth 300, because of its position, while “9” is only

worth 9

329 102 101 100

101 22 21 20

3x100 + 2x10 + 9x1 = 329 1x4 + 0x2 + 1x1 = 5

most significant

least significant

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Seen on a geek T Shirt

! There are only 10 kinds of people

! Those that know binary

! Those that don’t

34 CS270 – Spring 2012 - Colorado State University

Page 18: Introduction to Computing Systems · inputs: data, description of computation (other TMs) U a,b,c c(a+b) Universal Turing Machine T add, T mul Universal machine is programmable –

18

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

35 CS270 – Spring 2012 - Colorado State University

Unsigned Integers (cont.) ! An n-bit unsigned integer represents 2n values:

from 0 to 2n-1.

22 21 20

0 0 0 0 0 0 1 1 0 1 0 2 0 1 1 3 1 0 0 4 1 0 1 5 1 1 0 6 1 1 1 7

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

36 CS270 – Spring 2012 - Colorado State University

Unsigned Binary Arithmetic

! Base-2 addition – just like base-10! n  add from right to left, propagating carry

10010 10010 1111 + 1001 + 1011 + 1

11011 11101 10000

10111 + 111

Subtraction, multiplication, division,…

Page 19: Introduction to Computing Systems · inputs: data, description of computation (other TMs) U a,b,c c(a+b) Universal Turing Machine T add, T mul Universal machine is programmable –

19

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

37 CS270 – Spring 2012 - Colorado State University

Signed Integers ! With n bits, we have 2n distinct values.

n  assign about half to positive integers (1 through 2n-1) n  assign about half to negative (- 2n-1 through -1) n  that leaves two values: one for 0, and one extra

! Positive integers n  just like unsigned – zero in most significant (MS) bit

00101 = 5 ! Negative integers

n  sign-magnitude – set sign bit to show negative 10101 = -5

n  one’s complement – flip every bit to represent negative 11010 = -5

n  in either case, MS bit indicates sign: 0=pos., 1=neg.

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

38 CS270 – Spring 2012 - Colorado State University

Two’s Complement ! Problems with sign-magnitude, 1’s complement

n  two representations of zero (+0 and –0) n  arithmetic circuits are complex

! How to add two sign-magnitude numbers? n  e.g., try 2 + (-3)

! How to add two one’s complement numbers? n  e.g., try 4 + (-3)

Page 20: Introduction to Computing Systems · inputs: data, description of computation (other TMs) U a,b,c c(a+b) Universal Turing Machine T add, T mul Universal machine is programmable –

20

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

39 CS270 – Spring 2012 - Colorado State University

Two’s Complement ! Two’s complement representation developed

to make circuits easy for arithmetic. n  for each positive number (X), assign value to its

negative (-X), such that X + (-X) = 0 with “normal” addition, ignoring carry out

00101 (5) 01001 (9) + 11011 (-5) + (-9)

00000 (0) 00000 (0)

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

40 CS270 – Spring 2012 - Colorado State University

Two’s Complement Representation

! If number is positive or zero, n  normal binary representation, zeroes in upper bit(s)

! If number is negative, n  start with positive number n  flip every bit (i.e., take the one’s complement) n  then add one

00101 (5) 01001 (9) 11010 (1’s comp) (1’s comp)

+ 1 + 1 11011 (-5) (-9)

Page 21: Introduction to Computing Systems · inputs: data, description of computation (other TMs) U a,b,c c(a+b) Universal Turing Machine T add, T mul Universal machine is programmable –

21

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

41 CS270 – Spring 2012 - Colorado State University

Two’s Complement Shortcut

! To take the two’s complement of a number: n  copy bits from right to left until (and including) first “1” n  flip remaining bits to the left

011010000 011010000 100101111 (1’s comp)

+ 1 100110000 100110000

(copy) (flip)

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

42 CS270 – Spring 2012 - Colorado State University

Two’s Complement Signed Integers ! MS bit is sign bit – it has weight –2n-1. ! Range of an n-bit number: -2n-1 through 2n-1 – 1.

n  The most negative number has no positive counterpart.

-23 22 21 20

0 0 0 0 0 0 0 0 1 1 0 0 1 0 2 0 0 1 1 3 0 1 0 0 4 0 1 0 1 5 0 1 1 0 6 0 1 1 1 7

-23 22 21 20

1 0 0 0 -8 1 0 0 1 -7 1 0 1 0 -6 1 0 1 1 -5 1 1 0 0 -4 1 1 0 1 -3 1 1 1 0 -2 1 1 1 1 -1

Page 22: Introduction to Computing Systems · inputs: data, description of computation (other TMs) U a,b,c c(a+b) Universal Turing Machine T add, T mul Universal machine is programmable –

22

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

43 CS270 – Spring 2012 - Colorado State University

Converting Binary (2’s C) to Decimal

1.  If leading bit is one, take two’s complement to get a positive number.

2.  Add powers of 2 that have “1” in the corresponding bit positions.

3.  If original number was negative, add a minus sign.

n 2n

0 1 1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 256 9 512

10 1024

X = 01101000two = 26+25+23 = 64+32+8 = 104ten

Assuming 8-bit 2’s complement numbers.