Playing With Electricity: Multiplication

Author(s): Harvey Taylor
Publication: ZX-Appeal
Pub Details: ZX-Appeal Summer 88
Date: Summer 1988
Pages: 4-8

– July 12/88

Back in the good old bad days when I was programming on an 8 bit CPU, I used to hear talk of multiplication routines and was rather mystified just how this wonder was worked in binary. When I got into 68xxx programming, there was, lo & behold, a multiply instruction which removed any need to bother to figure out how binary multiplication might work … until I ran into the need to multiply numbers greater than 16 bits. I started playing with multiplication lately to enhance my Mandelbrot generating program and thought you might find the topic of interest.

One of the first methods of multiplying a machine language programmer runs into is shifting. Let us say you wanted to calculate 2 times some small number. The quickest and easiest way to do this is just to add it it to itself. eg. ADD A,A in Z80 or ADD D0,D0 will leave double the initial value in the register. So will shifting the number one position to the left. eg. SLA A or LSL #1,D0 These two methods have the same effect. The addition method is faster if you are counting cycles.

It is relatively straightforward to multiply by a small integer number. Let us say you want to calculate 9 times some other small number. You could do it something like this.

LD  B,A       ; SAVE A COPY
SLA A ; X2
SLA A ; X4
SLA A ; X8
ADD A,B ; X9

Similarly some odd numbers can be constructed of sums of shift products. eg. 13n = 8n + 4n + n where 8n and 4n can be found easily by shifting (or addition). You can see how multiplying some odd numbers might get confusing. For slightly larger products (larger than 255) this method will be really confusing because you will have to start juggling the HiLo register pairs. There is another alternative.

Let us say you are multiplying 245 * 137
245   = $F5    = %11110101
137   = $89    = %10001001

You know how to multiply it it longhand. How about in binary? If you write it out in the same longhand method, it looks like this:

                11110101
                10001001
                --------
                11110101
                      0
                     0
             11110101
                   0
                  0
                 0
         11110101
         ----------------
        1000001100011101  = $831D
                          = 33565

Now the question is how to code this multiplication? It is relatively simple if you remember to get your shifts right.

************************************************************************
*
*
* 16 BIT MULTIPLICATION ADOPTED FROM LEVENTHAL 68K.ASM PROG PAGE 8-11
*
* EXPECTS D1 = NUMBER1.W
* D2 = NUMBER2.W
*
* USES D0,D1,D2,D3
* PRESERVES D0,D2,D3
*
* RETURNS D0 = 32 BIT PRODUCT
*
MULT_16 MOVEM.L D0/D2/D3,-(A7)
MOVEQ #0,D0 * CLEAR FOR PRODUCT
MOVEQ #16-1,D3 * bit COUNTER
*
LOOP ADD.L D0,D0 * SHIFT PRODUCT LEFT 1 BIT
ADD.W D2,D2 * SHIFT MULTIPLIER LEFT 1 BIT
BCC.S STEP * IF MULTIPLIER BIT =0:
ADD.L D1,D0 * ELSE ADD MULTIPLICAND
STEP DBRA D3,LOOP
MOVE.L D0,D1 * RETURN PROD IN D1
MOVEM.L (A7)+,D0/D2/D3
RTS
*
************************************************************************
*
*

Notice that two 8 bit operands produce a 16 bit product and in general two n bit operands will produce a 2n bit product. Once you have the method down, you can see how to multiply two 32 bit numbers.

*
************************************************************************
*
*
* 32 BIT MULTIPLICATION AFTER LEVENTHAL 68K.ASM PROG PAGE 8-11
*
* EXPECTS D1 = NUMBER1.L
* D2 = NUMBER2.L
*
* USES D0,D1,D2,D3
* PRESERVES D0,D3
*
* RETURNS D0:D1 = 64 BIT PROD
*
* MSB LSB
* D0 D1
* $1F - $10:$0F - 00 | $1F - $10:$0F - 00
*
MULT_32 MOVEM.L A0/D2-D5,-(A7)
MOVEQ #0,D5 * CLEAR MS LWORD FOR PRODUCT
MOVEQ #0,D4 * CLEAR LS LWORD FOR PRODUCT
MOVEQ #0,D0 * CLEAR FOR ADDING CARRY
MOVEQ #32-1,D3 * BIT COUNTER
*
MLOOP ADD.L D4,D4 * SHIFT PRODUCT LEFT 1 BIT
ADDX.L D5,D5 * NOW SHIFT MS LWORD
ADD.L D2,D2 * SHIFT MULTIPLIER LEFT 1 BIT
BCC.S MSTEP * IF MULTIPLIER BIT =0:
ADD.L D1,D4 * ELSE ADD MULTIPLICAND
ADDX.L D0,D5 * ADD IN CARRY + 0
MSTEP DBRA D3,MLOOP
*
MOVE.L D5,D0 * RETURN MS LWORD IN D0
MOVE.L D4,D1 * RETURN LS LWORD IN D1
MOVEM.L (A7)+,A0/D2-D5
RTS
*
************************************************************************

I have been ignoring the most useful ‘hardware’ multiply instruction on the 68xxx, MULU (or MULS). As I mentioned above, this instruction uses only 16 bit operands to produce a 32 bit result. This is sufficient for most indexing uses, but rarely for arithmetic. eg. MULU #137,D0 will calculate the previous product in one instruction.

What if you want to calculate 1.2345E9 * 2.345E12 ? Then you are slowly and gently led down the path of n-bit multiplication. You can do 32 bit multiplication by utilizing the MULU instruction creatively.

*
************************************************************************
*
*
* EXPECTS D1 = NUMBER1.L
* D2 = NUMBER2.L
*
* USES D0-D6
* PRESERVES D3-D6
*
* RETURNS D1:D2 = 64 BIT PRODUCT
*
* D1 = NUMBER1.L a:b
* D2 = NUMBER2.L c:d
* D1*D2 = (a:b)*(c:d)
* = [(a * 2^16)+b]*[(c * 2^16)+d]
* = ac * 2 ^32 + (ad+bc) * 2 ^16 + bd
*
LONGMULT MOVEM.L D3-D6,-(A7)
MOVEQ #0,D4 * CLEAR FOR D4:D5 RESULT
MOVEQ #0,D5
MOVE.L D2,D3 * GET C:D
MOVE.L D1,D0 * GET A:B
MULU D0,D3 * BD
MOVE.L D3,D5 * STORE BD
MOVE.L D2,D3 * GET C:D
SWAP D0 * B:A
MULU D0,D3 * AD
SWAP D0 * A:B
SWAP D2 * D:C
MULU D2,D0 * BC
ADD.L D3,D0 * AD+BC
BCC.S NO_CARR0 * NOTE 16 BIT OFFSET TO REGS
* IF CARRY: ADD $00010000 TO D4
SWAP D4
ADDQ.W #1,D4 * ADD IN OFFSET CARRY
SWAP D4
NO_CARR0 MOVE.L D0,D6 * COPY AD+BC
SWAP D6 * GET MS WORD
LSL.L #8,D0 * SHIFT TO HIGH WORD
LSL.L #8,D0 * SHIFT TO HIGH WORD
ADD.L D0,D5 * CALC LS LWORD
ADDX.W D6,D4 * ADD X MS WORD
BCC.S NO_CARR1 * NOTE 16 BIT OFFSET TO REGS
SWAP D4
ADDQ.W #1,D4 * ADD IN OFFSET CARRY
SWAP D4
NO_CARR1 SWAP D1 * B:A
MULU D1,D2 * AC
ADD.L D2,D4 * CALC MS LWORD
*
MOVE.L D5,D2 * LS LWORD
MOVE.L D4,D1 * MS LWORD
MOVEM.L (A7)+,D3-D6
MOVEQ #0,D0
RTS
*
************************************************************************

For my Mandlebrot program, the multiplication I need to do is in Fixed Point arithmetic. All sorts of FP formats are possible. I have heard of people using 3 bits for the Integer and 29 bits for the fraction; and 4 bits for the integer and n bits for the fraction for n up to 1024. One format I used because it was easy to code was 16 bits Integer and 16 bits Fraction. In this format, the multiplication routine came out like this:

*
***************************************************************************
*
* EXPECTS FIX#'s IN D0,D1
* RETURNS PRODUCT IN D1
* NO ERROR FLAGS
* FIXED POINT FORMAT: IIII^FFFF
* WHERE: I = INTEGER PORTION
* F = FRACTIONAL PORTION = INTEGER/65536
*
* [I0+F0][I1+F1] = I0*I1 + I0*F1 + I1*F0 + F0*F1
*
FIX_MULT MOVEM.L D2-D5,-(A7)
CLR.W D5 * DEFAULT SIGN + ie 0's
TST.L D0 * SIGN?
BEQ.S PRODZERO
BGE.S ONE_POS * IF POSITIVE DO NOTHING
ONE_NEG NEG.L D0 * MAKE +
TST.L D1 * SIGN?
BEQ.S PRODZERO
BGE.S RESULTNEG * IF +: JUST SIGNAL RESULT SIGN
NEG.L D1 * MAKE +
BRA.S FM_CONT0 * IF -: RESULT WILL BE +
ONE_POS TST.L D1 * SIGN?
BEQ.S PRODZERO
BGE.S FM_CONT0
NEG.L D1 * MAKE +
RESULTNEG MOVEQ #-1,D5 * SIGN - ie. 1's
*
FM_CONT0 MOVE.L D0,D2
MOVE.L D1,D3
SWAP D2 * GET I0
SWAP D3 * GET I1
MULU D3,D2 * D2.L=I1 * I0
SWAP D2 * RETURN TO INTEGER POSITION
MOVE.L D2,D4 * SAVE RESULT

MULU D0,D3 * D3.L=F0 * I1
ADD.L D3,D4 * D4.L=I1*I0 + F0*I1

SWAP D0 * GET I0
MOVE.W D1,D3 * GET F1
MULU D0,D3 * D3.L=I0 * F1
ADD.L D3,D4 * D4.L=I1*I0 + F0*I1 + F1*I0

SWAP D0 * GET F0
MULU D1,D0 * D0=F0/64K * F1/64K
MOVE.W #0,D0
SWAP D0 * D0=F0*F1/64K
ADD.L D0,D4 * D4.L=D4.L> + F0*F1
MOVE.L D4,D1 * GET RESULT
*
MULTSIGN TST.W D5
BEQ.S FM_EXIT
NEG.L D1
FM_EXIT
MOVEM.L (A7)+,D2-D5
RTS
PRODZERO MOVEQ #0,D1
BRA.S FM_EXIT
*
************************************************************************
*
*

I have yet to write the ultimate n-bit * n-bit Fixed Point multiplication routine, however the need is there & sooner or later the topic will arise. I wonder if there is anybody who has interfaced an MC68881/2 FPU to the QL?

>eof

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.