670 likes | 838 Views
EE345 - Micro-Controllers Digital Systems and Binary Numbers. Prof. Ahmad Abu-El-Haija. Acknowledgement.
E N D
EE345 - Micro-ControllersDigital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija
Acknowledgement • This presentation is a modified version of lecture notes prepared by Dr. Pradondet Nilagupta, Kasetsart University. The latter is also a modified version based upon presentations by Prof. Maciej Ciesielski and Prof. Tilman Wolf, University of Massachusetts Amherst, and original slides from the publisher. Digital System Design
+5 +5 1 0 1 V V T ime T ime –5 –5 Digital Hardware Systems Digital Systems Digital vs. Analog Waveforms Digital: only assumes discrete values Analog: values vary over a broad range continuously EE345 – Micro-Controllers
Digital circuit can be represented by a black-box with inputs on one side, and outputs on the other. Digital circuit inputs outputs : : High Low Digital Circuits The input/output signals are discrete/digital in nature, typically with two distinct voltages (a high voltage and a low voltage). In contrast, analog circuits use continuous signals. EE345 – Micro-Controllers
Digital Hardware Systems • Digital Binary System • Two discrete values: • yes, on, 5 volts, current flowing, "1" • no, off, 0 volts, no current flowing, "0” • Advantage of binary systems: • rigorous mathematical foundation based on logic • it’s easy to implement both the door must be open and the car running before I can back out IF the garage door is open AND the car is running THEN the car can be backed out of the garage the preconditions must be true to imply the conclusion EE345 – Micro-Controllers
Binary Bit and Group Definitions • Bit - a single binary digit • Nibble - a group of four bits • Byte - a group of eight bits • Word - depends on processor; 8, 16, 32, or 64 bits • LSB - Least Significant Bit (on the right) • MSB - Most Significant Bit (on the left) EE345 – Micro-Controllers
Binary Representation of Information • Information divided into groups of symbols • 26 English letters • 28 Arabic letters • 10 decimal digits • 22 Arab countries • Digital systems manipulate information as 1’s & 0’s • The mapping of symbols to binary value is known as a “code” • The mapping must be unique EE345 – Micro-Controllers
Digital Systems • Digital systems operate on discrete elements of information • Numbers (e.g., pocket calculator) -> “digits” -> “digital” • Letters (e.g., word processor) • Pictures (e.g., digital cameras) • For a digital systems to operate on a continuous data, it needs to quantize (digitize) that data first • Covert data into digital representation • Topics: • How are numbers represented in digital systems • How computer performs basic arithmetic operations EE345 – Micro-Controllers
Numbers • Common numbering system is “base10” • Why? • Numbers in base 10 • Ten different digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 • Number is represented by a sequence of digits: an an-1 … a1 a0 • Value of number is: an×10n+an-1×10n-1+…+a1×101+a0×100 • Positional notation • General equation: • May contain a decimal point • Negative index for digits after decimal point • Examples • 1234.56 – if ambiguous, write (1234.56)10 • Leading zeros cause no problems: 00001234.56 EE345 – Micro-Controllers
Number Systems • General form, with base r : • Base r is also called radix • In decimal system r = 10; in binary r = 2 • Coefficients in positional notation are: 0,1,…, r-1. • What is the range of values of an n-bit number in radix r ? • Minimum value: 0 • Maximum value: rn-1 • Number of different values: rn EE345 – Micro-Controllers
Positional Number Systems • Numeric value is represented by a series of digits • Number of digits used is fixed by radix • Digits multiplied by a power of the radix • Digit order determines radix powers • Very large numbers can be represented • Can also represent fractional values. EE345 – Micro-Controllers
Positional Integer Number Values Given a digit series of The full expression for the represented value is EE345 – Micro-Controllers
Positional Fractional Number Values Given a digit series of The full expression for the represented value is EE345 – Micro-Controllers
Binary Numbers • Base 2 number use only two digits: 0, 1 • Why? • Digits need to be represented in a system • Electronic systems typically use voltage levels • Representing 10 different voltages reliably is difficult • Binary decision is much easier (On, Off) • Binary representation is ideal • Minimal number of digits • Easily represented in voltages EE345 – Micro-Controllers
Examples for Binary Numbers • What value is represented by (01001)2? • Leading zero makes no difference • (1001)2 translates into 1×23+0×22+0×21+1×20=8+0+0+1=(9)10 • Same process for numbers with decimal point • What is the value of (1001.1001)2? • (1001.1001)2 = 1×23+0×22+0×21+1×20+1×2-1+0×2-2+0×2-3+1×2-4=8+0+0+1+1/2+0+0+1/16=(9.5625)10 • Important: it’s NOT (9.9)10! • Can you count binary? • How far can you count with 10 fingers? EE345 – Micro-Controllers
Binary Number Terminology • Base is also called “radix” • Binary numbers are made of binary digits (bits) • Groups of four bits are called “nibbles” • E.g., (1101)2 • Groups of eight bits are called “bytes” • E.g., (01001101)2 • What is the range of values of an n-bit binary number? • Minimum value: 0 • Maximum value: 2n-1 • Number of different values: 2n • Powers of 2 are important EE345 – Micro-Controllers
Powers of 2 • You must memorize all powers of 2 up to 216! • Other important powers of 2: • Trick to simplify estimation: • 210=1024≈1000=103 • Example: 232=4×230≈4×109=4 billion • Prefixes: • kilo (103 ≈ 210), Mega (106 ≈ 220), Giga (109 ≈ 230), Tera (1012 ≈ 240), … • Computer systems are typically based on powers of 2 EE345 – Micro-Controllers
octal (base 8) decimal (base 10) binary (base 2) hexadecimal (base16) Other Number Systems • Octal number system • Digits 0, 1, … 7 • Aggregates 3 digits of binary system (i.e., 3-bit number) • E.g., (57)8 = (47)10 • Hexadecimal number system • 16 digits require 6 new digit symbols: 0, … 9, A, B, C, D, E, F • Aggregates 4 digits of binary system (1 nibble or half byte) • E.g., (1F)16 = (31)10 • Conversion between system: EE345 – Micro-Controllers
Number Base Conversions • Conversion from base r to decimal • Expansion to power series and addition of terms • Conversion from decimal to base r • Divide number and successive quotients by r • Sequence of remainders is base r number • Example: convert (41)10 to binary (r=2) • 41 = 20 2 + 1 • 20 = 10 2 + 0 • 10 = 5 2 + 0 • 5 = 2 2 + 1 • 2 = 1 2 + 0 • 1 = 0 2 + 1 a0 = 1 a1 = 0 a2 = 0 a3 = 1 a4 = 0 a5 = 1 (101001)2 EE345 – Micro-Controllers
(4 5 5 6)8 (9 6 E)16 Number Base Conversions • Conversion to/from octal and hexadecimal • Easier if done via binary • 3 or 4 bit sequences correspond to digit • Example: (2414)10=(100101101110)2 EE345 – Micro-Controllers
Number Systems - Summary • Base r numbers • Binary • Octal • Hexadecimal • Conversion between number systems • Summation of power series • Division with remainders • Powers of 2 • Computer systems based on powers of 2 • kilo = 1024 • Mega = 1024 × 1024 • Giga = 1024 × 1024 × 1024 • Next: • Computer arithmetic • Signs, complements EE345 – Micro-Controllers
Computer Arithmetic • Computer arithmetic • Addition, subtraction • Multiplication • Signed numbers • Complements EE345 – Micro-Controllers
Binary Addition • Binary addition works like “normal” addition • Stay within {0,1} • Carries as usual • Example: Carry: 111111 augend: addend: 111101 + 10111 1010100 • Addition of multiple numbers possible • Carry gets a bit more difficult EE345 – Micro-Controllers
Binary Subtraction • Subtraction same as “normal” subtraction • Borrows as usual • Example: 10 0 – – 0 10 – – minuend: subtrahend: 111101 - 10111 1 0 0 1 1 0 EE345 – Micro-Controllers
Binary Multiplication • Binary multiplication • Same as “normal” multiplication • Multiplication a lot easier in binary domain • Example: multiplicand multiplier 111101 × 1010 000000 111101 000000 111101 1001100010 EE345 – Micro-Controllers
Signed Numbers • How are signed numbers handled in base 10? • Plus or minus sign placed in front of number • Can we do that for binary numbers? • Sign needs to be represented in digital system • Only choice are ‘0’ and ‘1’ • ‘0’ indicates ‘+’ • ‘1’ indicates ‘–’ • Examples on five-bit numbers: • 01101 11101 00000 10000 • + 13 – 13 + 0 – 0 • Signed Magnitude representation EE345 – Micro-Controllers
Arithmetic with Signed Magnitude • Addition example 1: • Plus signs are leading zeros -> no problem Augend: 01001 (9)10 Addend: + 00010+ (2)10 01011 (11)10 • Addition example 2: • What happens with negative numbers? 01001 (9)10 + 10010+ (–2)10 11011 ??? (7)10 • Problem: negative numbers • Sign can turn addition into subtraction EE345 – Micro-Controllers
0000 0001 1111 1110 0010 +0 -7 +1 -6 +2 1101 0011 -5 +3 +4 0100 1100 -4 +5 -3 1011 0101 +6 -2 +7 -1 -0 0110 1010 1001 0111 Signed Magnitude Representation • Arithmetic with signed magnitude is difficult • Two representations of zero • Different cases for addition and subtraction 1000 EE345 – Micro-Controllers
Complements • “Complements” allow easier arithmetic • Representation of negative numbers a bit more involved • Two types of complements • Radix complement: r’ s complement • Decimal: 10’s complement • Binary: 2’s complement • Diminished radix complement: (r–1)’s complement • Decimal: 9’s complement • Binary: 1’s complement EE345 – Micro-Controllers
Diminished Radix Complement • Given a number N in base r having n digits, the (r–1)’s complement of N is defined as: (r n –1) – N • Example for 6-digit decimal numbers: • 9’s complement is (r n–1)–N = (106–1)–N = 999999–N • 9’s complement of 546700 is 999999–546700 = 453299 • Example for 7-digit binary numbers: • 1’s complement is (r n –1) – N = (27–1)–N = 1111111–N • 1’s complement of 1011000 is 1111111–1011000 = 0100111 • Observation: • Subtraction from (rn–1) will never require a borrow • Diminished radix complement can be computed digit-by-digit • For binary: 1 – 0 = 1 and 1 – 1 = 0 • Flips 0’s to 1’s and 1’s to 0’ (bit complementation) EE345 – Micro-Controllers
0000 0001 1111 1110 0010 +0 -0 +1 -1 +2 1101 0011 -2 +3 +4 0100 1100 -3 +5 -4 1011 0101 +6 -5 +7 -6 -7 0110 1010 1001 0111 One’s Complement Representation • 1’s complement is simple to compute • Bit complementation • Still, two representations of zero 1000 EE345 – Micro-Controllers
Radix Complement • The r’s complement of an n-digit number N in base r is defined as r n – N for N ≠ 0 and 0 for N = 0 • Radix complement is diminished radix complement + 1:(r n –1) – N +1 = r n – N • Example for 6-digit decimal numbers: • 10’s complement is r n – N = 106–N = 1000000–N • 10’s complement of 546700 is 1000000–546700= 453300 • Rule:Leave least significant 0’s unchanged, subtract first nonzero least significant digit from 10, subtract all higher significant digits from 9. • Example for 7-digit binary numbers: • 2’s complement is r n–N = 27–N = 10000000–N • 2’s complement of 1011000 is 10000000–1011000 = 0101000 • Rule: “Leave least significant 0’s and first 1 unchanged, replace 1’s with 0’s and 0’s with 1’s in all higher significant digits.” EE345 – Micro-Controllers
0000 0001 1111 1110 0010 -1 +1 -2 +2 1101 0011 -3 +3 +4 0100 1100 -4 +5 -5 1011 0101 +6 -6 +7 -7 -8 0110 1010 1001 0111 Two’s Complement Representation • Arithmetic with 2’s complement is most efficient • Bit complementation + 1 • Single representation of zero! 0 1000 EE345 – Micro-Controllers
Complements - Summary • Complement of complement is original number • Diminished radix complement: (r n–1)–((r n–1)–N) = r n–1–r n+1 + N = N • Radix complement: r n–(r n–N) = r n–r n+ N = N • Representation of zero • Radix complement: 0 • Diminished radix complement: r n –1 and it’s complement 0 • Radix points • Remove radix point • Compute complement • Put radix point back at same relative position EE345 – Micro-Controllers
Signed Binary Numbers Comparison ( n=4 ) Decimal Signed Magnitude signed One’s Complement Signed Two’s Complement -8------- 1000 -7 1111 1000 1001 -6 1110 1001 1010 -5 1101 1010 1011 -4 1100 1011 1100 -3 1011 1100 1101 -2 1010 1101 1110 -1 1001 1110 1111 0 0000 or 1000 0000 or 1111 0000 1 0001 0001 0001 2 0010 0010 0010 3 0011 0011 0011 4 0100 0100 0100 5 0101 0101 0101 6 0110 0110 0110 7 0111 0111 0111 EE345 – Micro-Controllers
Subtraction with Complements • Subtraction M – N • Add minuend M to r’s complement of subtrahend N • M – N = M + (rn – N) = M – N + r n • What about the additional r n? • If M N • Then M – N + r nr n • With n bits, only numbers < r n can be expressed • n+1st digit is ignored (drop the “carry out” bit) • If M < N • Then M – N + r n = r n – (N – M) = r ’s complement of (N-M) • r ’s complement signifies negative number • So, – (N – M) = M – N EE345 – Micro-Controllers
Arithmetic with Radix Complement • Subtraction works as addition of complement • Addition/subtraction of signed numbers • Negative numbers are expressed as r ’s complement • Simple addition yields correct result • No need to distinguish different cases • Addition/subtraction of unsigned numbers • Can be performed on same hardware as signed numbers • Most modern digital systems use 2’s complement EE345 – Micro-Controllers
Two’s Complement Overflow Consider two 8-bit 2’s complement numbers. We can represent the signed integers -128 to +127 using this representation.What if we do (+1) + (+127) = +128. The number +128 is OUT of the RANGE that we can represent with 8 bits. What happens when we do the binary addition? +127 = 7F + 1 = 01------------------- 128 ≠ 80 (this is actually -128 as a two’s complement number!!! - the wrong answer!!!)How do we know if overflow occurred? Added two POSITIVE numbers, and got a NEGATIVE result. EE345 – Micro-Controllers
Detecting Two’s Complement Overflow Two’s complement overflow occurs when we: Add two POSITIVE numbers and get a NEGATIVE result, or Add two NEGATIVE numbers and get a POSITIVE resultWe CANNOT get two’s complement overflow if we add a NEGATIVE and a POSITIVE number together. The Carry out of the Most Significant Bit means nothing if the numbers are two’s complement numbers. EE345 – Micro-Controllers
Weighted and Unweighted Codes • Most numeric number representations are in a class known as “Weighted Codes” where • Binary integers and fractions are special case where weights are powers of 2 • Unweighted codes are codes that cannot be assigned a weight value for each bit EE345 – Micro-Controllers
Binary Coded Decimal • Four bits are used to represent each decimal digit • In each 4-bit group, 6 values are not used • Many possible codes, natural BCD (equivalent binary digits) most common • BCD is not as efficient as binary • BCD is easy to convert to/from decimal (it is really decimal with different symbols) • BCD add/subtract circuits are complex EE345 – Micro-Controllers
Other Decimal Codes Weighted codes Unweighted code The 8421 or natural BCD code is the most common BCD code in use EE345 – Micro-Controllers
BCD Addition Case 1: Case 2: 0001 1 0101 5 (0) 0110 (0) 6 0110 6 0101 5 (0) 1011 (1) 1 WRONG! Case 3: 1000 8 1001 9 (1) 0001 (1) 7 Note that for cases 2 and 3, adding a factor of 6 (0110) gives us the correct result. EE345 – Micro-Controllers
BCD Addition (cont.) • BCD addition is therefore performed as follows • 1) Add the two BCD digits together using normal binary addition • 2) Check if correction is needed • a) 4-bit sum is in range of 1010 to 1111 • b) carry out of MSB = 1 • 3) If correction is required, add 0110 to 4-bit sum to get the correct result; BCD carry out = 1 EE345 – Micro-Controllers
BCD Negative Number Representation • Similar to binary negative number representation except r = 10. • BCD sign-magnitude • MSD (sign digit options) • MSD = 0 (positive); not equal to 0 = negative • MSD range of 0-4 positive; 5-9 negative • BCD 10’s complement • -N 10r- N; 9’s complement + 1 • BCD 9’s complement • invert each BCD digit (09, 1 8, 2 7,3 6, …7 2, 8 1, 9 0) EE345 – Micro-Controllers
Decimal Arithmetic • Everything needs to be 4-bit aligned • ‘+’ represented by 0 (=‘0000’) • ‘–’ represented by 9 (=‘1001’) • Signed magnitude representation or complements • Signed magnitude hardly used • 10’s complement most common • Example: 375 + (–240) • Negative numbers represented by 10’s complement • 10’s complement of 240 is 104 – 240 = 9760 • Addition of all digits and discard of end carry: • Sign of result automatically correct 0 375 + 9 760 0 135 EE345 – Micro-Controllers
Gray Codes Gray Code Dec. Equiv.0000 00001 10011 20010 30110 40111 50101 60100 71100 81101 9 1111 101110 111010 121011 131001 14 1000 15 • Gray codes are minimum change codes • From one numeric representation to the next, only one bit changes • Primary use is in numeric input encoding applications where we expect non-random input values changes (i.e. value n to either n-1 or n+1) EE345 – Micro-Controllers
Alphanumeric Representation • Binary codes used to represent alphabetic and numeric characters • Two most common are: • ASCII, 7 bit code, 128 symbols • EBCDIC, 8 bit code, 256 symbols • Problems can arise when comparing symbol values (collation) • Comparing ‘A’ to ‘a’ in ASCII system yields different results in an EBCDIC system. EE345 – Micro-Controllers
ASCII CODE The ASCII code (American Standard Code for Information Interchange) is a 7-bit code for character data. Typically 8 bits are actually used with the 8th bit being zero or used for error detection (parity checking).8 bits = 1 byte. ‘A’ = % 01000001 = $41 ‘&’ = % 00100110 = $267 bits can only represent 27 (128) different values. This is enough to represent the Latin alphabet (A-Z, a-z, 0-9, punctuation marks, some symbols like $), but what about other symbols or other languages (e.g., Arabic)? EE345 – Micro-Controllers
ASCII CODE EE345 – Micro-Controllers