13
CS 251 Fall 2019 Principles of Programming Languages Ben Wood λ CS 240 Spring 2020 Foundations of Computer Systems Ben Wood https://cs.wellesley.edu/~cs240/s20/ Logic for Arithmetic adders Arithmetic Logic Unit Logic for Arithmetic 1

Logic for Arithmeticcs240/s20/slides/alu.pdf · 2020-01-29 · Addition: 1-bit halfadder ex A B Sum Carry out A B Carry out Sum 0 0 0 1 1 0 1 1 A + B Sum Carry out Logic for Arithmetic

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Logic for Arithmeticcs240/s20/slides/alu.pdf · 2020-01-29 · Addition: 1-bit halfadder ex A B Sum Carry out A B Carry out Sum 0 0 0 1 1 0 1 1 A + B Sum Carry out Logic for Arithmetic

CS 251 Fall 2019Principles of Programming LanguagesBen Woodλ CS 240 Spring 2020Foundations of Computer SystemsBen Wood

https://cs.wellesley.edu/~cs240/s20/

Logic for Arithmeticadders

Arithmetic Logic Unit

Logic for Arithmetic 1

Page 2: Logic for Arithmeticcs240/s20/slides/alu.pdf · 2020-01-29 · Addition: 1-bit halfadder ex A B Sum Carry out A B Carry out Sum 0 0 0 1 1 0 1 1 A + B Sum Carry out Logic for Arithmetic

Addition: 1-bit half adder ex

AB

Sum

Carry out

A B Carry out Sum

0 0

0 1

1 0

1 1

+A

BSum

Carry out

Logic for Arithmetic 2

Page 3: Logic for Arithmeticcs240/s20/slides/alu.pdf · 2020-01-29 · Addition: 1-bit halfadder ex A B Sum Carry out A B Carry out Sum 0 0 0 1 1 0 1 1 A + B Sum Carry out Logic for Arithmetic

Addition: 1-bit full adder

Carry in A B Carry

out Sum

0 0 0

0 0 1

0 1 0

0 1 1

1 0 0

1 0 1

1 1 0

1 1 1

ex+A

BSum

Carry in

Carry out

AB

Sum

Carry in

Carry out

Logic for Arithmetic 3

Page 4: Logic for Arithmeticcs240/s20/slides/alu.pdf · 2020-01-29 · Addition: 1-bit halfadder ex A B Sum Carry out A B Carry out Sum 0 0 0 1 1 0 1 1 A + B Sum Carry out Logic for Arithmetic

Addition: n-bit ripple-carry adder

+A0

B0Sum0

Carry in0

+An-1

Bn-1Sumn-1

Carry outn-1

+A1

B1Sum1

+A2

B2Sum2

There are faster, more complicated ways too…

AB

Sum

Carry in

Carry out

Logic for Arithmetic 4

Page 5: Logic for Arithmeticcs240/s20/slides/alu.pdf · 2020-01-29 · Addition: 1-bit halfadder ex A B Sum Carry out A B Carry out Sum 0 0 0 1 1 0 1 1 A + B Sum Carry out Logic for Arithmetic

ALU

Processor Components

Registers MemoryInstruction Fetch and Decode

1 324

Abstraction!

Logic for Arithmetic 5

Page 6: Logic for Arithmeticcs240/s20/slides/alu.pdf · 2020-01-29 · Addition: 1-bit halfadder ex A B Sum Carry out A B Carry out Sum 0 0 0 1 1 0 1 1 A + B Sum Carry out Logic for Arithmetic

Arithmetic Logic Unit (ALU)

Operand A

Operand B

Condition Codes(sign, overflow, carry-out, zero)

Result

Operation

Hardware unit for arithmetic and bitwise operations.

words

word

a few bits

a few bits

1

ALU

Logic for Arithmetic 6

Page 7: Logic for Arithmeticcs240/s20/slides/alu.pdf · 2020-01-29 · Addition: 1-bit halfadder ex A B Sum Carry out A B Carry out Sum 0 0 0 1 1 0 1 1 A + B Sum Carry out Logic for Arithmetic

1-bit ALU for bitwise operationsBuild an n-bit ALU from n 1-bit ALUs.Each bit i in the result is computed from the corresponding bit i in the two inputs.

MU

X

A

B

0

1

Operation

Result

Op A B Result

0 0 0

0 0 1

0 1 0

0 1 1

1 0 0

1 0 1

1 1 0

1 1 1

ex

Logic for Arithmetic 7

Page 8: Logic for Arithmeticcs240/s20/slides/alu.pdf · 2020-01-29 · Addition: 1-bit halfadder ex A B Sum Carry out A B Carry out Sum 0 0 0 1 1 0 1 1 A + B Sum Carry out Logic for Arithmetic

1-bit ALU

A

B

0

1

Operation

Result

2

2Carry in

+ Sum

Carry out

MUX

Logic for Arithmetic 8

Page 9: Logic for Arithmeticcs240/s20/slides/alu.pdf · 2020-01-29 · Addition: 1-bit halfadder ex A B Sum Carry out A B Carry out Sum 0 0 0 1 1 0 1 1 A + B Sum Carry out Logic for Arithmetic

A0

B0

0

1Result0

2

Carry in

+ Sum

MUX

An-1

Bn-1

0

1Resultn-1

2+ Sum

Carry out

MUX

A1

B1

0

1Result1

2+ Sum

MUX

Operation

… ....

2

+A0

B0

Sum0

Carry in

+An-1

Bn-1

Sumn-1

Carry out

+A1

B1

Sum1

+A2

B2

Sum2

n-bit ripple carry adder

n-bit ALU

Logic for Arithmetic 9

Page 10: Logic for Arithmeticcs240/s20/slides/alu.pdf · 2020-01-29 · Addition: 1-bit halfadder ex A B Sum Carry out A B Carry out Sum 0 0 0 1 1 0 1 1 A + B Sum Carry out Logic for Arithmetic

ALU conditionsExtra ALU outputs describing properties of result.Zero Flag:1 if result is 00...0 else 0Sign Flag:1 if result is negative else 0Carry Flag:1 if carry out else 0(Signed) Overflow Flag:1 if signed overflow else 0

Implement these.

A0

B0

0

1Result0

2

Carry in

+ Sum

MUX

An-1

Bn-1

0

1Resultn-1

2+ Sum

Carry out

MUX

A1

B1

0

1Result1

2+ Sum

MUX

Operation

… ....

2

ex

ex

Logic for Arithmetic 10

Page 11: Logic for Arithmeticcs240/s20/slides/alu.pdf · 2020-01-29 · Addition: 1-bit halfadder ex A B Sum Carry out A B Carry out Sum 0 0 0 1 1 0 1 1 A + B Sum Carry out Logic for Arithmetic

Add subtraction

Logic for Arithmetic 12

0

1Result0

2+

MUX

0

1Resultn-1

2+Carry out

MUX

0

1Result1

2+

MUX

Operation

… ....

B10

1

B00

1

Bn-10

1

....

2

How can we control ALU inputsor add minimal new logicto compute A-B?

A0

A1

An-1

Page 12: Logic for Arithmeticcs240/s20/slides/alu.pdf · 2020-01-29 · Addition: 1-bit halfadder ex A B Sum Carry out A B Carry out Sum 0 0 0 1 1 0 1 1 A + B Sum Carry out Logic for Arithmetic

A NAND B

A NOR B

A<B

A==B

How can we control ALU inputsor add minimal new logicto compute each?

Logic for Arithmetic 13

0

1Result0

2+

MUX

0

1Resultn-1

2+Carry out

MUX

0

1Result1

2+

MUX

Operation

… ....

A1 0

1

B10

1

A0 0

1

B00

1

Negate B

An-1 0

1

Bn-10

1

....

Invert A

....

2ex

Page 13: Logic for Arithmeticcs240/s20/slides/alu.pdf · 2020-01-29 · Addition: 1-bit halfadder ex A B Sum Carry out A B Carry out Sum 0 0 0 1 1 0 1 1 A + B Sum Carry out Logic for Arithmetic

Controlling the ALU

Logic for Arithmetic 17

ALU control lines Function

0000 AND0001 OR0010 add0110 subtract1100 NOR

Operand A

Operand B

Result

Control Lines

Condition Codes

Abstraction!

ALU