Addition and Subtraction Philipp Koehn 4 September 2019 Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

1 addition Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

1-Bit Adder 2 • Let’s start simple: Adding two 1-Bit numbers • Truth table A B A+B 0 0 0 0 1 1 1 0 1 1 1 10 Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

Really 2 Operations 3 • Truth table for "position 0" bit A B A+B 0 0 0 xor 0 1 1 1 0 1 1 1 0 • Truth table for carry bit A B A+B carry 0 0 0 0 1 0 and 1 0 0 1 1 1 Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

Reminder: Basic Gates 4 AND OR NOT NAND NOR Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

Circuits 5 • "Position 0" bit A B OUT0 A OR 0 0 0 B xor OUT0 0 1 1 AND 1 0 1 NAND 1 1 0 • Carry bit A B OUTC 0 0 0 A and OUTC AND 0 1 0 B 1 0 0 1 1 1 Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

Putting them Together 6 A OR B OUT0 AND NAND CARRY AND Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

N-Bit Addition 7 011 +11 --- 110 --- 110 Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

N-Bit Addition 8 011 +11 --- 110 --- 110 1+1 = 0, carry the 1 Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

N-Bit Addition 9 011 +11 --- 110 --- 110 1+1+1 = 1, carry the 1 Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

N-Bit Addition 10 011 +11 --- 110 --- 110 copy carry bit Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

1-Bit Adder 11 A OR B OUT0 AND NAND CARRY AND Our adder cannot handle carry as input yet Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

Half 1-Bit Adder 12 A OR B OUT0 AND NAND CARRY AND 1-BIT HALF ADDER Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

Building a 1-Bit Full Adder 13 SUM A S A 1-BIT HALF B CARRY ADDER B C Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

Building a 1-Bit Full Adder 14 CARRY SUM S A 1-BIT HALF A CARRY ADDER S B A C 1-BIT HALF B CARRY ADDER B C Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

Building a 1-Bit Full Adder 15 SUM CARRY S A 1-BIT HALF A ADDER S B A C 1-BIT CARRY HALF OR B ADDER B C Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

1-Bit Full Adder 16 SUM CARRY S A 1-BIT HALF A ADDER S B A C 1-BIT CARRY HALF OR B ADDER B C 1-BIT FULL ADDER Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

N-Bit Full Adder 17 011 +11 --- 110 --- 110 Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

N-Bit Full Adder 18 B0 A0 011 +11 --- A B CI 110 1-BIT --- FULL 110 ADDER CO S SUM0 Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

N-Bit Full Adder 19 B1 B0 A1 A0 011 +11 --- A B CI A B CI 110 1-BIT 1-BIT --- FULL FULL 110 ADDER ADDER CO S CO S SUM1 SUM0 Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

N-Bit Full Adder 20 B2 B1 B0 A2 A1 A0 and so A B A B A B CI CI CI on 1-BIT 1-BIT 1-BIT ... FULL FULL FULL ADDER ADDER ADDER CO S CO S CO S SUM2 SUM1 SUM0 Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

21 subtraction Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

First, a Trick 22 • Normally, we subtract like this: 0253 -176 ---- 0110 ---- 0077 Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

Computing the Inverse 23 • Now we use the inverse of the subtrahend 0999 -176 ---- 0823 Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

Subtraction by Addition 24 • This allows us to carry our subtraction by addition 00253 +0823 ----- 01076 • Well, with minor corrections 01076 +0001 -1000 ----- 0077 Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

Also Works in Binary 25 Original problem 00253 0011111101 -0176 -010110000 ----- ---------- 00077 0001001101 Inverse of subtrahend 00823 0001001111 Addition 00253 0011111101 +0823 +001001111 ----- ---------- 01076 0101001100 Corrections +0001 +000000001 -1000 -100000000 ---------- ---------- 0077 0001001101 Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

Start with N-Bit Adder 26 A0 A1 A2 A3 B0 B1 B2 B3 4-BIT CI CO FULL ADDER S0 S1 S2 S3 Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

Invert Bits of Subtrahend 27 B0 B1 B2 B3 A0 A1 A2 A3 B0 B1 B2 B3 4-BIT CI CO FULL ADDER S0 S1 S2 S3 Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

Add One 28 A0 A1 A2 A3 B0 B1 B2 B3 A0 A1 A2 A3 B0 B1 B2 B3 4-BIT V CI CO FULL ADDER S0 S1 S2 S3 Trick: add one as carry in Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

Invert Overflow --- DONE 29 A0 A1 A2 A3 B0 B1 B2 B3 A0 A1 A2 A3 B0 B1 B2 B3 4-BIT V CI CO FULL ADDER S0 S1 S2 S3 OVERFLOW SUM mm... Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

30 unifying addition and subtraction machines Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

Goal 31 • Not two machines for addition and subtraction ⇒ Combined adder and subtractor • Input: A, B, and subtraction flag SUB • Output – if SUB=0: A+B – if SUB=1: A-B Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

NOT only if SUB 32 B0 B1 B2 B3 A0 A1 A2 A3 A0 A1 A2 A3 B0 B1 B2 B3 4-BIT V CI CO FULL ADDER S0 S1 S2 S3 OVERFLOW SUM mm... Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

NOT only if SUB 33 • Truth table SUB X OUT 0 0 0 0 1 1 1 0 1 1 1 0 • Looks like XOR XOR Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

Combined Machine 34 A0 A1 A2 A3 B0 B1 B2 B3 SUB A0 A1 A2 A3 B0 B1 B2 B3 4-BIT SUB CI CO FULL ADDER SUB S0 S1 S2 S3 OVERFLOW SUM Philipp Koehn Computer Systems Fundamentals: Addition and Subtraction 4 September 2019

Recommend

More recommend