Presentation on theme: "Cosc 2150: Computer Organization Chapter 9, Part 2 Integer multiplication and division."— Presentation transcript:
Cosc 2150: Computer Organization Chapter 9, Part 2 Integer multiplication and division
Multiplication Complex Work out partial product for each digit Take care with place value (column) Add partial products
Multiplication Example (unsigned) (long hand) 1011 Multiplicand (11 dec) x 1101 Multiplier (13 dec) 1011 Partial products Note: if multiplier bit is 1 copy multiplicand (place value) otherwise zero Product (143 dec) Note: need double length result
Flowchart for Unsigned Binary Multiplication
Execution of Example
Multiplying Negative Numbers This does not work! Solution 1 —Convert to positive if required —Multiply as above —If signs were different, negate answer Solution 2 —Booth’s algorithm
Example of Booth’s Algorithm 3*7 First setup the columns and initial values. This case 3 is in Q and M is 7. —But could put 7 in Q and M as 3
Example of Booth’s Algorithm First cycle: Now look at Q 0 and Q -1 With a 10, we Sub (A=A-M), then shift (always to the right) Second cycle: looking at Q 0 and Q -1 —With a 11, we only shift.
Example of Booth’s Algorithm 2 nd cycle Result Third cycle, Q 0 and Q -1 have 01 —So we will Add (A=A+M), then shift
3 nd cycle Result 4 th cycle, Q 0 and Q -1 have 00 —So we only shift Example of Booth’s Algorithm
4 nd cycle Result 5 th cycle, Q 0 and Q -1 have 00 —So we only shift Example of Booth’s Algorithm
5 th cycle Result Since we are working in 5 bits, we only repeat 5 times
Example of Booth’s Algorithm Result is A and Q so which is 21. Note: The sign bit is the last bit in A.
Division More complex than multiplication Negative numbers are really bad! Based on long division
Signed Division 1. Load divisor into M and the dividend into A, Q registers. Dividend must be 2n-bit twos complement number —0111 (7) becomes —1001 (-7) becomes Shift A, Q left 1 bit position 3. If M and A have the same signs, A A –M else A A+M
4. Step 3 is successful if sign of A is the same as before step 3 and at the end of step 3 A.if successful or (A=0 AND Q=0) then set Q0 1 B.if unsuccessful and (A 0 OR Q 0) then restore the previous value of A 5. Repeat steps 2 through 4 as many times as there are bit positions in Q. 6. The reminder is in A. If the signs are of Divisor and dividend were the same, the quotient is in Q, otherwise the correct quotient is the twos complement of Q.
Examples of division (signed) AQM=0011AQ Initial Value Initial Value shift shift 1101subtract0010Add restore Restore shift Shift 1110subtract0001Add restore Restore shift Shift 0000subtract1111Add set Q0 = Q0 = Shift Shift 1110subtract0010Add restore Restore (a) 7/3(b) -7/3