basic arithmetic (adding and subtracting) digital logic to show add/subtract boolean algebra...

30
Basic Arithmetic (adding and subtracting) Digital logic to show add/subtract Boolean algebra abstraction of physical, analog circuit behavior 1 0 CPU components – ALU logic circuits logic gates transistors

Upload: gordon-barker

Post on 28-Dec-2015

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Basic Arithmetic (adding and subtracting) Digital logic to show add/subtract Boolean algebra abstraction of physical, analog circuit behavior 1 0 CPU components

Basic Arithmetic (adding and subtracting) 

Digital logic to show add/subtractBoolean algebra abstraction of physical, analog circuit behavior

1

0

CPUcomponents – ALUlogic circuitslogic gatestransistors

Page 2: Basic Arithmetic (adding and subtracting) Digital logic to show add/subtract Boolean algebra abstraction of physical, analog circuit behavior 1 0 CPU components

Digital Logic

A B A * B0 0 00 1 01 0 01 1 1

A B A B0 0 00 1 01 0 01 1 1

AB

out

outAB

and (* or ^)

or ( )

Page 3: Basic Arithmetic (adding and subtracting) Digital logic to show add/subtract Boolean algebra abstraction of physical, analog circuit behavior 1 0 CPU components

Digital Logic

A out

outAB

not (~, ⌐, −)

xor ( )

A ~A0 11 0

A BA B

0 0 00 1 11 0 11 1 0

Page 4: Basic Arithmetic (adding and subtracting) Digital logic to show add/subtract Boolean algebra abstraction of physical, analog circuit behavior 1 0 CPU components

Digital Logic

AB

out

outAB

nand

nor

A B0 0 10 1 11 0 11 1 0

B*A

A BA nor

B0 0 10 1 01 0 01 1 0

Page 5: Basic Arithmetic (adding and subtracting) Digital logic to show add/subtract Boolean algebra abstraction of physical, analog circuit behavior 1 0 CPU components

Digital Logic

Given the truth table:

F = + + +

Sum of products from the truth table. Often we can simplify.

  A B F

0 0 10 1 11 0 11 1 1

BABABABA

BA BA BA BA

Page 6: Basic Arithmetic (adding and subtracting) Digital logic to show add/subtract Boolean algebra abstraction of physical, analog circuit behavior 1 0 CPU components

Unsigned ArithmeticBinary Addition

A B cout sum

0 0 0 10 1 0 11 0 0 11 1 1 0

Input Output

A B S C

0 0 0 0

0 1 1 0

1 0 1 0

1 1 0 1

cout

sumAB

Half Adder

Page 7: Basic Arithmetic (adding and subtracting) Digital logic to show add/subtract Boolean algebra abstraction of physical, analog circuit behavior 1 0 CPU components

Unsigned Arithmetic

The half adder is an example of a simple digital circuit built from two logic gates.

half adder logic - two one-bit inputs (a, b) and two one-bit outputs (carry_out, sum)  a 0 0 1 1 + b + 0 + 1 + 0 +1

carry_out sum 00 01 01 10

  carry_out = a and b sum = a xor b

Page 8: Basic Arithmetic (adding and subtracting) Digital logic to show add/subtract Boolean algebra abstraction of physical, analog circuit behavior 1 0 CPU components

Unsigned Arithmetic

The problem with a half-adder is that it doesn't handle carries.

Consider adding the following two numbers: 1 1 1 + 0 1 1

When we add the two numbers, we get1 1

1 1 1 + 0 1 1 (1) 0 1 0

Look at the middle and leftmost columns. You add 3 bits. Half adders can only add two bits.

Page 9: Basic Arithmetic (adding and subtracting) Digital logic to show add/subtract Boolean algebra abstraction of physical, analog circuit behavior 1 0 CPU components

Unsigned Arithmetic

For adding multi-bit fields/words, e.g., 4 bits  a_3 a_2 a_1 a_0 + b_3 b_2 b_1 b_0 --------------------------------------- sum_3 sum_2 sum_1 sum_0

we also need to add a carry_in with a_i and b_i, where i>0  

Page 10: Basic Arithmetic (adding and subtracting) Digital logic to show add/subtract Boolean algebra abstraction of physical, analog circuit behavior 1 0 CPU components

Unsigned Arithmetic

A full adder for a_i + b_i + carry_in is given in the figure below.– three one-bit inputs (a, b, carry_in) and– two one-bit outputs (carry_out, sum)– cascade two half adders (sum output bit of first

attached to one input line of the other) and then or together the carry_outs

  

InputOutpu

tC A B S C0 0 0 0 01 0 0 1 00 1 0 1 01 1 0 0 10 0 1 1 01 0 1 0 10 1 1 0 11 1 1 1 1

Cin

AB

sum

Cout

Page 11: Basic Arithmetic (adding and subtracting) Digital logic to show add/subtract Boolean algebra abstraction of physical, analog circuit behavior 1 0 CPU components

Full Adder• An n-bit adder built by connecting n full adders• carries propagate from right to left (i.e., connect

the carry_out of an adder to the carry_in of the adder in the next leftmost bit position

• the initial, that is, rightmost, carry_in is zero)

overflow occurs when a number is too large to represent for unsigned arithmetic, overflow occurs when a carry out occurs from the most significant (i.e., leftmost) bit position

Page 12: Basic Arithmetic (adding and subtracting) Digital logic to show add/subtract Boolean algebra abstraction of physical, analog circuit behavior 1 0 CPU components

Full Adder

(there are faster forms of addition hardware where the carries do not have to propagate from one side to the other, e.g., carry-lookahead adder)

Page 13: Basic Arithmetic (adding and subtracting) Digital logic to show add/subtract Boolean algebra abstraction of physical, analog circuit behavior 1 0 CPU components

Signed Arithmetic

fundamental idea #1 finite width arithmetic- modulus rn, where r is radix, n is number of digits wide

- wraps around from biggest number to zero, ignoring overflow

  e.g., 4-bit arithmetic => modulus is 24 = 16 0, 1, 2, ..., 15 then wrap around back to 0 

thus an addition of rn to an n-digit finite width value has no effect on the n-digit value

overflow occurs when a number is too large to represent for unsigned arithmetic, overflow occurs when a carry out occurs from the most significant (i.e., leftmost) bit position (there are faster forms of addition hardware where the carries do not have to propagate from one side to the other, e.g., carry-lookahead adder)

 

Page 14: Basic Arithmetic (adding and subtracting) Digital logic to show add/subtract Boolean algebra abstraction of physical, analog circuit behavior 1 0 CPU components

Signed Arithmetic

fundamental idea #2  subtraction is equivalent to adding the negative of number

  e.g., a - b = a + (-b)

  observation  a - b == a - b + rn == a + (rn - b)  \______/ \_______/ #1 #2 \______/ this term is our representation for (-b)

it turns out that we can more easily perform rn - b than a - b

 

Page 15: Basic Arithmetic (adding and subtracting) Digital logic to show add/subtract Boolean algebra abstraction of physical, analog circuit behavior 1 0 CPU components

Signed Arithmetic

digit complement for n digits == (rn - 1) - number in binary, this is called one's complement and equals a value of n ones minus the bits of the number

for binary, one's complement (2n - 1 - number) is equivalent to inverting each bit 

in decimal, this is called nine's complement and equals a value of n nines minus the digits of the number 

in hexadecimal, this is n f's (fifteens) minus the digits of the number 

 

Page 16: Basic Arithmetic (adding and subtracting) Digital logic to show add/subtract Boolean algebra abstraction of physical, analog circuit behavior 1 0 CPU components

Signed Arithmetic

radix complement for n digits == (rn - 1) - number + 1

two's complement in binary ten's complement in decimal

 for binary, two's complement (2n - 1 - number + 1) is equivalent to inverting each bit and adding one

 One's complement format of a number

– Change the number to binary, ignoring the sign.– Add 0s to the left of the binary number to make a

total of n bits– If the sign is positive, no more action is needed.– If the sign is negative, complement every bit (i.e.

change from 0 to 1 or from 1 to 0) 

 

Page 17: Basic Arithmetic (adding and subtracting) Digital logic to show add/subtract Boolean algebra abstraction of physical, analog circuit behavior 1 0 CPU components

Signed Arithmetic

Most computers today use 2's complement representation for negative numbers. 

The 2's complement of a negative number is obtained by adding 1 to the 1's complement. i.e. 

Two's complement format of a number– Change the number to binary, ignoring the sign.– Add 0s to the left of the binary number to make a

total of n bits– If the sign is positive, no more action is needed.– If the sign is negative, complement every bit (i.e.

change from 0 to 1 or from 1 to 0) and add 1

 

 

Page 18: Basic Arithmetic (adding and subtracting) Digital logic to show add/subtract Boolean algebra abstraction of physical, analog circuit behavior 1 0 CPU components

Signed Arithmetic

Examples converting to two’s complement representation:For -13, 8-bit representation

00001101 base integer11110010 1's complement +111110011 2's complement

 For -227, 12-bit representation

000011100011 base integer111100011100 1's complement +1111100011101 2's complement

 

Page 19: Basic Arithmetic (adding and subtracting) Digital logic to show add/subtract Boolean algebra abstraction of physical, analog circuit behavior 1 0 CPU components

Signed Arithmetic

Converting from two's complement to decimal:

• If the sign bit is 0, convert the binary number to decimal.

• If the sign bit is 1– complement each bit– add 1– convert the binary number to decimal– put a minus sign in front

 

 

Page 20: Basic Arithmetic (adding and subtracting) Digital logic to show add/subtract Boolean algebra abstraction of physical, analog circuit behavior 1 0 CPU components

Signed Arithmetictwo's complement encoding - note one zero and asymmetric range first (leftmost) bit is sign bit (1 => -) 

binary sign magnitude one's complement two's complement 

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

 

 

Page 21: Basic Arithmetic (adding and subtracting) Digital logic to show add/subtract Boolean algebra abstraction of physical, analog circuit behavior 1 0 CPU components

Signed ArithmeticWe can easily make a full adder do subtraction by adding an inverter in front of each b sub i and setting carry into the rightmost adder to one

Page 22: Basic Arithmetic (adding and subtracting) Digital logic to show add/subtract Boolean algebra abstraction of physical, analog circuit behavior 1 0 CPU components

Signed Arithmeticrange for n-bit field: unsigned is [ 0, 2n - 1 ]2's compl. signed is [ -2n-1, 2n-1 - 1 ]

signed overflow occurs whenever the sign bits of the two operands agree, but the sign bit of the result differs (i.e., add two positives and result appears negative, or add two negatives and result appears nonnegative)range diagrams for three bitsunsigned

signed(2's compl)

000 001 010 011 100 101 110 111

|------- |------- |------- |------- |------- |------- |-------| 0 1 2 3 4 5 6

7

100 101 110 111 000 001 010 011

|------- |------- |------- |------- |------- |------- |-------|-4 -3 -2 -1 0 +1 +2

+3

b2 b1 b0

sign

b1

b0

Page 23: Basic Arithmetic (adding and subtracting) Digital logic to show add/subtract Boolean algebra abstraction of physical, analog circuit behavior 1 0 CPU components

Signed Arithmeticmodulo arithmetic (keep adding +1 and wrap around)

000 001 010 011 100 101 110 111

(unsigned) 0 1 2 3

4 5 6 7

 (or 2's compl)

 0

 +1

 +2

 +3

  -4

 -3

 -2

 -1

^^--carry occurs on wrap around

Page 24: Basic Arithmetic (adding and subtracting) Digital logic to show add/subtract Boolean algebra abstraction of physical, analog circuit behavior 1 0 CPU components

Signed Arithmetic3-bit examples 

bits unsigned signed 111 = 7 = (–1)

+001 = +1 = +(+1) ----- --- -------000 0 (0)(carry) OVF ^^^-- this is what the ALU computes for either unsigned or signed. but, while it is an unsigned overflow, it is CORRECT for signed

 

Page 25: Basic Arithmetic (adding and subtracting) Digital logic to show add/subtract Boolean algebra abstraction of physical, analog circuit behavior 1 0 CPU components

Signed Arithmetic3-bit examples 

Example 2  bits unsigned signed 011 3 = (+3) +001 = +1 = +(+1) ----- -- ----- 100 4 (–4) OVF

^^^-- this is what the ALU computes for either unsigned or signed, but, while it is correct for unsigned, it is SIGNED OVERFLOW!

 

 

Page 26: Basic Arithmetic (adding and subtracting) Digital logic to show add/subtract Boolean algebra abstraction of physical, analog circuit behavior 1 0 CPU components

Signed Arithmetic

16-bit signed (2's complement) examples

in 16-bit arithmetic, we can represent values as four hex digits;if the leading hex digit is between 0 and 7 (thus it has a leading bit of 0), it is a nonnegative value; if the leading hex digit is between 8 and f (thus it has a leading bit of 1), it is a negative value

signed overflow occurs if a. (+) added with (+) gives a (-), or b. (-) added with (-) gives a (+)

 

 

Page 27: Basic Arithmetic (adding and subtracting) Digital logic to show add/subtract Boolean algebra abstraction of physical, analog circuit behavior 1 0 CPU components

Signed Arithmetic

hexadecimal hexadecimal decimal 

0x7654 = 0x7654 = (+30292)

+0xffed = +(-0x13) = +( –19)

0x7641 0x7641 (+30273) (carry) carry occurs but there is no signed overflow (thus carry is ignored)

(+) added with (-) cancels out, so signed overflow is not possible

 

Page 28: Basic Arithmetic (adding and subtracting) Digital logic to show add/subtract Boolean algebra abstraction of physical, analog circuit behavior 1 0 CPU components

Signed Arithmetic

hex decimal  0x7654 = (+30292)+0x1abc = +( + 6844) ---------- ------------ 0x9110 (-28400) should be 37136, but is >

max positive 32767

  no carry occurs but there is signed overflow (+) added with (+) giving (-) => SIGNED OVERFLOW! 

 

Page 29: Basic Arithmetic (adding and subtracting) Digital logic to show add/subtract Boolean algebra abstraction of physical, analog circuit behavior 1 0 CPU components

Signed Arithmetic

hex decimal  0x7654 = (+30292)+0x1abc = +( + 6844)

0x9110 (-28400) should be 37136, but is > max positive 32767

  no carry occurs but there is signed overflow (+) added with (+) giving (-) => SIGNED OVERFLOW! 

 

Page 30: Basic Arithmetic (adding and subtracting) Digital logic to show add/subtract Boolean algebra abstraction of physical, analog circuit behavior 1 0 CPU components

Signed Arithmetic

hexadecimal 0x7654 change subtraction to addition by ffff -0xff8d taking two's complement of 0xff8d -ff8d

0072+ 1

0073

hexadecimal hexadecimal decimal 

0x7654 = 0x7654 = (+30292)

-0xff8d = +0x0073 = +( +115) 0x76c7 = (+30407) 

no carry occurs and no signed overflow (+) added with (+) giving (+) => no signed overflow