Presentation is loading. Please wait.

Presentation is loading. Please wait.

UNIT 1 INTRODUCTION NUMBER SYSTEMS AND CONVERSION Click the mouse to move to the next page. Use the ESC key to exit this chapter. This chapter in the book.

Similar presentations


Presentation on theme: "UNIT 1 INTRODUCTION NUMBER SYSTEMS AND CONVERSION Click the mouse to move to the next page. Use the ESC key to exit this chapter. This chapter in the book."— Presentation transcript:

1 UNIT 1 INTRODUCTION NUMBER SYSTEMS AND CONVERSION Click the mouse to move to the next page. Use the ESC key to exit this chapter. This chapter in the book includes: Objectives Study Guide 1.1Digital Systems and Switching Circuits 1.2Number Systems and Conversion 1.3Binary Arithmetic 1.4Representation of Negative Numbers 1.5Binary Codes Problems

2 Figure 1-1: Switching circuit

3 Decimal Notation 953.78 10 = 9x10 2 + 5x10 1 + 3x10 0 + 7x10 -1 + 8x10 -2 Binary 1011.11 2 = 1x2 3 + 0x2 2 + 1x2 1 + 1x2 0 + 1x2 -1 + 1x2 -2 = 8 + 0 + 2 + 1 + 1 / 2 + 1 / 4 = 11.75 10

4 EXAMPLE: Convert 53 10 to binary. Conversion (a)

5 Conversion (b) EXAMPLE:Convert.625 10 to binary.

6 Conversion (c) EXAMPLE: Convert 0.7 10 to binary.

7 Conversion (d) EXAMPLE: Convert 231.3 4 to base 7.

8 Equation (1-1) Conversion from binary to hexadecimal (and conversely) can be done by inspection because each hexadecimal digit corresponds to exactly four binary digits (bits). Binary  Hexadecimal Conversion

9 Numbering and Coding Systems – Decimal and binary number systems – Converting from decimal to binary – Converting from binary to decimal – Hexadecimal system Conversion to/from binary Conversion to/from decimal Adding/subtracting binary and hex numbers Two’s complement representation Powers of 2 “versus” Powers of 10 – 2 8 = 256 – 2 10 = 1Kilo (1024) 10 3 = 1,000 – 2 20 = 1Mega (1024 2 = 1,048,576 ) 10 6 = 1,000,000 – 2 30 = 1Giga (1024 3 = 1,073,741,824) 10 9 = 1,000,000,000 – 2 40 = 1Tera (1024 4 = 1,099,511,627,776) 10 12 = 1,000,000,000,000 9

10 From “any base”  To “Decimal” Positional Weighting Addition of the products of each digit by its respective “column weight” e.g. 0x3C7B= 3*16 3 + 12*16 2 + 7*16 1 + 11*16 0 = 3* 4096 + 12*256 + 7*16 + 11 = 15483 From “Decimal”  To “any base” Dabble Method (Successive Division by base radix) e.g. 29758=29753/16=1859R14 (0xE) 1859 / 16=116R3 116 / 16=7R4 7 / 16=0R7 (Done when Quot is 0) Thus, 29758 = 0x743E (Remainders taken in reverse order) 10 Radix Conversions

11 Since 16 is the 4 th power of 2, we can express large binary numbers in hex using ¼ less digits. The debugging tools used in assembly language program development express the data in CPU registers and Memory registers as hex values. Compiled object code is expressed as hex. It is well worth the time and effort of the student to be able to switch back and forth between the two. The table below, to be memorized, shows the equivalences: 11 Hex to Binary & Binary to Hex Conversions HexBinaryDecimal 000000 100011 200102 300113 401004 501015 601106 701117 HexBinaryDecimal 810008 910019 A101010 B101111 C110012 D110113 E111014 F111115

12 Example 1: based upon the lookup tables on the previous page, convert the hex value 0x7C9B_F3D6 into its binary equivalent: Example 2: based upon the lookup tables on the previous page, convert the binary value 1010 0010 1110 1000 0100 1011 0001 1111 2 into its hex equivalent: 12 Hex to Binary & Binary to Hex Conversions 0111 1100 1001 1011 11110011 11010110 1010 0010 1110 1000 01001011 00011111 2 E 8 4B 1F A 7 C 9 B F3 D6

13 Hex addition is also performed similar to decimal addition except that each hex digit has a range of ‘0’ to ‘F’ instead of from ‘0’ to ‘9’, and a “carry out” occurs when the sum of hex digits in a particular column exceeds ‘F’ (15 decimal). Examples: 13 Hex Addition A B 3 +2 7 C 7 +2 A 8 6 +E D D 1 1 F 3 1 1 7 3 B 9 A +D 2 E 6 0 1 8 1 E1 0

14 / Hex addition is also performed similar to decimal subtraction except that if the subtrahend is greater than the minuend, we must “borrow” 16 (10 hex) from the previous digit. Examples: 14 Hex Subtraction D B 3 -3 6 C A -2 5 7 5 A / 9 A 1 A / 1 1 2 C D 3 5 F -4 3 B 6 F 8

15 The basic data types used by processors, expressed as binary values, are signed and unsigned integers that are “n-bits” wide (e.g. 8-bit, 16-bit, 32-bit, 64-bit). Range of “n-bit” unsigned integer (all are positive) 0 (min) to 2 n – 1 (max) – Examples:8-bit unsigned 0 to 2 8 – 1  0 to 255 16-bit unsigned 0 to 2 16 – 1  0 to 65,535 Range of “n-bit” signed integer (half are negative, half are positive) -2 n-1 (min) to 2 n-1 – 1 (max) – Examples:8-bit signed -2 7 to 2 7 – 1  -128 to 127 16-bit signed -2 15 to 2 15 – 1  -32768to 32767 o Signed integers are represented in two’s complement format 15 Signed and Unsigned Integers

16 American Standard Code for Informational Interchange (ASCII) 16 Introduction to Computing 2 Binary000001010011100101110111 BinaryHex01234567 00000NULLDLE SP0@P‘p 00011SOHDC1 !1AQaq 00102STXDC2 “2BRbr 00113ETXDC3 #3CScs 01004EOTDC4 $4DTdt 01015ENQNAK %5EUeu 01106ACKSYN &6FVfv 01117BELETB ‘7GWgw 10008BSCAN (8HXhx 10019HTEM )9IYiy 1010ALFSUB *:JZjz 1011BVTESC +;K[k{ 1100CFFFS `<L\l| 1101DCRGS -=M]m} 1110ESORS.>N^n~ 1111FSIUS /?O_oDEL MSBs LSBs

17 Addition Add 13 10 and 11 10 in binary.

18 Subtraction (a) The subtraction table for binary numbers is 0 – 0 = 0 0 – 1 = 1and borrow 1 from the next column 1 – 0 = 1 1 – 1 = 0 Borrowing 1 from a column is equivalent to subtracting 1 from that column.

19 Subtraction (b) EXAMPLES OF BINARY SUBTRACTION:

20 Subtraction (c) A detailed analysis of the borrowing process for this example, indicating first a borrow of 1 from column 1 and then a borrow of 1 from column 2, is as follows:

21 Multiplication (a) The multiplication table for binary numbers is 0 x 0 = 0 0 x 1 = 0 1 x 0 = 0 1 x 1 = 1

22 Multiplication (b) The following example illustrates multiplication of 13 10 by 11 10 in binary:

23 Multiplication (c) When doing binary multiplication, a common way to avoid carries greater than 1 is to add in the partial products one at a time as illustrated by the following example: 1111multiplicand 1101multiplier 11111st partial product 0000 2nd partial product (01111)sum of first two partial products 1111 3rd partial product (1001011)sum after adding 3rd partial product 1111 4th partial product 11000011final product (sum after adding 4th partial product)

24 Binary Division Binary division is similar to decimal division, except it is much easier because the only two possible quotient digits are 0 and 1. We start division by comparing the divisor with the upper bits of the dividend. If we cannot subtract without getting a negative result, we move one place to the right and try again. If we can subtract, we place a 1 for the quotient above the number we subtracted from and append the next dividend bit to the end of the difference and repeat this process with this modified difference until we run out of bits in the dividend.

25 Binary Division The following example illustrates division of 145 10 by 11 10 in binary:

26 3 Systems for representing negative numbers in binary Sign & Magnitude: Most significant bit is the sign Ex: – 5 10 = 1101 2 1’s Complement: = (2 n – 1) - N Ex: – 5 10 = (2 4 – 1) – 5 = 16 – 1 – 5 = 10 10 = 1010 2 2’s Complement: N* = 2 n - N Ex: – 5 10 = 2 4 – 5 = 16 – 5 = 11 10 = 1011 2 Section 1.4 (p. 16)

27 Table 1-1: Signed Binary Integers (word length n = 4)

28 2’s Complement Addition (a)

29 2’s Complement Addition (b)

30 2’s Complement Addition (c)

31 1’s Complement Addition (b)

32 1’s Complement Addition (c)

33 1’s Complement Addition (d)

34 2’s Complement Addition (d)

35 Binary Codes Although most large computers work internally with binary numbers, the input-output equipment generally uses decimal numbers. Because most logic circuits only accept two-valued signals, the decimal numbers must be coded in terms of binary signals. In the simplest form of binary code, each decimal digit is replaced by its binary equivalent. For example, 937.25 is represented by: Section 1.5 (p. 21)

36 Table 1–2. Binary Codes for Decimal Digits

37 Table 1-3 ASCII code (incomplete)

38 Floating Point Numbers Floating point numbers Scientific notation – Decimal scientific notation – Binary scientific notation IEEE 754 FP Standard Floating point representation inside a computer – Greater range vs. precision Decimal to Floating Point conversion Type is not associated with data MIPS floating point instructions, registers

39 Computer Numbers Computers are made to deal with numbers What can we represent in n bits? – Unsigned integers:0 to 2 n - 1 – Signed integers: -2 (n-1) to 2 (n-1) - 1 What about other numbers? – Very large numbers? (seconds/century) 3,155,760,000 10 (3.15576 10 x 10 9 ) – Very small numbers? (atomic diameter) 0.00000001 10 (1.0 10 x 10 -8 ) – Rationals (repeating pattern) 2/3 (0.666666666...) – Irrationals:2 1/2 (1.414213562373...) – Transcendentals:e (2.718...),  (3.141...)

40 Scientific Notation Normalized form: no leadings 0s (exactly one digit to left of decimal point) Alternatives to representing 1/1,000,000,000 – Normalized: 1.0 x 10 -9 – Not normalized: 0.1 x 10 -8, 10.0 x 10 -10 6.02 x 10 23 radix (base) decimal pointmantissa exponent

41 Binary Scientific Notation Floating point arithmetic – Binary point is not fixed (as it is for integers) – Declare such variable in C as float 1.0 two x 2 -1 radix (base) “binary point” Mantissa Exponent

42 Floating Point Representation Normal format: +1.xxxxxxxxxx two *2 yyyy two Multiple of Word Size (32 bits) 0 31 S Exponent 302322 Significand 1 bit8 bits23 bits S represents Sign Exponent represents y’s Significand represents x’s Represent numbers as small as 2.0 x 10 -38 to as large as 2.0 x 10 38

43 Overflow and Underflow Overflow – Result is too large (> 2.0x10 38 ) – Exponent larger than represented in 8-bit Exponent field Underflow – Result is too small >0, < 2.0x10 -38 – Negative exponent larger than represented in 8-bit Exponent field How to reduce chances of overflow or underflow?

44 Double Precision FP Use two words (64 bits) C variable declared as double Represent numbers almost as small as 2.0 x 10 -308 to almost as large as 2.0 x 10 308 Primary advantage is greater accuracy (52 bits) 031 SExponent 302019 Significand 1 bit11 bits20 bits Significand (cont’d) 32 bits

45 Floating Point Representation Significand (cont’d) 0 S Exponent 2019 Significand 1 bit11 bits 20 bits 32 bits 3130 Double Precision Exponent: biased notation Significand: sign – magnitude notation Bias 127 (SP) 1023 (DP) 0 31 S Exponent 30 2322 Significand 1 bit 8 bits23 bits Single Precision

46 IEEE 754 FP Standard Used in almost all computers (since 1980) – Porting of FP programs – Quality of FP computer arithmetic Sign bit: Significand: – Leading 1 implicit for normalized numbers – 1 + 23 bits single, 1 + 52 bits double – always true: 0 < Significand < 1 0 has no leading 1 – Reserve exponent value 0 just for number 0 1 means negative 0 means positive (-1) S * (1 + Significand) * 2 Exp

47 IEEE 754 Exponent Use FP numbers even without FP hardware – Sort records with FP numbers using integer compares Break FP number into 3 parts: compare signs, then compare exponents, then compare significands Faster (single comparison, ideally) – Highest order bit is sign ( negative < positive) – Exponent next, so big exponent => bigger # – Significand last: exponents same => bigger #

48 Biased Notation for Exponents Two’s complement does not work for exponent Most negative exponent: 00000001 two Most positive exponent: 11111110 two Bias: number added to real exponent – 127 for single precision – 1023 for double precision 1.0 * 2 -1 (-1) S * (1 + Significand) * 2 (Exponent - Bias) 0 0111 1110 0000 0000 0000 0000 0000 000

49 Significand Method 1 (Fractions): – In decimal: 0.340 10 => 340 10 /1000 10 => 34 10 /100 10 – In binary: 0.110 2 => 110 2 /1000 2 (6 10 /8 10 ) => 11 2 /100 2 (3 10 /4 10 ) – Helps understand the meaning of the significand Method 2 (Place Values): – Convert from scientific notation – In decimal: 1.6732 = (1x10 0 ) + (6x10 -1 ) + (7x10 -2 ) + (3x10 -3 ) + (2x10 -4 ) – In binary: 1.1001 = (1x2 0 ) + (1x2 -1 ) + (0x2 -2 ) + (0x2 -3 ) + (1x2 -4 ) – Interpretation of value in each position extends beyond the decimal/binary point – Good for quickly calculating significand value – Use this method for translating FP numbers

50 Binary to Decimal FP Sign: 0 => positive Exponent: –0110 1000 two = 104 ten –Bias adjustment: 104 - 127 = -23 Significand: –1 + 1x2 -1 + 0x2 -2 + 1x2 -3 + 0x2 -4 + 1x2 -5 +... =1+2 -1 +2 -3 +2 -5 +2 -7 +2 -9 +2 -14 +2 -15 +2 -17 +2 -22 = 1.0 + 0.666115 00110 1000101 0101 0100 0011 0100 0010 Represents: 1.666115*2 -23 ~ 1.986*10 -7

51 Decimal to Binary FP (1/2) Simple Case: If denominator is a power of 2 (2, 4, 8, 16, etc.), then it’s easy. Example: Binary FP representation of -0.75 – -0.75 = -3/4 – -11 two /100 two = -0.11 two – Normalized to -1.1 two x 2 -1 – (-1) S x (1 + Significand) x 2 (Exponent-127) – (-1) 1 x (1 +.100 0000... 0000) x 2 (126-127) 10111 1110100 0000 0000 0000 0000 0000

52 Decimal to Binary FP (2/2) Denominator is not an exponent of 2 – Number can not be represented precisely – Lots of significand bits for precision – Difficult part: get the significand Rational numbers have a repeating pattern Conversion – Write out binary number with repeating pattern. – Cut it off after correct number of bits (different for single vs. double precision). – Derive sign, exponent and significand fields.

53 0.33333332 x 2 0.66666664 0.66666666 x 2 1.33333332 0.33333333 x 2 0.66666666 Decimal to Binary 1.Significand: 101 0101 0101 0101 0101 0101 2.Sign: negative => 1 3.Exponent: 1 + 127 = 128 ten = 1000 0000 two 11000 0000101 0101 0101 0101 0101 0101 - 3. 3 3 3 3 3 3… => - 1.1010101.. x 2 1 1 1. 0 1 0 1 0 1 0... -

54 Types and Data –1.986 *10 -7 –878,003,010 –“4UCB” ori $s5, $v0, 17218 Data can be anything; operation of instruction that accesses operand determines its type! Power/danger of unrestricted addresses/pointers: –Use ASCII as FP, instructions as data, integers as instructions,... –Security holes in programs 001101000101 0101 0100 0011 0100 0010

55 IEEE 754 Special Values -(1-2 -24 )*2 128 (1-2 -24 )*2 128 -.5*2 -127.5*2 -127 Positive Overflow Negative Overflow Expressible Positive Numbers Expressible Negative Numbers Positive Underflow Negative Underflow 0 Special ValueExponentSignificand +/- 00000 0 Denormalized number0000 Nonzero NaN1111 Nonzero +/- infinity1111 0

56 Value: Not a Number What is the result of: sqrt(-4.0) or 0/0 ? – If infinity is not an error, these shouldn’t be either. – Called Not a Number (NaN) – Exponent = 255, Significand nonzero Applications – NaNs help with debugging – They contaminate: op(NaN, X) = NaN – Don’t use NaN – Ask math majors

57 Value: Denorms Problem: There’s a gap among representable FP numbers around 0 – Smallest pos num: a = 1.0… 2 * 2 -126 = 2 -126 – 2nd smallest pos num: b = 1.001 2 * 2 -126 = 2 -126 + 2 -150 – a - 0 = 2 -126 – b - a = 2 -150 Solution: – Denormalized numbers: no leading 1 – Smallest pos num: a = 2 -150 – 2nd smallest num: b = 2 -149 b a0 + - Gap! 0 + -

58 Rounding Math on real numbers => rounding Rounding also occurs when converting types – Double  single precision  integer Round towards +infinity – ALWAYS round “up”: 2.001 => 3; -2.001 => -2 Round towards -infinity – ALWAYS round “down”: 1.999 => 1; -1.999 => -2 Truncate – Just drop the last bits (round towards 0) Round to (nearest) even (default) – 2.5 => 2; 3.5 => 4

59 FP Fallacy FP Add, subtract associative: FALSE! – x = – 1.5 x 10 38, y = 1.5 x 10 38, and z = 1.0 – x + (y + z) = –1.5x10 38 + (1.5x10 38 + 1.0) = –1.5x10 38 + (1.5x10 38 ) = 0.0 – (x + y) + z= (–1.5x10 38 + 1.5x10 38 ) + 1.0 = (0.0) + 1.0 = 1.0 Floating Point add, subtract are not associative ! – Why? FP result approximates real result – 1.5 x 10 38 is so much larger than 1.0 that 1.5 x 10 38 + 1.0 in floating point representation is still 1.5 x 10 38

60 Computational Errors with FP

61 FP Addition / Subtraction Much more difficult than with integers Can’t just add significands Algorithm – De-normalize to match exponents – Add (subtract) significands to get resulting one – Keep the same exponent – Normalize (possibly changing exponent) Note: If signs differ, just perform a subtract instead.

62 MIPS FP Architecture (1/2) Separate floating point instructions: – Single Precision: add.s, sub.s, mul.s, div.s – Double Precision: add.d, sub.d, mul.d, div.d These instructions are far more complicated than their integer counterparts Problems: – It’s inefficient to have different instructions take vastly differing amounts of time. – Generally, a particular piece of data will not change from FP to int, or vice versa, within a program. – Some programs do not do floating point calculations – It takes lots of hardware relative to integers to do FP fast

63 MIPS FP Architecture (2/2) 1990 Solution: separate chip that handles only FP. Coprocessor 1: FP chip – Contains 32 32-bit registers: $f0, $f1, … – Most registers specified in.s and.d instructions ($f) – Separate load and store: lwc1 and swc1 (“load word coprocessor 1”, “store …”) – Double Precision: by convention, even/odd pair contain one DP FP number: $f0 / $f1, …, $f30 / $f31 1990 Computer contains multiple separate chips: – Processor: handles all the normal stuff – Coprocessor 1: handles FP and only FP; Move data between main processor and coprocessors: – mfc0, mtc0, mfc1, mtc1, etc.

64 Floating Point Hardware (FP Add)

65 C => MIPS float f2c (float fahr) { return ((5.0 / 9.0) * (fahr – 32.0)); } F2c: lwc1 $f16, const5($gp) # $f16 = 5.0 lwc1 $f18, const9($gp) # $f18 = 9.0 div.s $f16, $f16, $f18 # $f16 = 5.0/9.0 lwc1 $f20, const32($gp) # $f20 = 32.0 sub.s $f20, $f12, $f20 # $f20 = fahr – 32.0 mul.s $f0, $f16, $f20 # $f0 = (5/9)*(fahr-32) jr $ra # return

66 Conclusion Floating Point numbers approximate values that we want to use. IEEE 754 Floating Point Standard is most widely accepted attempt to standardize FP arithmetic New MIPS architectural elements – Registers ( $f0 - $f31 ) – Single Precision (32 bits, 2x10 -38 … 2x10 38 ) add.s, sub.s, mul.s, div.s – Double Precision (64 bits, 2x10 -308 …2x10 308 ) add.d, sub.d, mul.d, div.d Type is not associated with data, bits have no meaning unless given in context (e.g., int vs. float)

67 Number Systems (Review) Introduction Polynomial Expansion Binary Numbers Hexadecimal Numbers Two’s Complement Number System Arithmetic & Overflow Detection American Standard Code for Information Interchange (ASCII)

68 Polynomial Expansion of a Decimal Number (Base 10) 496 = 4 x 10 + 9 x 10 + 6 x 10 Polynomial Expansion of a Binary Number (Base 2) 210 10 00101101 = 1 x 2 + 0 x 2 + 1 x 2 + 1 x 2 + 0 x 2 + 1x 2 543210 2 A Faster Method ------ Double and Add 00101101 = 45 (1, 2, 5, 11, 22, 45)

69 Conversion of Decimal Numbers to Binary Divide by 2 and extract the remainder 45 Remainder1 0 1 1 0 1 221 110 51 21 10 01

70 Practice - Convert 25 to Binary Divide by 2 and record the remainder 25 Remainder 12

71 To represent binary values in the positive and negative domains we use the Two’s Complement Number System Here is the polynomial expansion of a two’s complement 8-bit binary number N: N = - d 7 x2 + d 6 x2 + d 5 x2 + d 4 x2 + d 3 x2 + d 2 x2 + d 1 x2 +d 0 x2 Notice the Minus sign !!!!!!!! *** You need to memorize powers of 2 *** 7564321 0

72 The Two’s Complement Operation When we take the two’s complement of a binary number, the result will be the negative of the value we started with. For example, the 8-bit binary value 00011010 is 26 in decimal. To find the value negative 26 (-26) in binary we perform the two’s complement operation on 00011010. Scan the binary number from right to left leaving all least significant zeros (0) and the first one (1) unchanged, and then complement the remaining digits to the left: 11100110 The result is the value negative 26 (-26) in binary.

73 Binary Arithmetic & Overflow Detection in the Two’s Complement Number System Here is an addition example where we assume we are limited to 8 binary digits. 01000100= 68 +00111100= 60 10000000 = -128 Overflow Occurred

74 Overflow 0000 0010 0001 0011 0100 0101 0110 0111 1000 1001 1010 1011 1101 1110 1111 1 2 0 3 4 5 6 7 -8 -7 -6 -5 -4 -3 -2 1100

75 Binary Arithmetic in the Two’s Complement Number System Here is a subtraction example where we assume we are limited to 8 binary digits. To subtract in binary we always add the two’s complement of the subtrahend. 01000100= 01000100 68 -00111100= +11000100 60 00001000= 00001000 = 8

76 The Rule for Detection of Overflow ################################################# Adding numbers of opposite signs, overflow is impossible. When adding numbers of the same sign, if the result is not the same as the operands then overflow occurred. ################################################# Here is an example: You are given the following two numbers in two’s complement representation. Perform the binary subtraction and indicate if there is signed overflow. ______Explain Why: 11101000 -00010011 11101000 = -24 +11101101 = -19 11010101 Correct Result = -43

77 Sign Extension The value –43 as an 8-bit binary number is: 11010101 The value –43 as an 32-bit binary number is: 11111111111111111111111111010101 In Hexadecimal – 43 appears as: 0xFFFFFFD5 ############################################### The value 68 as an 8-bit binary number is: 01000100 The value 68 as an 32-bit binary number is: 00000000000000000000000001000100 In Hexadecimal 68 appears as: 0x00000044

78 The Hexadecimal Number System DecimalHexBinary 000000 110001 220010 330011 440100 550101 660110 770111 881000 991001 10A1010 11B1011 12C1100 13D1101 14E1110 15F1111 Here is an example of how we compactly represent binary numbers in hexadecimal: | | | | | 001111001000111101111110 0x 3 C 8 F 7 E


Download ppt "UNIT 1 INTRODUCTION NUMBER SYSTEMS AND CONVERSION Click the mouse to move to the next page. Use the ESC key to exit this chapter. This chapter in the book."

Similar presentations


Ads by Google