COMPSCI 210S1C 2014 Computer Systems 1 Introduction
description
Transcript of COMPSCI 210S1C 2014 Computer Systems 1 Introduction
COMPSCI 210S1C 2014Computer Systems 1
Introduction
1
Computer Science 210 s1cComputer Systems 1
2014 Semester 1
Lecture Notes
James Goodman (revised by Robert Sheehan)
Introduction & Layers
Lecture 1
2
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
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
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
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
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
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
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
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
Basic componentsoData representation
• Binary fraction• Floating point representation
Introduction to COperatorsControl structureFunctionsPointers, arrays, stringI/OAdvanced programming
C programming
11
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
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
Exam
Multiple-choice questions (MCQ)See examples from previous semestersWorth 60% towards course grade
14
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
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
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
Chapter 1Welcome Aboard
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
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.
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
Alan Turing (12 Jun 1912 – 7 Jun 1954)
22
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/
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.
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, ...
26
Big Idea #2: Transformations Between Layers
Problems
Language
Instruction Set Architecture
Microarchitecture
Circuits
Devices
Algorithms
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
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
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
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
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.)
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?
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
Chapter 2Bits, Data Types,and Operations
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
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
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…
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
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
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,…
41
“There are 10 kinds of people in the world: those who understand binary, and those who don’t.”
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
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)
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)
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
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
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
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.
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
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
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
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
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
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.
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)
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
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)
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
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
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
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
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
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.
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
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
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.
Representation of non-Integers
Text, stringsFractionsScientific notation/Floating point representation
67
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
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.
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
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
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
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
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
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
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
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
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
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?)
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.