Вы находитесь на странице: 1из 6

Multiplication

http://www.cs.uaf.edu/~cs301/notes/Chapter5/node5.html

Next: Division Up: Arithmetic and Logical Operations Previous: Addition and Subtraction

Multiplication
The rules for binary multiplication are:

In truth table form, the multiplication of two bits, a x b is:

Observe that a x b is identical to the logical and operation. Multiplication of two unsigned binary numbers, X and Y, can be performed using the longhand algorithm:
Y: X: 1011 x 101 -----1011 0000 + 1011 -------110111

Note that the 0 bits in X contribute a 0 to the product, while the 1 bits in X contribute Y shifted left to align with the corresponding bit in X. The basis for the longhand algorithm can be understood from the binary representation for unsigned integers. Let X and Y be unsigned n+1 bit integers:

1 of 6

09/10/2012 09:32 AM

Multiplication

http://www.cs.uaf.edu/~cs301/notes/Chapter5/node5.html

The product XY is given by:

Each nonzero bit in X contributes a term consisting of Y multiplied by a power of 2. The product of two unsigned n bit numbers can require up to 2n bits since

Overflow occurs when the product is larger than n bits. The longhand multiplication algorithm for n bit unsigned integers can be implemented using n bit addition. Observe that the least significant bit is determined by the first term in the sum. This bit can be stored and the term can be shifted 1 bit to the right in preparation for adding the next term. This process is then repeated n times for n bits. The longhand algorithm in SAL is:
# # # # X: Y: S: A SAL program to multiply two positive integers using addition and logical operations. No overflow checking is performed. .data .word .word .word

# first number to multiply # second number to multiply # the partial sum of products

2 of 6

09/10/2012 09:32 AM

Multiplication

http://www.cs.uaf.edu/~cs301/notes/Chapter5/node5.html

I: X0: Pi: P:

.word .word .word .word

# # # #

bit counter rightmost bit of X i-th bit of product product X times Y

.text __start: move move move get get loop: and srl beqz add shift: and or srl ror sub bgtz or ror put done

I, 32 S, 0 P, 0 X Y X0, X, 1 X, X, 1 X0, shift S, S, Y Pi, S, 1 P, P, Pi S, S, 1 P, P, 1 I, I, 1 X, loop P, P, S P, P, I P

# # # # # # # # # # # # # # # # # #

initialize counter initialize partial sum initialize result input multiplier input multiplicand mask rightmost bit of X shift X right 1 bit if x0=0, shift and get next bit add Y to partial sum mask rightmost bit of sum store the bit in P clear sum bit from S rotate P right 1 bit decrement shift counter repeat if X nonzero combine partial sum/result final shift output product

To avoid multiplicative overflow, two 32 bit integers may be multiplied to produce a 64 bit product in two 32 bit words, HI and LO, which hold the most and least significant 32 bits of the product, respectively. In the HI/LO algorithm, multiplication is performed after splitting the 32 bit numbers to be multiplied into 16 bit HI/LO pairs and performing 32 bit multiplication on the pairs. An implementation of the HI/LO algorithm in C language is shown below:
/* Multiply two 32-bit numbers, V1 and V2, to produce a 64 bit result in the HI/LO registers. The algorithm is high-school math:

3 of 6

09/10/2012 09:32 AM

Multiplication

http://www.cs.uaf.edu/~cs301/notes/Chapter5/node5.html

A B x C D -----AD || BD AC || CB || 0 where A and B are the high and low short words of V1, C and D are the short words of V2, AD is the product of A and D, and X || Y is (X << 16) + Y. Since the algorithm is programmed in C, we need to be careful not to overflow. */ static void long_multiply (reg_word v1, reg_word v2) { register long a, b, c, d; register long x, y; a b c d = = = = (v1 >> 16) & 0xffff; v1 & 0xffff; (v2 >> 16) & 0xffff; v2 & 0xffff;

LO = b * d; /* BD */ x = a * d + c * b; /* AD + CB */ y = ((LO >> 16) & 0xffff) + x; LO = (LO & 0xffff) | ((y & 0xffff) << 16); HI = (y >> 16) & 0xffff; HI += a * c; } /* AC */

The 64 bit HI/LO algorithm may also be implemented in SAL:


# # Compute the product of two 32 bit integers with 64 bit result. # # Multiply two 32-bit numbers, V1 and V2, to produce a 64 bit result in # the HI/LO registers. The algorithm is high-school math: # # A B # x C D # ------

4 of 6

09/10/2012 09:32 AM

Multiplication

http://www.cs.uaf.edu/~cs301/notes/Chapter5/node5.html

# # # # # # #

AD || BD AC || CB || 0 where A and B are the high and low short words of V1, C and D are the short words of V2, AD is the product of A and D, and V1 || V2 is (V1 << 16) + V2. We need to be careful not to overflow. C code is given in comments. .data .word .word .word .word .word .word .word .word .word .word .word .text srl and srl and mul mul mul mul add srl add and sll or srl add put put put done

LO: HI: V1: V2: B: A: D: C: Mask: X: Y:

0x20000000 0x40000000

0xFFFF

# # # # # # # # #

least significant word of result most siginificant word of result number to multiply number to multiply lo 16 bits of V1 hi 16 bits of V1 lo 16 bits of V2 hi 16 bits of V2 16 bit mask

__start:

A,V1,16 B,V1,Mask C,V2,16 D,V2,Mask LO,B,D HI,A,C X,B,C Y,A,D X,X,Y Y,LO,16 Y,Y,X LO,LO,Mask X,Y,16 LO,LO,X Y,Y,16 HI,HI,Y HI ' ' LO

# # # # # # #

a = (v1 >> 16) & 0xffff; b = v1 & 0xffff; c = (v2 >> 16) & 0xffff; d = v2 & 0xffff; LO = b * d; HI = a * c; x = a * d + c * b;

# y = ((LO >> 16) & 0xffff) + x; # LO = (LO & 0xffff) # LO = LO | ((y & 0xffff) << 16); # HI += (y >> 16) & 0xffff;

The algorithm works without modification for a negative multiplier and a positive multiplicand. It can be extended to work for 2's complement integers. To multiply two negative
5 of 6 09/10/2012 09:32 AM

Multiplication

http://www.cs.uaf.edu/~cs301/notes/Chapter5/node5.html

numbers or a positive multiplier and negative multiplicand, two approaches are possible: 1. Convert negative numbers to positive, multiply positive numbers and then convert result to correct sign. 2. Sign extend both numbers to 64 bits and perform 64 bit multiplication. The correct result will be found in the least significant 64 bits of the product. An example of method #2 applied to 4-bit 2's complement integers is shown below. The 4-bit integers 1101 (-3) and 0110 (6) are sign extended to 8 bits before multiplication. The correct result (-18) is found in the rightmost 8 bits.
11111101 (-3) x 00000110 (6) ---------00000000 11111101 11111101 00000000 00000000 00000000 00000000 + 00000000 ----------------11101110 (-18)

Next: Division Up: Arithmetic and Logical Operations Previous: Addition and Subtraction CS 301 Class Account Thu Nov 5 12:25:05 AST 1998

6 of 6

09/10/2012 09:32 AM

Вам также может понравиться