Top Banner
Arithmetic II CPSC 321 Andreas Klappenecker
27

Arithmetic II CPSC 321 Andreas Klappenecker. Any Questions?

Jan 15, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Arithmetic II CPSC 321 Andreas Klappenecker. Any Questions?

Arithmetic IICPSC 321

Andreas Klappenecker

Page 2: Arithmetic II CPSC 321 Andreas Klappenecker. Any Questions?

Any Questions?

Page 3: Arithmetic II CPSC 321 Andreas Klappenecker. Any Questions?

Review: Recursive Procedures

• The number of ways to order n items is given by n x (n-1) x … x 2 x 1 = n!

• There is just one way to order 0 items, hence 0!=1

Write a program to calculate n!.

Page 4: Arithmetic II CPSC 321 Andreas Klappenecker. Any Questions?

Review: Recursive Procedures

int fac(int n) { if (n==0)

return 1; else

return n*fac(n-1);}A MIPS assembly language program for the same problem is almost as simple!

Page 5: Arithmetic II CPSC 321 Andreas Klappenecker. Any Questions?

Factorial Numbers

fac: bne $a0, $zero, gen # if $a0<>0, goto gen

ori $v0, $zero, 1 # else return 1jr $ra #

gen: <save registers>addiu $a0, $a0, -1 # $a0 = n-1jal fac # $v0 = fac(n-1)<restore registers> mul $v0, $v0, $a0 # $v0 = fac(n-

1) x njr $ra # return n!

Page 6: Arithmetic II CPSC 321 Andreas Klappenecker. Any Questions?

Factorial Numbers

<save registers>=addiu $sp, $sp, -8 # multipushsw $ra, 4($sp) # save $ra sw $a0, 0($sp) # save $a0

<restore registers>=

lw $a0, 0($sp) # restore $a0=nlw $ra, 4($sp) # restore $raaddiu $sp, $sp, 8 # multipop

Page 7: Arithmetic II CPSC 321 Andreas Klappenecker. Any Questions?

Practice, Practice, Practice!

• Why recursive procedures? • Often shorter than iterative versions!• It is often straightforward to write the

recursive version, but sometimes difficult to find an iterative version.

• Towers of Hanoi• Quicksort• Generate all permutations• Backtracking

Page 8: Arithmetic II CPSC 321 Andreas Klappenecker. Any Questions?

Today’s Menu

Arithmetic-Logic UnitsLogic Design RevisitedFaster AdditionMultiplication (if time permits)

Page 9: Arithmetic II CPSC 321 Andreas Klappenecker. Any Questions?

Goals

• We recall some basic logic gates• Determine the truth tables for the Boolean

functions• We recall half-adders and full-adders• Ripple-carry adders• Discuss faster adders

Page 10: Arithmetic II CPSC 321 Andreas Klappenecker. Any Questions?

Logic Gates

• AND gate

• OR gate

• NOT gate

What are the truth tables?

Page 11: Arithmetic II CPSC 321 Andreas Klappenecker. Any Questions?

Logic Gates

• NOR gate

• NAND gate

• XOR gate

What are the truth tables?

Page 12: Arithmetic II CPSC 321 Andreas Klappenecker. Any Questions?

Half Adder

s

c

ab

a b c s

0 0 0 0

0 1 0 1

1 0 0 1

1 1 1 0

Page 13: Arithmetic II CPSC 321 Andreas Klappenecker. Any Questions?

Full Adder

cin a b cout s

0 0 0 0 0

0 0 1 0 1

0 1 0 0 1

0 1 1 1 0

1 0 0 0 1

1 0 1 1 0

1 1 0 1 0

1 1 1 1 1

Give a Boolean formula for s

• s=cin xor a xor b

Give a Boolean formula for cout

• cout =ab+cin(a xor b)

Design now a circuit using and, or, xor.

Page 14: Arithmetic II CPSC 321 Andreas Klappenecker. Any Questions?

Full Adder

cin

ab

cout

s

s=cin xor a xor b cout = ab+cin(a xor b)

Page 15: Arithmetic II CPSC 321 Andreas Klappenecker. Any Questions?

Ripple Carry Adder

Page 16: Arithmetic II CPSC 321 Andreas Klappenecker. Any Questions?

Critical Path

cin

ab

cout

s

Suppose that each gate has a unit delay. What is the critical path (= path with the longest delay)?

Page 17: Arithmetic II CPSC 321 Andreas Klappenecker. Any Questions?

Ripple Carry Adders

• Each gates causes a delay• our example: 3 gates for carry generation • book has example with 2 gates

• Carry might ripple through all n adders• O(n) gates causing delay• intolerable delay if n is large

• Carry lookahead adders

Page 18: Arithmetic II CPSC 321 Andreas Klappenecker. Any Questions?

Faster Adders

cin a b cout s

0 0 0 0 0

0 0 1 0 1

0 1 0 0 1

0 1 1 1 0

1 0 0 0 1

1 0 1 1 0

1 1 0 1 0

1 1 1 1 1

cout=ab+cin(a xor b) =ab+acin+bcin

=ab+(a+b)cin

= g + p cin

Generate g = abPropagate p = a+b

Why are they called like that?

Page 19: Arithmetic II CPSC 321 Andreas Klappenecker. Any Questions?

Fast Adders

Iterate the idea, generate and propagateci+1 = gi + pici

= gi + pi(gi-1 + pi-1 ci-1)

= gi + pigi-1+ pipi-1ci-1

= gi + pigi-1+ pipi-1gi-2 +…+ pipi-1 …p1g0

+pipi-1 …p1p0c0

Two level AND-OR circuit Carry is known early!

Page 20: Arithmetic II CPSC 321 Andreas Klappenecker. Any Questions?

Carry Lookahead Adders

• Based on the previous identityFast because critical path is shorterO(log n) gate delays [assuming 2-input gates]More complex to implement Design is less regularLayout of one bit adder cells depend on i

• Compromise couple blocks of carry lookahead adders

Page 21: Arithmetic II CPSC 321 Andreas Klappenecker. Any Questions?

Building an ALU

0

2

Result

Operation

a

1

CarryIn

CarryOut

0

1

Binvert

b

AdditionSubtractionANDOR

What is missing?

Page 22: Arithmetic II CPSC 321 Andreas Klappenecker. Any Questions?

• Need to support the set-on-less-than instruction

(slt)

• remember: slt is an arithmetic instruction

• produces 1 if rs < rt and 0 otherwise

• use subtraction: (a-b) < 0 implies a < b

• Need to support test for equality (beq $t5, $t6,

$t7)

• use subtraction: (a-b) = 0 implies a = b

Tailoring the ALU to the MIPS

Page 23: Arithmetic II CPSC 321 Andreas Klappenecker. Any Questions?

SLT

• Determine a<b• Calculate b-a• If MSB equals

• 1, then a<b• 0, then a>=b

• Changes?• Operation less than• Output of

subtraction• Overflow

0

2

Result

Operation

a

1

CarryIn

CarryOut

0

1

Binvert

b

Page 24: Arithmetic II CPSC 321 Andreas Klappenecker. Any Questions?

SLT

0

3

Result

Operation

a

1

CarryIn

CarryOut

0

1

Binvert

b 2

Less

0

3

Result

Operation

a

1

CarryIn

0

1

Binvert

b 2

Less

Set

Overflowdetection

Overflow

a.

b.

• 4 operations• subtraction output available• Connect • MSB set output

w/ LSB less

Page 25: Arithmetic II CPSC 321 Andreas Klappenecker. Any Questions?

Seta31

0

ALU0 Result0

CarryIn

a0

Result1a1

0

Result2a2

0

Operation

b31

b0

b1

b2

Result31

Overflow

Binvert

CarryIn

Less

CarryIn

CarryOut

ALU1Less

CarryIn

CarryOut

ALU2Less

CarryIn

CarryOut

ALU31Less

CarryIn

• LSB indicates whether a<b

• 0 if false

• 1 if true

Page 26: Arithmetic II CPSC 321 Andreas Klappenecker. Any Questions?

Test for equality

• Notice control lines:

000 = and001 = or010 = add110 = subtract111 = slt

•Note: zero is a 1 when the result is zero!Set

a31

0

Result0a0

Result1a1

0

Result2a2

0

Operation

b31

b0

b1

b2

Result31

Overflow

Bnegate

Zero

ALU0Less

CarryIn

CarryOut

ALU1Less

CarryIn

CarryOut

ALU2Less

CarryIn

CarryOut

ALU31Less

CarryIn

Page 27: Arithmetic II CPSC 321 Andreas Klappenecker. Any Questions?

Summary

• We can build an ALU to support the MIPS instruction set

• key idea: use multiplexor to select the output we want• we can efficiently perform subtraction using two’s complement• we can replicate a 1-bit ALU to produce a 32-bit ALU

• Important points about hardware• all of the gates are always working• the speed of a gate is affected by the number of inputs to the

gate• the speed of a circuit is affected by the number of gates in

series(on the “critical path” or the “deepest level of logic”)

• We focused on basic principles. We noted that • clever changes to organization can improve performance

(similar to using better algorithms in software)• faster addition, next time: faster multiplication