digital logic design lecture 3 complements, number codes and registers
TRANSCRIPT
Digital Logic DesignLecture 3
Complements, Number Codes and Registers
Overview
° Complement of numbers• Addition and subtraction
° Binary coded decimal
° Gray codes for binary numbers
° ASCII characters
° Moving towards hardware• Storing data
• Processing data
Complements
• In general, we (human beings) express negative numbers by placing a minus (-) sign at the left end of the number. Similarly while representing the integers in binary format, we can leave the left-most bit be the sign bit. If the left-most bit is a zero, the integer is positive; if it is a one, it is negative.
• Zero is positive and -0 = 0
• The top-most bit should tell us the sign of the integer.
• The negative of a negative integer is the original integer ie., -(-55) is 55.
• x - y should give the same result as x + (-y). That is, 8 - 3 should give us the same result as 8 + (-3).
• Negative and positive numbers shouldn't be treated in different ways when we do multiplication and division with them.
Complement
° If we consider only positive numbers, this would allow for all numbers from 0 (naturally represented by 00000000) to and inclusively 255 (represented by 11111111)
° The most obvious solution is to spare the first bit as a sign indicator, thus leaving the 7 last bits to represent the numbers.
° This simple way to represent negative numbers has for it the equally simple way to compute the negative of a given number: just invert the first bit!. Hence, the number 3, for example, represented as (00000011) will give (10000011) for -3 (we have just toggled the first bit).
° This simple way has a drawback: the negative of 0 (00000000) is now (10000000), known as -0
Two’s Complement Representation
• The two’s complement of a binary number involves inverting all bits and adding 1.
• 2’s comp of 00110011 is 11001101
• 2’s comp of 10101010 is 01010110
• For an n bit number N the 2’s complement is (2n-1) – N + 1.
• Called radix complement by Mano since 2’s complement for base (radix 2).
• To find negative of 2’s complement number take the 2’s complement.
000011002 = 1210
Sign bit Magnitude
111101002 = -1210
Sign bit Magnitude
Two’s Complement Shortcuts° Algorithm 1 – Simply complement each bit and
then add 1 to the result.• Finding the 2’s complement of (01100101)2 and of its 2’s
complement…
N = 01100101 [N] = 10011011
10011010 01100100
+ 1 + 1
--------------- ---------------
10011011 01100101
° Algorithm 2 – Starting with the least significant bit, copy all of the bits up to and including the first 1 bit and then complementing the remaining bits.• N = 0 1 1 0 0 1 0 1
[N] = 1 0 0 1 1 0 1 1
Finite Number Representation
° Machines that use 2’s complement arithmetic can represent integers in the range
-2n-1 <= N <= 2n-1-1
where n is the number of bits available for representing N. Note that 2n-1-1 = (011..11)2 and –2n-1 = (100..00)2
oFor 2’s complement more negative numbers than positive.
oFor 1’s complement two representations for zero.
oFor an n bit number in base (radix) z there are zn
different unsigned values.
(0, 1, …zn-1)
1’s Complement Addition
° Using 1’s complement numbers, adding numbers is easy.
° For example, suppose we wish to add +(1100)2 and +(0001)2.
° Let’s compute (12)10 + (1)10.• (12)10 = +(1100)2 = 011002 in 1’s comp.
• (1)10 = +(0001)2 = 000012 in 1’s comp. 0 1 1 0 0 + 0 0 0 0 1-------------- 0 0 1 1 0 1 0-------------- 0 1 1 0 1
Add carry
Final Result
Step 1: Add binary numbersStep 2: Add carry to low-order bit
Add
1’s Complement Subtraction
° Using 1’s complement numbers, subtracting numbers is also easy.
° For example, suppose we wish to subtract +(0001)2 from +(1100)2.
° Let’s compute (12)10 - (1)10.• (12)10 = +(1100)2 = 011002 in 1’s comp.
• (-1)10 = -(0001)2 = 111102 in 1’s comp.
0 1 1 0 0 - 0 0 0 0 1--------------
0 1 1 0 0 + 1 1 1 1 0-------------- 1 0 1 0 1 0 1-------------- 0 1 0 1 1
Add carry
Final Result
Step 1: Take 1’s complement of 2nd operandStep 2: Add binary numbersStep 3: Add carry to low order bit
1’s comp
Add
2’s Complement Addition
° Using 2’s complement numbers, adding numbers is easy.
° For example, suppose we wish to add +(1100)2 and +(0001)2.
° Let’s compute (12)10 + (1)10.• (12)10 = +(1100)2 = 011002 in 2’s comp.
• (1)10 = +(0001)2 = 000012 in 2’s comp. 0 1 1 0 0 + 0 0 0 0 1-------------- 0 0 1 1 0 1
FinalResult
Step 1: Add binary numbersStep 2: Ignore carry bit
Add
Ignore
2’s Complement Subtraction
° Using 2’s complement numbers, follow steps for subtraction
° For example, suppose we wish to subtract +(0001)2 from +(1100)2.
° Let’s compute (12)10 - (1)10.• (12)10 = +(1100)2 = 011002 in 2’s comp.
• (-1)10 = -(0001)2 = 111112 in 2’s comp.
0 1 1 0 0 - 0 0 0 0 1--------------
0 1 1 0 0 + 1 1 1 1 1-------------- 1 0 1 0 1 1
Final Result
Step 1: Take 2’s complement of 2nd operandStep 2: Add binary numbersStep 3: Ignore carry bit
2’s comp
Add
IgnoreCarry
2’s Complement Subtraction: Example #2
° Let’s compute (13)10 – (5)10.• (13)10 = +(1101)2 = (01101)2
• (-5)10 = -(0101)2 = (11011)2
° Adding these two 5-bit codes…
° Discarding the carry bit, the sign bit is seen to be zero, indicating a correct result. Indeed,
(01000)2 = +(1000)2 = +(8)10.
0 1 1 0 1 + 1 1 0 1 1-------------- 1 0 1 0 0 0
carry
2’s Complement Subtraction: Example #3° Let’s compute (5)10 – (12)10.
• (-12)10 = -(1100)2 = (10100)2
• (5)10 = +(0101)2 = (00101)2
° Adding these two 5-bit codes…
° Here, there is no carry bit and the sign bit is 1. This indicates a negative result, which is what we expect. (11001)2 = -(7)10.
0 0 1 0 1 + 1 0 1 0 0-------------- 1 1 0 0 1
2’s Complement Subtraction
° Let’s compute (13)10 - (5)10.• (13)10 = +(1101)2 = (01101)2
• (-5)10 = -(0101)2 = (11011)2
° Adding these two 5-bit codes…
° Discarding the carry bit, the sign bit is seen to be zero, indicating a correct result.
0 1 1 0 1 + 1 1 0 1 1-------------- 1 0 1 0 0 0
carry
Data Representation and Communication° Human communication
Includes language, images and sounds
° Computers Process and store all forms of data in binary format
° Conversion to computer-usable representation using data formats
Define the different ways human data may be represented, stored and processed by a computer
° Numbers are important to computers as they • Represent information precisely
• Can be processed
° Decimal numbers are favored by humans. Binary numbers are natural to computers. Hence, conversion is required.
° If little calculation is required, we can use some coding schemes to store decimal numbers, for data transmission purposes.
° Examples: BCD (or 8421), Excess-3, 8-4-2-1, 2421, etc.
° Each decimal digit is represented as a 4-bit code.
° The number of digits in a code is also called the length of the code.
Data Representation and Communication
BCD
Digit Bit pattern
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
Note: the following 6 bit patterns are not used:
101010111100110111101111
° Binary Coded Decimal (BCD) represents each decimal digit with four bits• Ex. 0011 0010 1001 = 32910
° This is NOT the same as 0011001010012
° Why do this? Because people think in decimal.
Putting It All Together
° BCD not very efficient
° Used in early computers (40s, 50s)
° Used to encode numbers for seven-segment displays.
° Easier to read?
Gray Code
° Gray code is not a number system.• It is an alternate way to represent
four bit data
° Only one bit changes from one decimal digit to the next
° Useful for reducing errors in communication.
° Can be scaled to larger numbers.
Digit Binary Gray Code
0 0000 0000 1 0001 0001 2 0010 0011 3 0011 0010 4 0100 0110 5 0101 0111 6 0110 0101 7 0111 0100 8 1000 1100 9 1001 1101 10 1010 1111 11 1011 1110 12 1100 1010 13 1101 1011 14 1110 1001 15 1111 1000
ASCII Code
° American Standard Code for Information Interchange
° ASCII is a 7-bit code, frequently used with an 8th bit for error detection (more about that in a bit).
Character ASCII (bin) ASCII (hex) Decimal OctalA 1000001 41 65 101B 1000010 42 66 102C 1000011 43 67 103…
Z
a
…
1
‘
21
000 001 010 011 100 101 110 1110000 NULL DLE 0 @ P ` p0001 SOH DC1 ! 1 A Q a q0010 STX DC2 " 2 B R b r0011 ETX DC3 # 3 C S c s0100 EDT DC4 $ 4 D T d t0101 ENQ NAK % 5 E U e u0110 ACK SYN & 6 F V f v0111 BEL ETB ' 7 G W g w1000 BS CAN ( 8 H X h x1001 HT EM ) 9 I Y i y1010 LF SUB * : J Z j z1011 VT ESC + ; K [ k {1100 FF FS , < L \ l |1101 CR GS - = M ] m }1110 SO RS . > N ^ n ~1111 SI US / ? O _ o DEL
Most significant bit
Least significant bit
ASCII Code
22
000 001 010 011 100 101 110 1110000 NULL DLE 0 @ P ` p0001 SOH DC1 ! 1 A Q a q0010 STX DC2 " 2 B R b r0011 ETX DC3 # 3 C S c s0100 EDT DC4 $ 4 D T d t0101 ENQ NAK % 5 E U e u0110 ACK SYN & 6 F V f v0111 BEL ETB ' 7 G W g w1000 BS CAN ( 8 H X h x1001 HT EM ) 9 I Y i y1010 LF SUB * : J Z j z1011 VT ESC + ; K [ k {1100 FF FS , < L \ l |1101 CR GS - = M ] m }1110 SO RS . > N ^ n ~1111 SI US / ? O _ o DEL
e.g., ‘a’ = 1100001
ASCII Code
23
Alphabetic codes
000 001 010 011 100 101 110 1110000 NULL DLE 0 @ P ` p0001 SOH DC1 ! 1 A Q a q0010 STX DC2 " 2 B R b r0011 ETX DC3 # 3 C S c s0100 EDT DC4 $ 4 D T d t0101 ENQ NAK % 5 E U e u0110 ACK SYN & 6 F V f v0111 BEL ETB ' 7 G W g w1000 BS CAN ( 8 H X h x1001 HT EM ) 9 I Y i y1010 LF SUB * : J Z j z1011 VT ESC + ; K [ k {1100 FF FS , < L \ l |1101 CR GS - = M ] m }1110 SO RS . > N ^ n ~1111 SI US / ? O _ o DEL
ASCII Code
24
Numeric codes
000 001 010 011 100 101 110 1110000 NULL DLE 0 @ P ` p0001 SOH DC1 ! 1 A Q a q0010 STX DC2 " 2 B R b r0011 ETX DC3 # 3 C S c s0100 EDT DC4 $ 4 D T d t0101 ENQ NAK % 5 E U e u0110 ACK SYN & 6 F V f v0111 BEL ETB ' 7 G W g w1000 BS CAN ( 8 H X h x1001 HT EM ) 9 I Y i y1010 LF SUB * : J Z j z1011 VT ESC + ; K [ k {1100 FF FS , < L \ l |1101 CR GS - = M ] m }1110 SO RS . > N ^ n ~1111 SI US / ? O _ o DEL
ASCII Code
25
000 001 010 011 100 101 110 1110000 NULL DLE 0 @ P ` p0001 SOH DC1 ! 1 A Q a q0010 STX DC2 " 2 B R b r0011 ETX DC3 # 3 C S c s0100 EDT DC4 $ 4 D T d t0101 ENQ NAK % 5 E U e u0110 ACK SYN & 6 F V f v0111 BEL ETB ' 7 G W g w1000 BS CAN ( 8 H X h x1001 HT EM ) 9 I Y i y1010 LF SUB * : J Z j z1011 VT ESC + ; K [ k {1100 FF FS , < L \ l |1101 CR GS - = M ] m }1110 SO RS . > N ^ n ~1111 SI US / ? O _ o DEL
Punctuation, etc.
ASCII Code
ASCII Codes and Data Transmission
° ASCII Codes
° A – Z (26 codes), a – z (26 codes)
° 0-9 (10 codes), others (@#$%^&*….)
° Complete listing in Mano text
° Transmission susceptible to noise
° Typical transmission rates (1500 Kbps, 56.6 Kbps)
° How to keep data transmission accurate?
Parity Codes
° Parity codes are formed by concatenating a parity bit, P to each code word of C.
° In an odd-parity code, the parity bit is specified so that the total number of ones is odd.
° In an even-parity code, the parity bit is specified so that the total number of ones is even.
Information BitsP
1 1 0 0 0 0 1 1
Added even parity bit
0 1 0 0 0 0 1 1
Added odd parity bit
Parity Code Example
° Concatenate a parity bit to the ASCII code for the characters 0, X, and = to produce both odd-parity and even-parity codes.
Character ASCII Odd-Parity ASCII
Even-Parity ASCII
0 0110000 10110000 00110000
X 1011000 01011000 11011000
= 0111100 10111100 00111100
Binary Data Storage
• Binary cells store individual bits of data
• Multiple cells form a register.
• Data in registers can indicate different values
• Hex (decimal)
• BCD
• ASCII
Binary Cell
0 0 1 0 1 0 1 1
Register Transfer
° Data can move from register to register.
° Digital logic used to process data
° We will learn to design this logic
Register A Register B
Register C
Digital Logic Circuits
Transfer of Information
° Data input at keyboard
° Shifted into place
° Stored in memory
NOTE: Data input in ASCII
Building a Computer
° We need processing
° We need storage
° We need communication
° You will learn to use and design these components.
Summary° 2’s complement most important (only 1
representation for zero).
° Important to understand treatment of sign bit for 1’s and 2’s complement.
° Although 2’s complement most important, other number codes exist
° ASCII code used to represent characters (including those on the keyboard)
° Registers store binary data