+ All Categories
Home > Documents > Basic Arithmetic (adding and subtracting)

Basic Arithmetic (adding and subtracting)

Date post: 10-Feb-2017
Category:
Upload: lycong
View: 234 times
Download: 1 times
Share this document with a friend
26
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
Transcript
Page 1: Basic Arithmetic (adding and subtracting)

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

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

A B A B0 0 00 1 11 0 11 1 1

AB

out

outAB

and (* or ^)

or ( )∨

Page 3: Basic Arithmetic (adding and subtracting)

Digital Logic

A out

outAB

not (~, ⌐, −)

xor ( )

A ~A0 11 0

A B A B0 0 00 1 11 0 11 1 0

Page 4: Basic Arithmetic (adding and subtracting)

Digital Logic

AB

out

outAB

nand

nor

A B0 0 10 1 11 0 11 1 0

B*A

A B A nor B0 0 10 1 01 0 01 1 0

Page 5: Basic Arithmetic (adding and subtracting)

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

BA∗BA∗BA∗BA∗

BA∗ BA∗ BA∗ BA∗

Page 6: Basic Arithmetic (adding and subtracting)

Unsigned Binary 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

Page 7: Basic Arithmetic (adding and subtracting)

Unsigned Binary ArithmeticBinary Addition

A B cout sum

0 0 0 00 1 0 11 0 0 11 1 1 0

Input Output

A B Cout S

0 0 0 00 1 0 11 0 0 11 1 1 0

cout

sumAB

Half Adder

carry_out = a and b

sum = a xor b

Page 8: Basic Arithmetic (adding and subtracting)

Unsigned Binary Arithmetic

The problem with a half-adder is that it doesn't handle carries. Consider adding the following two numbers:

When we add the two numbers, we get

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

1 11 1 1

+ 0 1 1(1) 0 1 0

1 1 1

+ 0 1 1

Page 9: Basic Arithmetic (adding and subtracting)

Unsigned Binary Arithmetic

For adding multi-bit fields/words, e.g., 4 bits

a3 a2 a1 a0

+ b3 b2 b1 b0

---------------------------------------sum3 sum2 sum1 sum0

we also need to add a carry_in with ai and bi, where i > 0

Page 10: Basic Arithmetic (adding and subtracting)

Unsigned Binary 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 ortogether the carry_outs

Input OutputCin A B Cout S0 0 0 0 01 0 0 0 10 1 0 0 11 1 0 1 00 0 1 0 11 0 1 1 00 1 1 1 01 1 1 1 1

Cin

AB

sum

Cout

Page 11: Basic Arithmetic (adding and subtracting)

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)

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)

Signed Binary Addition/Subtraction

fundamental idea #1finite 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 = 160, 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

Page 14: Basic Arithmetic (adding and subtracting)

Signed Binary Addition/Subtraction

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

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

observationa - b == a - b + rn == a + (rn - b)

\______/ \_______/#1 #2

\______/this term is our representationfor (-b)

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

Page 15: Basic Arithmetic (adding and subtracting)

Signed Binary Addition/Subtraction

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)

Signed Binary Addition/Subtraction

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

two's complement in binaryten's complement in decimal

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

Page 17: Basic Arithmetic (adding and subtracting)

Signed Binary Addition/SubtractionWe can easily make a full adder do subtraction by adding an inverter in front of each bi and setting carry into the rightmost adder to one

Page 18: Basic Arithmetic (adding and subtracting)

Signed Binary Addition/Subtractionrange 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 bits

unsigned

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 19: Basic Arithmetic (adding and subtracting)

Signed Binary Addition/Subtractionmodulo 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'scompl)

0 +1 +2 +3 -4 -3 -2 -1^^--carry occurs on wrap around

Page 20: Basic Arithmetic (adding and subtracting)

Signed Binary Addition/Subtraction3-bit examples

bits unsigned signed111 = 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 21: Basic Arithmetic (adding and subtracting)

Signed Binary Addition/Subtraction3-bit examples

Example 2bits unsigned signed011 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 22: Basic Arithmetic (adding and subtracting)

Signed Binary Addition/Subtraction

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 ifa. (+) added with (+) gives a (-), orb. (-) added with (-) gives a (+)

Page 23: Basic Arithmetic (adding and subtracting)

Signed Binary Addition/Subtraction

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 24: Basic Arithmetic (adding and subtracting)

Signed Binary Addition/Subtraction

hex decimal0x7654 = (+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 25: Basic Arithmetic (adding and subtracting)

Signed Binary Addition/Subtraction

hex decimal0x7654 = (+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 26: Basic Arithmetic (adding and subtracting)

Signed Binary Addition/Subtraction

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

0072+ 10073

hexadecimal hexadecimal decimal

0x7654 = 0x7654 = (+30292)-0xff8d = +0x0073 = +( +115)

0x76c7 = (+30407)

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


Recommended