Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arithmetic II CPSC 321 Andreas Klappenecker. Any Questions?

Similar presentations


Presentation on theme: "Arithmetic II CPSC 321 Andreas Klappenecker. Any Questions?"— Presentation transcript:

1 Arithmetic II CPSC 321 Andreas Klappenecker

2 Any Questions?

3 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!.

4 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!

5 Factorial Numbers fac:bne $a0, $zero, gen # if $a0<>0, goto gen ori $v0, $zero, 1 # else return 1 jr $ra # gen: addiu $a0, $a0, -1 # $a0 = n-1 jal fac # $v0 = fac(n-1) mul $v0, $v0, $a0 # $v0 = fac(n-1) x n jr $ra # return n!

6 Factorial Numbers = addiu $sp, $sp, -8 # multipush sw $ra, 4($sp) # save $ra sw $a0, 0($sp) # save $a0 = lw $a0, 0($sp) # restore $a0=n lw $ra, 4($sp) # restore $ra addiu $sp, $sp, 8 # multipop

7 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

8 Today’s Menu Arithmetic-Logic Units Logic Design Revisited Faster Addition Multiplication (if time permits)

9 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

10 Logic Gates AND gate OR gate NOT gate What are the truth tables?

11 Logic Gates NOR gate NAND gate XOR gate What are the truth tables?

12 Half Adder s c a b a b c s 0 000 0 101 1 001 1 110

13 Full Adder c in a b c out s 0 0 000 0 0 101 0 1 001 0 1 110 1 0 001 1 0 110 1 1 010 1 1 111 Give a Boolean formula for s s=cin xor a xor b Give a Boolean formula for c out c out =ab+c in (a xor b) Design now a circuit using and, or, xor.

14 Full Adder c in a b c out s s=c in xor a xor b c out = ab+c in (a xor b)

15 Ripple Carry Adder

16 Critical Path c in a b c out s Suppose that each gate has a unit delay. What is the critical path (= path with the longest delay)?

17 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

18 Faster Adders c in a b c out s 0 0 000 0 0 101 0 1 001 0 1 110 1 0 001 1 0 110 1 1 010 1 1 111 c out =ab+c in (a xor b) =ab+ac in +bc in =ab+(a+b)c in = g + p c in Generate g = ab Propagate p = a+b Why are they called like that?

19 Fast Adders Iterate the idea, generate and propagate c i+1 = g i + p i c i = g i + p i (g i-1 + p i-1 c i-1 ) = g i + p i g i-1 + p i p i-1 c i-1 = g i + p i g i-1 + p i p i-1 g i-2 +…+ p i p i-1 …p 1 g 0 +p i p i-1 …p 1 p 0 c 0 Two level AND-OR circuit Carry is known early!

20 Carry Lookahead Adders Based on the previous identity Fast because critical path is shorter O(log n) gate delays [assuming 2-input gates]  More complex to implement  Design is less regular  Layout of one bit adder cells depend on i Compromise  couple blocks of carry lookahead adders

21 Building an ALU Addition Subtraction AND OR What is missing?

22 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

23 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

24 SLT 4 operations subtraction output available Connect MSB set output w/ LSB less

25 LSB indicates whether a<b 0 if false 1 if true

26 Test for equality Notice control lines: 000 = and 001 = or 010 = add 110 = subtract 111 = slt Note: zero is a 1 when the result is zero!

27 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


Download ppt "Arithmetic II CPSC 321 Andreas Klappenecker. Any Questions?"

Similar presentations


Ads by Google