Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMPSCI 210S1C 2014 Computer Systems 1 Introduction

Similar presentations


Presentation on theme: "COMPSCI 210S1C 2014 Computer Systems 1 Introduction"— Presentation transcript:

1 COMPSCI 210S1C 2014 Computer Systems 1 Introduction

2 James Goodman (revised by Robert Sheehan)
Computer Science 210 s1c Computer Systems Semester 1 Lecture Notes Introduction & Layers Lecture 1 James Goodman (revised by Robert Sheehan)

3 Lecturers Tutors Class Representative Robert Sheehan (week 1-6)
Office Office hours: by appointment, after class, or whenever the office door is open Xinfeng Ye (week 7-12) Office Office hours: Tutors Ahmad Obidat Office hours: TBA Class Representative TBD (volunteers?) Arash Heidarian Office hours: TBA

4 Forum Tutors We 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. Tutors Are available to help 4

5 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 online Lecture recordings in the Knowledge Map area of Cecil

6 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 lectures Outside office hours and tutorials, tutors are not expected to be at hand

7 Course Outline Bits and Bytes Digital Logic
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? Redundant?

8 Textbook LC-3 simulator
LC-3 simulator

9 Data Representation Data Performing Arithmetic
Binary Octal Decimal Signed Numbers Performing Arithmetic Addition Subtraction Shifting (Mul/Div) Types and Representation Integer Floating point -- IEEE format Alpha-numeric representation IEEE Format is so widely adopted it is often not mentioned. Note that P&P don’t mention octal except in Appendix D!!!

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

11 C programming Basic components Data representation Binary fraction
Floating point representation Introduction to C Operators Control structure Functions Pointers, arrays, string I/O Advanced programming

12 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 marks Submissions 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. 3 or 4??? New system this semester – bugs still being worked out!

13 Test Tentative: During class on 30 April, Week 7 (28 April – 2 May)
Multiple-choice questions (MCQ) Material through week 6 See example from previous semesters Worth 20% towards course grade SURVEY: How many would prefer MCQ ONLY?

14 Exam Assignments are important!!! Multiple-choice questions (MCQ)
See examples from previous semesters Worth 60% towards course grade Assignments are important!!!

15 How to Do Well in CompSci 210
1. Read the lecture notes after each lecture 2. Read the relevant textbook sections a. To learn more b. To complement lectures 3. If you have questions or do not understand something Attend the tutorials Check the forum Discuss with other 210 students Ask a tutor during office hours or see me 4. How to prepare for exams Do 1, 2 & 3 above Do exercises of the course/tutorials/exercises/textbook Study previous years’ exams: You can get these online from the library website Point 3 is Patrice’s point, not mine! The internet is a wonderful tool, especially, Wikipedia

16 Assignments Assignment Subject Approx date out Date due
% of the final mark # 1 Data representation/Digital logic/simple machines 10/03/14 27/03/14 5 # 2 Low-level programming 31/03/14 01/05/14 # 3 C code 05/05/14 29/05/14 10

17 Yale N. Patt Sanjay J. Patel
Introduction to Computing Systems: From Bits and Gates to C and Beyond 2nd Edition Yale N. Patt Sanjay J. Patel Based on slides originally prepared by Gregory T. Byrd, North Carolina State University

18 Chapter 1 Welcome 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 C and 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 Hardware vs. Software
Productivity enhancer – don’t need to worry about details… Can drive a car without knowing how the internal combustion engine works. …until something goes wrong! Where’s the dipstick? What’s a spark plug? Important to understand the components and how 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. Many aspects can be either S/W or H/W, or a combination. Driving choice: cost and speed (flexibility).

21 Big Idea #1: Universal Computing Device
All computers, given enough time and memory, are capable of computing exactly the same things. = = Smart phone Desktop Supercomputer

22 Alan Turing (12 Jun 1912 – 7 Jun 1954) Added

23 Tadd Tmul Turing Machine
Mathematical model of a device that can perform any 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) Previously, a “computer” was built to solve a problem: ordinance calculations, controlling a textile loom (Jacquard), computing logarithms, code breaking, tabulating results. Tadd a,b a+b Turing machine that adds Tmul a,b ab Turing machine that multiplies For more info about Turing machines, see

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) U a,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, ... Further Caveats: 1. A machine may be deliberately restricted for size or function (needs access to unbounded memory). 2. Accuracy may be limited.

26 Big Idea #2: Transformations Between Layers
Problems Algorithms Language Instruction Set Architecture Microarchitecture Circuits Devices

27 How do we solve a problem using a computer?
A systematic sequence of transformations between layers of abstraction. Problem Software Design: choose algorithms and data structures Algorithm Programming: use language to express design Program Compiling/Interpreting: convert language to machine instructions Instr Set Architecture

28 Deeper and Deeper… Processor Design:
Instr Set Architecture Processor Design: choose structures to implement ISA Microarch Logic/Circuit Design: gates and low-level circuits to implement components Circuits Process Engineering & Fabrication: develop and manufacture lowest-level components Devices

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 Jacobi iteration Red-black SOR Multigrid FORTRAN C C++ Java Intel x86 ARM NVIDIA Haswell Nehalem Atom Ripple-carry adder Carry-lookahead adder CMOS Bipolar GaAs Tradeoffs: cost performance power (etc.) SOR = Successive Over Relaxation Cloud? Multiple processors? Sun and Java are trademarks of Sun Microsystems, Inc. Intel, Pentium, Centrino, and Xeon are trademarks of Intel Corporation. AMD and Athlon and trademarks of Advanced Micro Devices, Inc. Atmel and AVR are registered trademarks of Atmel Corporation.

32 Course Outline Bits and Bytes Digital Logic
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? Review

33 James Goodman (revised by Robert Sheehan)
Computer Science 210 s1c Computer Systems Semester 1 Lecture Notes Data Representation Lecture 2 James Goodman (revised by Robert Sheehan) Credits: Adapted from slides prepared by Gregory T. Byrd, North Carolina State University

34 Chapter 2 Bits, Data Types, and Operations
Reading is critical – keep current

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: presence of a voltage – we’ll call this state “1” 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 Early work on analog computation.

36 Computer is a Binary Digital System.
finite number of symbols Binary (base two) system: has two states: 0 and 1 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.

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… Others are important, but numbers and text occur in nearly every application.

38 101 329 Unsigned Integers 22 21 20 102 101 100 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 101 22 21 20 most significant least 329 102 101 100 3x x10 + 9x1 = 329 1x4 + 0x2 + 1x1 = 5

39 Unsigned Integers (cont.)
An n-bit unsigned integer represents any of 2n (integer) values: from 0 to 2n-1. 22 21 20 Value 1 2 3 4 5 6 7 All eight possible combinations

40 Unsigned Binary Arithmetic
Base-2 addition – just like base-10! add from right to left, propagating carry carry 10111 + 111 Subtraction, multiplication, division,…

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

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 = 5 Negative integers Sign-Magnitude (or Signed-Magnitude) – set MS bit to show negative, other bits are the same as unsigned = -5 One’s complement – flip every bit to represent negative = -5 In either case, MS bit indicates sign: 0=positive, 1=negative For sign bit, we arbitrarily set 0 for positive, 1 for negative (for good reason) Problem: two representations of zero!

43 Two’s Complement + 11011 (-5) + (-9) 00000 (0) 00000 (0)
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 make circuits 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 Answer: = 10111 00101 (5) (9) (-5) + (-9) 00000 (0) (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 2’s complement negation: 1’s complement + 1 Works for both positive and negative numbers! 01001(9) -> = = -9 00101 (5) (9) 11010 (1’s comp) (1’s comp) 11011 (-5) (-9)

45 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. The most negative number (-2n-1) has no positive counterpart. -23 22 21 20 1 2 3 4 5 6 7 -23 22 21 20 1 -8 -7 -6 -5 -4 -3 -2 -1

46 “Biased” Representation of Signed Integers
All 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 -8 1 -7 -6 -5 -4 -3 -2 -1 23 22 21 20 Bias-8 1 2 3 4 5 6 7

47 “Biased” Representation of Signed Integers
All 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 -7 1 -6 -5 -4 -3 -2 -1 23 22 21 20 Bias-7 1 2 3 4 5 6 7 8 Why do this? Represent more positive numbers than negative numbers But means that zero is not represented by all zeros

48 Converting Binary (2’s C) to Decimal
If leading bit is one, take two’s complement to get a positive number. Add powers of 2 that have “1” in the corresponding bit positions. If original number was negative, add a minus sign. n 2n 1 2 4 3 8 16 5 32 6 64 7 128 256 9 512 10 1024 X = two = = = 104ten Assuming 8-bit 2’s complement numbers.

49 More Examples X = 00100111two = 25+22+21+20 = 32+4+2+1 = 39ten
= = = 39ten n 2n 1 2 4 3 8 16 5 32 6 64 7 128 256 9 512 10 1024 X = two -X = = = = 26ten X = -26ten Assuming 8-bit 2’s complement numbers.

50 Converting Decimal to Binary (2’s C)
First Method: Division Find magnitude of decimal number. (Always positive.) Divide by two – remainder is least significant bit. Keep dividing by two until answer is zero, writing remainders from right to left. Append a zero as the MS bit; if original number was negative, take two’s complement. Key observation: LSB for a binary representation tells you if the number is even or odd. X = 104ten 104/2 = 52 r0 bit 0 52/2 = 26 r0 bit 1 26/2 = 13 r0 bit 2 13/2 = 6 r1 bit 3 6/2 = 3 r0 bit 4 3/2 = 1 r1 bit 5 X = two 1/2 = 0 r1 bit 6

51 Converting Decimal to Binary (2’s C)
1 2 4 3 8 16 5 32 6 64 7 128 256 9 512 10 1024 Second Method: Subtract Powers of Two Find magnitude of decimal number. Subtract largest power of two less than or equal to number. Put a one in the corresponding bit position. Keep subtracting until result is zero. Append a zero as MS bit; if original was negative, take two’s complement. X = 104ten = 40 bit 6 = 8 bit 5 8 - 8 = 0 bit 3 X = two

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 + 11110000 (-16) + 11110111 (-9) (1)01011000 (88) (1)11101101
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 +9 : -9 : 1 (104) (-10) (-16) (-9) (1) (88) (1) (-19) Assuming 8-bit 2’s complement numbers.

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 (104) (-10) (16) (-9) (-16) (9) (88) (-1) Work out on blackboard? Don’t have to compute -9! Just use +9 Assuming 8-bit 2’s complement numbers.

55 Sign Extension To add two numbers, we must represent them with 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-bit 0100 (4) (still 4) 1100 (-4) (12, not -4) Replicate the sign bit: 4-bit 8-bit 0100 (4) (still 4) 1100 (-4) (still -4)

56 Logic and more data types
Computer Science 210 s1c Computer Systems Semester 1 Lecture Notes Logic and more data types Lecture 3 James Goodman (revised by Robert Sheehan) Credits: Adapted from slides prepared by Gregory T. Byrd, North Carolina State University

57 Overflow + 01001 (9) + 10111 (-9) 10001 (-15) 01111 (+15)
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) (-8) (9) (-9) 10001 (-15) (+15)

58 Overflow 1010 10000 carries 0110 10011(4-bit)= 0011 + 0111
Example: 4-bit Two’s complement - 8 <= x <= 7 Examples: -6 -7 = -13 (outside the range) 6 + 7 = 13 (outside the range) 1010 + 1001 10000 carries 10011(4-bit)= 0011 6 7 0110 01100 carries 01101 (4-bit) => 1101 6 + 7 : two positive numbers gives a negative result (4 bits) : two negative numbers gives a positive result : CAN’T overflow! 2 + 3 : two positive numbers give s positive result Answer = 3 Invalid answer Answer = -3 Invalid answer 2 3 2 + 3 = 5 -2 3 = 1 0010 +0011 0100 carries 0101 1110 11100 carries 10001 (4-bit)=0001 Answer = 1 valid answer Answer = 5 Valid answer

59 Addition/Subtraction with 2’s Complement
Two’s complement representation allows addition and subtraction from a single simple adder. Circuit to add : S = A + B To subtract A – B : invert B and enable carry in From chapter 3 of textbook (coming soon!) Easy to invert B inputs Easy to add carry in

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 B 1 A B A OR B 1 A NOT A 1 Truth table Also EXCLUSIVE OR

61 Examples of Logical Operations
AND 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 OR Note that NOT is also the 1’s complement operation! Also XOR (as opposed to inclusive OR) NOT

62 Hexadecimal Notation (not Representation)
It is often convenient to write binary (base-2) numbers using 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 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 Binary Hex Decimal 1000 8 1001 9 1010 A 10 1011 B 11 1100 C 12 1101 D 13 1110 E 14 1111 F 15 Hexadecimal is a Shorthand NOTATION, not a DATA TYPE Can talk about “unsigned hexadecimal” or hexadecimal “2’s complement” representation. We are NOT talking about 15’s complement and 16’s complement! Can perform BINARY arithmetic operation on hexadecimal notation.

63 Converting from Binary Notation to Hexadecimal Notation
Every four bits is a hex digit. start grouping from right-hand side 7 D 4 F 8 A 3 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 40 @ 50 P 60 ` 70 p 01 soh 11 dc1 21 ! 31 1 41 A 51 Q 61 a 71 q 02 stx 12 dc2 22 " 32 2 42 B 52 R 62 b 72 r 03 etx 13 dc3 23 # 33 3 43 C 53 S 63 c 73 s 04 eot 14 dc4 24 $ 34 4 44 D 54 T 64 d 74 t 05 enq 15 nak 25 % 35 5 45 E 55 U 65 e 75 u 06 ack 16 syn 26 & 36 6 46 F 56 V 66 f 76 v 07 bel 17 etb 27 ' 37 7 47 G 57 W 67 g 77 w 08 bs 18 can 28 ( 38 8 48 H 58 X 68 h 78 x 09 ht 19 em 29 ) 39 9 49 I 59 Y 69 i 79 y 0a nl 1a sub 2a * 3a : 4a J 5a Z 6a j 7a z 0b 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 Note definition did not define usage, simply name Missing: different kinds of quotes accent marks, diphthong, swung dash: NOT tilda (which is missing) bs: backspace nl: newline ht: (horizontal tab) esc: escape

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? ( “ASCIIbetical order” : why “B” comes before “a” (Note that Patt/Patel index is ASCIIbetical!) Other properties: integers are consecutive (can do limited arithmetic by subtracting ‘0’) No new operations -- integer arithmetic and logic.

67 Representation of non-Integers
Text, strings Fractions Scientific notation/Floating point representation

68 Other Data Types Text strings Image Sound
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. ASCII characters appear to be supported in I/O operations

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 2-1 = 0.5 2-2 = 0.25 2-3 = 0.125 Note that alignment is required (“de-normalizing”) Note that such computations can be done in software, but multiplication requires additional shifting (40.625) (-1.25) (39.375) No new operations -- same as integer arithmetic.

71 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 Use board to illustrate number line and non-representation of zero.

72 Scientific Notation Conventional (decimal) notation: ± mantissa x 10exponent 1 ≤ mantissa < 10 exponent is signed integer Binary notation: ± mantissa x 2exponent 1 ≤ mantissa < 2 Mantissa – also called “significand” is the part of a floating point number containing the significant digits Note that the 1 (most significant digit) can be implied, because it’s (almost) always 1.

73 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 * 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/ seconds Computable to arbitrary accuracy, but More digits probably won’t improve result. Use board to illustrate number line and non-representation of zero. SPECIAL CASES: speed of light, Pi

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

75 Very Large and Very Small: Floating-Point
Large values: x requires 79 bits Small values: x 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” representation Fraction has implicit 1 Exponent = 255 used for special values: If Fraction is non-zero, NaN (not a number). If Fraction is zero and sign is 0, positive infinity. If Fraction is zero and sign is 1, negative infinity. 1b 8b 23b S Exponent Fraction

76 Floating Point Example
Single-precision IEEE floating point number: Sign is 1 – number is negative. Exponent field is = 126 (decimal). Fraction is … = 0.5 (decimal). Value = -1.5 x 2( ) = -1.5 x 2-1 = sign exponent fraction Why subtract 127??? Biased exponent is a variant of 2’s complement (basically reversing sense of the sign bit)

77 IEEE Floating-Point Standard (32-bit)
Exponent Fraction 1b 8b 23b Exponent = 255 used for special values: If Fraction is non-zero, NaN (not a number). If Fraction is zero and sign is 0, positive infinity. If Fraction is zero and sign is 1, negative infinity.

78 Example: Show FP Representation for 40.625ten
Exponent Fraction 1b 8b 23b From earlier slide ten = two Put the binary rep. into normal form (make it look like scientific notation): x 20 = x 25 5 is the true exponent; with bias: = 132ten = two Mantissa/Fraction occupies 23 bits: (1.) Note that “Biased” means unsigned, but with 127 added. Fixedslide!!! Note that animated fraction has different number! 1b 8b 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 x 108?) Short answer: NO! (Need to “de-normalize” numbers!)

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. Unlike integers which use operations defined to give a correct value within a range, every two floating point numbers have infinitely many numbers between them. Only a few can be represented. No irrational numbers can be represented exactly!


Download ppt "COMPSCI 210S1C 2014 Computer Systems 1 Introduction"

Similar presentations


Ads by Google