190 likes | 404 Vues
Computer Organization and Architecture Lecture 6 COMPUTER ARITHMETICS. Huma Ayub. Department of Software Engineering. University of Engineering and Technology Taxila. 1. Multiplying Signed numbers. Main difficulty arises when signed numbers are involved.
E N D
Computer Organization and Architecture Lecture 6 COMPUTER ARITHMETICS Huma Ayub Department of Software Engineering. University of Engineering and Technology Taxila 1
Multiplying Signed numbers • Main difficulty arises when signed numbers are involved. • Naive approach: convert both operands to positive numbers, • multiply, then calculate separately the sign of the • product and convert if necessary. • To make multiplication fast • Exploit the fact that: 011111 = 100000 - 1 • Therefore we can replace multiplier • A better approach: Booth’s Algorithm
Booth algorithm gives a procedure for multiplying binary integers in signed –2’s complement representation • Example, 2 ten x (- 5) ten 0010 two * 1011 two • From the two numbers, pick the number with the smallest difference between a series of consecutive numbers, and make it a multiplier or if same higher magnitude . i.e., 0010 -- From 0 to 0 no change, 0 to 1 one change, 1 to 0 another change ,so there are two changes on this one 1011 -- From 1 to 0 one change, 0 to 1 two change, 1 to 1 no change, so there is only two change on this one. Therefore, multiplication of 2 x (– 5), where 2 ten (0010 two) is the multiplicand and and (– 5) ten (1011two) is the multiplier.
Step 1 for each pass • Add 4 leading zeros to the multiplier to get the beginning product: 0000 1011 • Use the LSB (least significant bit) and the previous LSB to determine the arithmetic action. • If it is the FIRST pass, use 0 as the previous LSB. • Possible arithmetic actions: • 00 no arithmetic operation • 01 add multiplicand to left half of product • 10 subtract multiplicand from left half of product • 11 no arithmetic operation • Perform an arithmetic right shift (ASR) on the entire product. • NOTE: For X-bit operands, Booth's algorithm requires X passes. • Let's continue with our example of multiplying (-5) x 2
Example continued • Initial Product and previous LSB 0000 1011 0 (Note: Since this is the first pass, we use 0 for the previous LSB) • Pass 1, Step 1: Examine the last 2 bits 0000 10110 The last two bits are 10, so we need to: subtract the multiplicand from left half of product • Pass 1, Step 1: Arithmetic action • (1) 0000 (left half of product) • -0010(multiplicand) 1110 (uses a phantom borrow) • Place result into left half of product 1110 1011 0
Example: Pass 1 continued and Pass 2 • Pass 1, Step 2: ASR (arithmetic shift right) • Before ASR 1110 1011 0 • After ASR 1111 0101 1 (left-most bit was 1, so a 1 was shifted in on the left) • Pass 1 is complete. • Current Product and previous LSB 1111 0101 1 • Pass 2, Step 1: Examine the last 2 bits 1111 01011 The last two bits are 11, so we do NOT need to perform an arithmetic action -- just proceed to step 2.
Example: Pass 2 continued and Pass 3 • Pass 2, Step 2: ASR (arithmetic shift right) • Before ASR 1111 0101 1 • After ASR 1111 1010 1 (left-most bit was 1, so a 1 was shifted in on the left) • Pass 2 is complete. • Current Product and previous LSB 1111 1010 1 • Pass 3, Step 1: Examine the last 2 bits 1111 10101 The last two bits are 01, so we need to: add the multiplicand to the left half of the product
Example: Pass 3 continued • Pass 3, Step 1: Arithmetic action (1) 1111 (left half of product) +0010(mulitplicand) 0001 (drop the leftmost carry) • Place result into left half of product 0001 1010 1 • Pass 3, Step 2: ASR (arithmetic shift right) • Before ASR 0001 1010 1 • After ASR 0000 1101 0 (left-most bit was 0, so a 0 was shifted in on the left) • Pass 3 is complete.
Example: Pass 4 • Current Product and previous LSB 0000 1101 0 • Pass 4, Step 1: Examine the last 2 bits 0000 11010 The last two bits are 10, so we need to: subtract the multiplicand from the left half of the product • Pass 4, Step 1: Arithmetic action (1) 0000 (left half of product) -0010(mulitplicand) 1110 (uses a phantom borrow) • Place result into left half of product 1110 1101 0
Example: Pass 4 continued and Pass 5 • Pass 4, Step 2: ASR (arithmetic shift right) • Before ASR 1110 1101 0 • After ASR 1111 0110 1 (left-most bit was 1, so a 1 was shifted in on the left) We have completed 4 passes on the 4-bit operands, so we are done.
Verification • To confirm we have the correct answer, convert the 2's complement final product back to decimal. • Final product: 1111 0110 • Decimal value: -10 which is the CORRECT product of: (-5) x 2
Division • Similar to multiplication: repeated subtract • The book discusses again three versions TU/e Processor Design 5Z032
Divide (1) • Well known algorithm: Dividend Divisor 1000/1001010\1001 Quotient -1000 10 101 1010 -1000 10 Remainder TU/e Processor Design 5Z032
Divisor Shift right 6 4 b i t s Quotient Shift left 6 4 - b i t A L U 3 2 b i t s Remainder C o n t r o l t e s t W r i t e 6 4 b i t s Start Division (2) 1. Substract the Divisor register from the Remainder register and place the result in the Remainder register • Implementation: Test Remainder >= 0 < 0 2.a Shift the Quotient register to the left, setting the rightmost bit to 1 2.b Restore the original value by adding the Divisor register. Also, shift a 1 into the Quotient register Shift Divisor Register right 1 bit 33rd repetition? no yes Done
Floating Point (a brief look) • We need a way to represent • numbers with fractions, e.g., 3.1416 • very small numbers, e.g., .000000001 • very large numbers, e.g., 3.15576 ´ 109 • Representation: • sign, exponent, significand: (–1)sign´ significand ´ 2exponent • more bits for significand gives more accuracy • more bits for exponent increases range • IEEE 754 floating point standard: • single precision : 8 bit exponent, 23 bit significand • double precision: 11 bit exponent, 52 bit significand
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 1 0 1 1 1 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 IEEE 754 floating-point standard • Leading “1” bit of significand is implicit • Exponent is “biased” to make sorting easier • all 0s is smallest exponent all 1s is largest • bias of 127 for single precision and 1023 for double precision • summary: (–1)sign´ (1+significand) ´ 2exponent – bias • Example: • decimal: -.75 = -3/4 = -3/22 • binary : -.11 = -1.1 x 2-1 • floating point: exponent = -1+bias = 126 = 01111110 • IEEE single precision:
0 0 0 1 0 0 1 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Examples of Double Precision Float • What is the decimal value of this Double Precision float ? • Solution: • Value of exponent = (10000000101)2 – Bias = 1029 – 1023 = 6 • Value of double float = (1.00101010 … 0)2 × 26 (1. is implicit) = (1001010.10 … 0)2 = 74.5 • What is the decimal value of ? • Do it yourself!(answer should be –1.5 × 2–7 = –0.01171875)
Computer Organization and Architecture Lecture 7 Data Path Huma Ayub Department of Software Engineering. University of Engineering and Technology Taxila 1