Professional Documents
Culture Documents
Computer Science 37 Lecture 8
Computer Science 37 Lecture 8
Computer Science 37 Lecture 8
Binary Arithmetic
4.1
A B
Sum
CarryOut
4.2
A B
+
CarryIn
CarryOut
b CarryOut
4.3
CarryIn
CarryIn
+
CarryOut
+
CarryOut
+
CarryOut
Sumn 1
Sumn 2
Sum0
4.4
Operations on 2s Complement
Subtraction: Note that (A-B) is the same as (A+(-B)), so this operation can be done in two steps: a sign inversion and an addition.
Just as in addition, overflow can occur:
4.5
1-bit version Note how it does all three operations in parallel, whether it is required or not.
0 1
Result
CarryOut
4.6
a1 b1
Result1
32-bit version
a2 b2
Result2
a31 b31
CarryIn ALU31
Result31
4.7
Operations on 2s Complement
Multiplication: Trivial when multiplier is a power of 2 (use a shift-register to do left shifts). Otherwise, we have to define an algorithm
Example:
A = 1110 = 10112 B = 1410 = 11102
C = A * B 8 bits
100110102 = 15410
SB=Sign(B)
operand 2
SC=Sign(C)
result
XOR SA,SB 0 1 1 0
4.9
Multiplication: In general, if the multiplicand has n bits and the multiplier has m bits, the product will have (n+m) bits. Note whats going on: we go through each bit in the multiplier and performing a sequence of left-shifts and additions. This is an indication that to implement multiplication in hardware, one needs a shift-register and an adder. What about the signs of the operands and the 4.10 sign of the result?
10
Start
Multiplier0 = 1
1. Test Multiplier0
Multiplier0 = 0
1a. Add multiplicand to product and place the result in Product register
How many registers are needed to implement this in hardware? In general, how many repetitions are needed by this algorithm?
4.11
32nd repetition?
11
4.12
12
Start
Multiplier0 = 1
1. Test Multiplier0
Multiplier0 = 0
1a. Add multiplicand to the left half of the product and place the result in the left half of the Product register
32nd repetition?
4.13
13
32-bit ALU
Product 64 bits
Control test
4.14
14
Operations on 2s Complement
Division: Trivial when multiplier is a power of 2 (use a shift-register to do right shifts). Otherwise, we have to define an algorithm, but first, lets think a bit. Quotient: How many times does the divisor fit into the dividend? Remainder: After a multiple of the divisor has been subtracted from the dividend, whats left?
4.15
15
Start
Division:
1001 1000 1001010 -1000 1010 -1000 10
1. Subtract the Divisor register from the Remainder register and place the result in the Remainder register
> Remainder 0
Test Remainder
Remainder < 0
2a. Shift the Quotient register to the left, setting the new rightmost bit to 1
2b. Restore the original value by adding the Divisor register to the Remainder register and place the sum in the Remainder register. Also shift the Quotient register to the left, setting the new least significant bit to 0
33rd repetition?
Yes: 33 repetitions
4.16
Done
16
64-bit ALU
Remainder 64 bits
Write
Control test
4.17
17