COMPSCI 210S1C 2014 Computer Systems 1 Introduction

80
COMPSCI 210S1C 2014 Computer Systems 1 Introduction 1

description

COMPSCI 210S1C 2014 Computer Systems 1 Introduction. Lecture 1. Introduction & Layers. Computer Science 210 s1c Computer Systems 1 2014 Semester 1 Lecture Notes. James Goodman (revised by Robert Sheehan). Lecturers Robert Sheehan (week 1-6) Office 303.488 - PowerPoint PPT Presentation

Transcript of COMPSCI 210S1C 2014 Computer Systems 1 Introduction

Page 1: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

COMPSCI 210S1C 2014Computer Systems 1

Introduction

1

Page 2: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

Computer Science 210 s1cComputer Systems 1

2014 Semester 1

Lecture Notes

James Goodman (revised by Robert Sheehan)

Introduction & Layers

Lecture 1

2

Page 3: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

LecturersRobert Sheehan (week 1-6)

Office 303.488 E-mail: [email protected] Office hours: by appointment, after class, or whenever the office door is open

Xinfeng Ye (week 7-12) Office 303.589 E-mail: [email protected] Office hours:

TutorsAhmad [email protected]

Office hours: TBA

Class RepresentativeTBD (volunteers?)

3

Arash [email protected] Office hours: TBA

Page 4: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

ForumWe are using the Cecil discussion list.Check it as often as you want.The tutor/s will be regularly participating.I follow the discussion area of Cecil irregularly and participate as appropriate but

likely not as quickly as the tutors.

TutorsAre available to help

44

Page 5: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

Tutorials

Tutorials are not compulsory but are strongly recommended. All tutorials will start from week. 9:00-10:00, Monday, 303S-191 (N.B. different room) 10:00-11:00, Monday, 303S-G75 13:00-14:00, Monday, 303S-G75 16:00-17:00, Monday, 303S-G75 12:00-13:00, Tuesday, 303S-G75 17:00-18:00, Tuesday, 303S-G75 13:00-14:00, Wednesday, 303S-G75

Tutor's office hours ?

Tutorials will be available onlineLecture recordings in the Knowledge Map area of Cecil

5

Page 6: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

Miscellaneous

Come to class . It is very dangerous to fall behind.Each new lecture will require you to know and understand the

content of the previous lectures. Lectures are tied to the content of the textbook. It is important

to read the textbook – exam questions may require more detail than is covered in the lectures.

Tutorials are a great way to supplement lecturesOutside office hours and tutorials, tutors are not expected to

be at hand

6

Page 7: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

Course OutlineBits and Bytes

How do we represent information using electrical signals?Digital Logic

How do we build circuits to process information?Computing Engines, Processors and Instruction Sets

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

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

I/O, Traps, and Interrupts How does a processor communicate with the outside world?

C Programming How do we write programs in C? How do we implement high-level programming constructs?

7

Page 8: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

Textbook

8

LC-3 simulator

http://highered.mcgraw-hill.com/sites/0072467509/

http://highered.mcgraw-hill.com/sites/0072467509/student_view0/lc-3_simulator.html

Page 9: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

Data Representation

Data Binary Octal Decimal Signed Numbers

Performing Arithmetic Addition Subtraction Shifting (Mul/Div)

Types and Representation Integer Floating point -- IEEE format Alpha-numeric representation

9

Page 10: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

Low-level Processes Introduction Digital logic structures Finite state machine ISA/Memory organisation Opcodes Operate instructions, data movement operations Control instructions (loop, if-then-else control) The Assembly process Input & Output Sub-routines / Stacks Coding examples

Note that tutorials will closely follow the course materials progression offering you a chance to apply new knowledge immediately. This will be very important for both assembly and C.

10

Page 11: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

Basic componentsoData representation

• Binary fraction• Floating point representation

Introduction to COperatorsControl structureFunctionsPointers, arrays, stringI/OAdvanced programming

C programming

11

Page 12: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

Assignments

There will be three assignments. The assignments count 20% of your grade.

Don’t pass your work to someone else; don’t copy some one else’s work. Do not copy other sources.If you are caught you will receive zero for the entire assignment and your previous and future submissions will be scrutinised.

For assembly and C, an assignment not compiling will receive 0 marksSubmissions Deadline: an assignment due date means the assignment should be

turned in by the time specified in the assignment description. The submission dropboxes normally stay open for late assignments but penalties apply.

12

Page 13: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

Test

Tentative: During class on 30 April, Week 7 (28 April – 2 May)Multiple-choice questions (MCQ)Material through week 6See example from previous semestersWorth 20% towards course grade

13

Page 14: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

Exam

Multiple-choice questions (MCQ)See examples from previous semestersWorth 60% towards course grade

14

Page 15: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

How to Do Well in CompSci 210

1. Read the lecture notes after each lecture2. Read the relevant textbook sections

a. To learn moreb. To complement lectures

3. If you have questions or do not understand somethinga. Attend the tutorialsb. Check the forumc. Discuss with other 210 studentsd. Ask a tutor during office hourse. E-mail or see me

4. How to prepare for examsf. Do 1, 2 & 3 aboveg. Do exercises of the course/tutorials/exercises/textbookh. Study previous years’ exams: You can get these online from the library website

15

Page 16: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

Assignments

Assignment Subject Approx date

out Date due % of the final mark

# 1Data

representation/Digital logic/simple machines

10/03/14 27/03/14 5

# 2 Low-level programming 31/03/14 01/05/14 5

# 3 C code 05/05/14 29/05/14 10

16

Page 17: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

17

Introduction to Computing Systems:

From Bits and Gates to C and Beyond2nd EditionYale N. Patt

Sanjay J. Patel

Based on slides originally prepared by Gregory T. Byrd, North Carolina State University

Page 18: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

Chapter 1Welcome Aboard

Page 19: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

19

Introduction to the World of Computing

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

Goal of the course:You will be able to write programs in Cand understand what’s going on underneath – no magic!

Approach:Build understanding from the bottom up.Bits Gates Processor Instructions C Programming

Page 20: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

20

Two Recurring Themes

Abstraction Productivity enhancer – don’t need to worry about details…

Can drive a car without knowing howthe internal combustion engine works.

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

Important to understand the components andhow they work together.

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

system. Even if you specialize in one, it is important to understand

capabilities and limitations of both.

Page 21: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

1-21

Big Idea #1: Universal Computing Device

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

= =Smart phone

DesktopSupercomputer

Page 22: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

Alan Turing (12 Jun 1912 – 7 Jun 1954)

22

Page 23: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

23

Turing Machine

Mathematical model of a device that can performany computation – Alan Turing (1937)

ability to read/write symbols on an infinite “tape” state transitions, based on current state and symbol

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

Tadda,b a+b

Turing machine that adds

Tmula,b ab

Turing machine that multiplies

For more info about Turing machines, seehttp://www.wikipedia.org/wiki/Turing_machine/

Page 24: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

24

Universal Turing Machine

A machine that can implement all Turing machines-- this is also a Turing machine!

inputs: data, plus a description of computation (other TMs)

Ua,b,c c(a+b)

Universal Turing Machine

Tadd, Tmul

U 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.

Page 25: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

25

From Theory to Practice

In theory, a computer can compute anything that’s possible to compute

given enough memory and time

In practice, solving problems involves computing under constraints.

time• weather forecast, next frame of animation, ...

cost• cell phone, automotive engine controller, ...

power• cell phone, handheld video game, ...

Page 26: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

26

Big Idea #2: Transformations Between Layers

Problems

Language

Instruction Set Architecture

Microarchitecture

Circuits

Devices

Algorithms

Page 27: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

27

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 SetArchitecture

Compiling/Interpreting:convert language to machine instructions

Page 28: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

28

Deeper and Deeper…

Instr SetArchitecture

Microarch

Circuits

Processor Design:choose structures to implement ISA

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

Devices

Process Engineering & Fabrication:develop and manufacturelowest-level components

Page 29: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

29

Descriptions of Each Level

Problem Statement stated using "natural language" may be ambiguous, imprecise

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

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

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

Page 30: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

30

Descriptions of Each Level (cont.)

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

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

(e.g., addition)Devices

properties of materials, manufacturability

Page 31: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

31

Many Choices at Each Level

Solve a system of equations

Gaussian elimination

JacobiiterationRed-black SOR Multigrid

FORTRAN C C++ Java

Intel x86ARM NVIDIA

Haswell Nehalem Atom

Ripple-carry adder Carry-lookahead adder

CMOS Bipolar GaAs

Tradeoffs:costperformancepower(etc.)

Page 32: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

32

Course Outline

Bits and Bytes How do we represent information using electrical signals?

Digital Logic How do we build circuits to process information?

Processor and Instruction Set How do we build a processor out of logic elements? What operations (instructions) will we implement?

Assembly Language Programming How do we use processor instructions to implement

algorithms? How do we write modular, reusable code? (subroutines)

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

C Programming How do we write programs in C? How do we implement high-level programming constructs?

Page 33: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

Computer Science 210 s1cComputer Systems 1

2014 Semester 1

Lecture Notes

James Goodman (revised by Robert Sheehan)

Credits: Adapted from slides prepared by Gregory T. Byrd, North Carolina State University

Data RepresentationLecture 2

33

Page 34: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

Chapter 2Bits, Data Types,and Operations

Page 35: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

35

How do we represent data in a computer?

At the lowest level, a computer is an electronic machine. 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. compare turning on a light switch to

measuring or regulating voltage

Page 36: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

36

Computer is a Binary Digital System.

Basic unit of information is the binary digit, or bit.Values with more than two states require multiple

bits. A collection of two bits has four possible states:

00, 01, 10, 11 A collection of three bits has eight possible states:

000, 001, 010, 011, 100, 101, 110, 111 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

Page 37: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

37

What kinds of data do we need to represent?

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

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

Data type: representation and operations within the computer

We’ll start with numbers…

Page 38: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

38

Unsigned Integers

Non-positional notation could represent a number (“5”) with a string of ones (“11111”) problems?

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

9

329102 101 100

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

10122 21 20

mostsignificant

leastsignificant

Page 39: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

39

Unsigned Integers (cont.)

An n-bit unsigned integer represents any of 2n (integer) values:from 0 to 2n-1. 22 21 20 Value

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

Page 40: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

40

Unsigned Binary Arithmetic

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

carry

10010 10010 1111+ 1001 + 1011 + 1

11011 11101 10000

10111+ 111

Subtraction, multiplication, division,…

Page 41: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

41

“There are 10 kinds of people in the world: those who understand binary, and those who don’t.”

Page 42: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

42

Signed Integers

With n bits, we can distinguish 2n unique values assign about half to positive integers (1 through 2n-1)

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

Positive integers just like unsigned, but zero in most significant (MS) bit

00101 = 5Negative integers

Sign-Magnitude (or Signed-Magnitude) – set MS bit to show negative, other bits are the same as unsigned10101 = -5

One’s complement – flip every bit to represent negative11010 = -5

In either case, MS bit indicates sign: 0=positive, 1=negative

Page 43: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

43

Two’s Complement

Problems with sign-magnitude and 1’s complement two representations of zero (+0 and –0) arithmetic circuits are complex

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

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

Two’s complement representation developed to makecircuits easy for arithmetic. 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)

Page 44: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

44

Two’s Complement Representation

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

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

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

+ 1 + 111011 (-5) (-9)

Page 45: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

45

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

The most negative number (-2n-1) 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 46: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

46

“Biased” Representation of Signed IntegersAll integers (positive & negative) are represented

as an unsigned integer supplemented with a “bias” to be subtracted out.

Range of an n-bit number: (0 - bias) through (2n-1 - bias).

Bias 8:23 22 21 20 Bias-8

0 0 0 0 -80 0 0 1 -70 0 1 0 -60 0 1 1 -50 1 0 0 -40 1 0 1 -30 1 1 0 -20 1 1 1 -1

23 22 21 20 Bias-8

1 0 0 0 01 0 0 1 11 0 1 0 21 0 1 1 31 1 0 0 41 1 0 1 51 1 1 0 61 1 1 1 7

Page 47: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

47

“Biased” Representation of Signed IntegersAll integers (positive & negative) are represented

as an unsigned integer supplemented with a “bias” to be subtracted out.

Range of an n-bit number: (0 - bias) through (2n-1 - bias).

Bias 7:23 22 21 20 Bias-7

0 0 0 0 -70 0 0 1 -60 0 1 0 -50 0 1 1 -40 1 0 0 -30 1 0 1 -20 1 1 0 -10 1 1 1 0

23 22 21 20 Bias-7

1 0 0 0 11 0 0 1 21 0 1 0 31 0 1 1 41 1 0 0 51 1 0 1 61 1 1 0 71 1 1 1 8

Page 48: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

48

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 thecorresponding bit positions.

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

n 2n

0 11 22 43 84 165 326 647 1288 256

9 51210 1024

X = 01101000two

= 26+25+23 = 64+32+8= 104ten

Assuming 8-bit 2’s complement numbers.

Page 49: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

49

More Examples

n 2n

0 11 22 43 84 165 326 647 128

8 256

9 512

10 1024Assuming 8-bit 2’s complement numbers.

X = 00100111two

= 25+22+21+20 = 32+4+2+1= 39ten

X = 11100110two

-X = 00011010= 24+23+21 = 16+8+2= 26ten

X = -26ten

Page 50: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

50

Converting Decimal to Binary (2’s C)

First Method: Division1. Find magnitude of decimal number. (Always positive.)2. Divide by two – remainder is least significant bit.3. Keep dividing by two until answer is zero,

writing remainders from right to left.4. Append a zero as the MS bit;

if original number was negative, take two’s complement.

X = 104ten 104/2 = 52 r0 bit 052/2 = 26 r0 bit 126/2 = 13 r0 bit 213/2 = 6 r1 bit 3

6/2 = 3 r0 bit 43/2 = 1 r1 bit 5

X = 01101000two 1/2 = 0 r1 bit 6

Page 51: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

51

Converting Decimal to Binary (2’s C)

Second Method: Subtract Powers of Two1. Find magnitude of decimal number.2. Subtract largest power of two

less than or equal to number.3. Put a one in the corresponding bit position.4. Keep subtracting until result is zero.5. Append a zero as MS bit;

if original was negative, take two’s complement.X = 104ten 104 - 64 = 40 bit 6

40 - 32 = 8 bit 58 - 8 = 0 bit 3

X = 01101000two

n 2n

0 11 22 43 84 165 326 647 128

8 256

9 512

10 1024

Page 52: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

52

Operations: Arithmetic and Logical

Recall: a data type includes representation and operations.

We now have a good representation for signed integers,so let’s look at some arithmetic operations: Addition Subtraction Sign Extension

We’ll also look at overflow conditions for addition.Multiplication, division, etc., can be built from these

basic operations.Logical operations are also useful:

AND OR NOT

Page 53: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

53

Addition

As we’ve discussed, 2’s comp addition is just binary addition. assume all integers have the same number of bits ignore carry out for now, assume that sum fits in n-bit 2’s comp.

representation

01101000 (104) 11110110 (-10)

+11110000 (-16) +11110111 (-9)(1)01011000 (88)

Assuming 8-bit 2’s complement numbers.

(-19)(1)11101101

Page 54: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

54

Subtraction

Negate subtrahend (2nd no.) and add. assume all integers have the same number of bits ignore carry out for now, assume that difference fits in n-bit 2’s comp

representation

01101000 (104) 11110110 (-10)

-00010000 (16) -11110111 (-9)

01101000 (104) 11110110 (-10)

+11110000 (-16) + 0001001 (9)

01011000 (88) 11111111 (-1)

Assuming 8-bit 2’s complement numbers.

Page 55: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

55

Sign Extension

To add two numbers, we must represent themwith the same number of bits.

If we just pad with zeroes on the left:

Instead,propagate the MS bit (the sign bit):

4-bit 8-bit0100(4) 00000100 (still 4)1100(-4) 00001100 (12, not -4)

4-bit 8-bit0100(4) 00000100 (still 4)1100(-4) 11111100 (still -4)

Page 56: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

Computer Science 210 s1cComputer Systems 1

2014 Semester 1

Lecture Notes

James Goodman (revised by Robert Sheehan)

Credits: Adapted from slides prepared by Gregory T. Byrd, North Carolina State University

Logic and more data typesLecture 3

56

Page 57: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

57

Overflow

If operands are too big, their sum cannot be represented as an n-bit 2’s comp number.

We have overflow if: signs of both operands are the same, and sign of sum is different.

Another test -- easy for hardware: carry into MS bit does not equal carry out

01000 (8) 11000(-8)+01001 (9) +10111 (-9)

10001 (-15) 01111 (+15)

Page 58: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

Overflow

Example: 4-bit Two’s complement - 8 <= x <= 7 Examples:

0110+ 0111 01100 carries 01101 (4-bit) => 1101

67

Answer = -3 Invalid answer

6 + 7 = 13 (outside the range)

1010 + 1001 10000 carries 10011(4-bit)= 0011

Answer = 3 Invalid answer

-6-7

-6 + -7 = -13 (outside the range)

1110 + 0011 11100 carries 10001 (4-bit)=0001

Answer = 1 valid answer

-23

-2 + 3 = 1 0010+0011 0100 carries 0101 Answer = 5

Valid answer

23

2 + 3 = 5

58

Page 59: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

Addition/Subtraction with 2’s Complement

Two’s complement representation allows addition and subtraction from a single simple adder.

Circuit to add : S = A + BTo subtract A – B : invert B and enable carry in

59

Page 60: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

60

Logical Operations

Operations on logical TRUE or FALSE two states -- takes one bit to represent: TRUE=1, FALSE=0

View n-bit number as a collection of n logical values operation applied to each bit independently

A B A AND B0 0 00 1 01 0 01 1 1

A B A OR B0 0 00 1 11 0 11 1 1

A NOT A0 11 0

Page 61: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

61

Examples of Logical Operations

AND useful for clearing bits

• AND with zero = 0• AND with one = no change

OR useful for setting bits

• OR with zero = no change• OR with one = 1

NOT unary operation -- one argument flips every bit

11000101AND 00001111

00000101

11000101OR 00001111

11001111

NOT 1100010100111010

Page 62: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

62

Hexadecimal Notation (not Representation)

It is often convenient to write binary (base-2) numbersusing hexadecimal (base-16) notation instead. fewer digits -- four bits per hex digit less error prone -- easy to corrupt long string of 1’s and 0’s

Binary Hex Decimal

0000 0 00001 1 10010 2 20011 3 30100 4 40101 5 50110 6 60111 7 7

Binary Hex Decimal

1000 8 81001 9 91010 A 101011 B 111100 C 121101 D 131110 E 141111 F 15

Page 63: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

63

Converting from Binary Notation to Hexadecimal NotationEvery four bits is a hex digit.

start grouping from right-hand side

011 1010 1000 1111 0100 1101 0111

7D4F8A3

This is not a new machine representation,

just a convenient way to write the number.

Page 64: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

64

Representing Text

American Standard Code for Information Interchange (ASCII) Developed from telegraph codes, alternative to IBM’s

Extended Binary Coded Decimal Interchange Code (EBCDIC) in 1960s

Printable and non-printable (ESC, DEL, …) characters (127) Limited set of characters – many character missing, especially

language-specific Many national “standards” developed Unicode – more than 110,000 characters covering 100 scripts

• UTF-8 is the form of Unicode which preserves the ASCII encoding

Page 65: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

65

Text: ASCII Characters

ASCII: Maps 128 characters to 7-bit code. both printable and non-printable (ESC, DEL, …) characters “ASCIIbetical” order

00 nul 10 dle 20 sp 30 0 40 @ 50 P 60 ` 70 p01 soh11 dc121 ! 31 1 41 A 51 Q 61 a 71 q02 stx 12 dc222 " 32 2 42 B 52 R 62 b 72 r03 etx 13 dc323 # 33 3 43 C 53 S 63 c 73 s04 eot 14 dc424 $ 34 4 44 D 54 T 64 d 74 t05 enq15 nak25 % 35 5 45 E 55 U 65 e 75 u06 ack 16 syn 26 & 36 6 46 F 56 V 66 f 76 v07 bel 17 etb 27 ' 37 7 47 G 57 W 67 g 77 w08 bs 18 can28 ( 38 8 48 H 58 X 68 h 78 x09 ht 19 em 29 ) 39 9 49 I 59 Y 69 i 79 y0a nl 1a sub2a * 3a : 4a J 5a Z 6a j 7a z0b vt 1b esc 2b + 3b ; 4b K 5b [ 6b k 7b {0c np 1c fs 2c , 3c < 4c L 5c \ 6c l 7c |0d cr 1d gs 2d - 3d = 4d M 5d ] 6d m 7d }0e so 1e rs 2e . 3e > 4e N 5e ^ 6e n 7e ~0f si 1f us 2f / 3f ? 4f O 5f _ 6f o 7f del

Page 66: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

66

Interesting Properties of ASCII Code

What is relationship between a decimal digit (‘0’, ‘1’, …)and its ASCII code?

What is the difference between an upper-case letter (‘A’, ‘B’, …) and its lower-case equivalent (‘a’, ‘b’, …)?

Given two ASCII characters, how do we tell which comes first in alphabetical order?

Is 128 characters enough?(http://www.unicode.org/)No new operations -- integer arithmetic and logic.

Page 67: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

Representation of non-Integers

Text, stringsFractionsScientific notation/Floating point representation

67

Page 68: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

68

Other Data Types

Text strings sequence of characters, terminated with NULL (0) typically, no hardware support

Image array of pixels

• monochrome: one bit (1/0 = black/white)• color: red, green, blue (RGB) components (e.g., 8 bits

each)• other properties: transparency

hardware support:• typically none, in general-purpose processors• MMX -- multiple 8-bit operations on 32- or 64-bit word• GPUs

Sound sequence of fixed-point numbers

Page 69: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

69

LC-3 Data Types

Some data types are supported directly by the instruction set architecture.

For LC-3, there is only one hardware-supported data type: 16-bit 2’s complement signed integer Operations: ADD, AND, NOT

Other data types are supported by interpreting 16-bit values as logical, text, fixed-point, etc., in the software that we write.

Page 70: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

70

Fractions: Fixed-Point

How can we represent fractions? Use a “binary point” to separate positive

from negative powers of two -- just like “decimal point.” 2’s comp addition and subtraction still work

• only if binary points are aligned

00101000.101 (40.625)+ 11111110.110 (-1.25)

00100111.011 (39.375)

No new operations -- same as integer arithmetic.

2-1 = 0.52-2 = 0.252-3 = 0.125

Page 71: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

Scientific Notation

We can only represent 232 (~ 4 billion) or maybe 264 (~ 18 quintillion) or even 2128 (~ 3.4 x 1038 or 340,000 decillion) unique values, but there are

• Infinitely many numbers between any two integers• Infinitely many numbers between any two real numbers!

We can only represent a (small) finite number of values. These values are not spread uniformly along number line Many numbers between zero and one Not many numbers between 1,000,000,000,000 and

1,000,000,000,001

71

Page 72: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

Scientific Notation

Conventional (decimal) notation:± mantissa x 10exponent

1 ≤ mantissa < 10exponent is signed integer

Binary notation:± mantissa x 2exponent

1 ≤ mantissa < 2exponent is signed integer

72

Page 73: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

Significant Digits

Accuracy of measurement leads to notion of Significant Digits For most purposes, we don’t need high precision Accuracy of calculations is generally limited by least precise

numbers Can represent numbers with a few significant digits

• 6.0221413 * 1023 Avogadro constant (approximately)• 299,792,458 meters/sec -- Speed of Light (exactly!)

– By definition, a meter is the distance light travels through a vacuum in exactly 1/299792458 seconds

• 3.141592…– Computable to arbitrary accuracy, but– More digits probably won’t improve result.

73

Page 74: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

Representation of Floating Point Numbers (Reals)

As with integers and chars, we ask

• Which reals? There is an infinite number between two adjacent integers.In fact, there are an infinite number between any two reals!!!!!!!

• Which bit patterns for selected reals?

Answer for both strongly related to scientific notation.

74

Page 75: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

75

Very Large and Very Small: Floating-Point

Large values: 6.023 x 1023 -- requires 79 bitsSmall values: 6.626 x 10-34 -- requires >110 bits

Use equivalent of “scientific notation”: F x 2E

Need to represent F (fraction), E (exponent), and sign.IEEE 754 Floating-Point Standard (32-bits):

Exponent uses “biased” representationFraction has implicit 1

S Exponent Fraction

1b 8b 23b

Page 76: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

76

Floating Point Example

Single-precision IEEE floating point number:1 01111110 10000000000000000000000

Sign is 1 – number is negative. Exponent field is 01111110 = 126 (decimal). Fraction is 0.100000000000… = 0.5 (decimal).

Value = -1.5 x 2(126-127) = -1.5 x 2-1 = -0.75.

sign exponent fraction

Page 77: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

77

IEEE Floating-Point Standard (32-bit)

S ExponentFraction

1b 8b 23b

0exponent,2fraction.0)1(

254exponent1,2fraction.1)1(126

127exponent

S

S

N

N

Page 78: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

Example: Show FP Representation for 40.625ten

1. From earlier slide 40.625ten = 00101000.101two

2. Put the binary rep. into normal form (make it look like scientific notation): +101000.101 x 20 = +1.01000101 x 25

3. 5 is the true exponent; with bias: 5 + 127 = 132ten = 1000 0100two

4. Mantissa/Fraction occupies 23 bits:(1.) 010 0010 1000 0000 0000 0000

78

S Exponent Fraction

1b 8b 23b

0 1000 0100

1b 8b

010 0010 1000 0000 0000 0000

23b

Page 79: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

79

Floating-Point Operations

Will regular 2’s complement arithmetic work for Floating Point numbers?

(Hint: In decimal, how do we compute 3.07 x 1012 + 9.11 x 108?)

Page 80: COMPSCI 210S1C  2014 Computer Systems 1 Introduction

80

Floating-Point Arithmetic

Floating point operations may overflow but, more importantly, floating point operations are inherently inexact

Some numbers (e.g. “repeating decimal”) cannot be represented exactly.

Introduces the “Rounding” problem• Every inexact result creates a difference

between the mathematical value and the computed value.

• Errors accumulate, often benignly by cancelling out.

• Worst-case accumulation of error can be enormous.