Certainly! Below is a 68000 assembly language function to multiply two 32-bit signed registers together and store the 64-bit result in two registers. This implementation uses the MULU.L instruction to perform 32-bit unsigned multiplications and then handles the sign manually. The two middle terms are added together before calculating the entire result, as requested.
Register Usage:
- D0 and D1: First 32-bit operand (D0 contains the lower 16 bits, D1 contains the upper 16 bits).
- D2 and D3: Second 32-bit operand (D2 contains the lower 16 bits, D3 contains the upper 16 bits).
- D4 and D5: Will hold the 64-bit result (D4 contains the lower 32 bits, D5 contains the upper 32 bits).
Steps:
- Decompose the 32-bit operands into their lower and upper 16-bit components.
- Perform the four necessary 16-bit multiplications to get the partial products.
- Add the two middle terms together.
- Combine the partial products to form the 64-bit result.
- Handle the signs of the operands and adjust the result accordingly.
Here is the assembly code:
; Input:
; D0:D1 = first 32-bit signed operand
; D2:D3 = second 32-bit signed operand
; Output:
; D4:D5 = 64-bit signed result
MOVE.L D0,D6 ; Save the lower 16 bits of the first operand
MOVE.L D1,D7 ; Save the upper 16 bits of the first operand
MOVE.L D2,D8 ; Save the lower 16 bits of the second operand
MOVE.L D3,D9 ; Save the upper 16 bits of the second operand
; Decompose the 32-bit operands into their lower and upper 16-bit components
SWAP D6 ; D6 now contains the lower 16 bits of the first operand
SWAP D7 ; D7 now contains the upper 16 bits of the first operand
SWAP D8 ; D8 now contains the lower 16 bits of the second operand
SWAP D9 ; D9 now contains the upper 16 bits of the second operand
; Perform the four necessary 16-bit multiplications to get the partial products
MULU.W D6,D8 ; D0 = (LO1 * LO2)
MOVE.L D0,D10 ; Save LO1 * LO2 in D10
MULU.W D6,D9 ; D0 = (LO1 * HI2)
MOVE.L D0,D11 ; Save LO1 * HI2 in D11
MULU.W D7,D8 ; D0 = (HI1 * LO2)
MOVE.L D0,D12 ; Save HI1 * LO2 in D12
MULU.W D7,D9 ; D0 = (HI1 * HI2)
MOVE.L D0,D13 ; Save HI1 * HI2 in D13
; Add the two middle terms together
ADD.L D11,D12 ; D11 = (LO1 * HI2) + (HI1 * LO2)
MOVE.L D11,D14 ; Save the sum in D14
; Combine the partial products to form the 64-bit result
; Result = (LO1 * LO2) + ((LO1 * HI2 + HI1 * LO2) << 16) + (HI1 * HI2 << 32)
; Lower 32 bits of the result
MOVE.L D10,D4 ; D4 = LO1 * LO2 (lower 32 bits of the result)
; Add the middle terms to the lower 32 bits and store the carry
ADDX.L D14,D4 ; D4 = LO1 * LO2 + (LO1 * HI2 + HI1 * LO2), X = carry
; Upper 32 bits of the result
MOVE.L #0,D5 ; Clear D5
; Add the carry from the previous addition
BCHG #31,D5 ; Set the most significant bit if there was a carry
ADD.L D13,D5 ; D5 = HI1 * HI2 + carry
; Handle the signs of the operands and adjust the result accordingly
; Check the signs of the inputs
MOVE.L D0,D6 ; Restore D0 (first operand lower 16 bits)
MOVE.L D1,D7 ; Restore D1 (first operand upper 16 bits)
MOVE.L D2,D8 ; Restore D2 (second operand lower 16 bits)
MOVE.L D3,D9 ; Restore D3 (second operand upper 16 bits)
; Sign extension for the first operand
TST.L D7
BPL.S L1
MOVE.L #-1,D15
ASLL.L #16,D15
OR.L D15,D4
OR.L D15,D5
L1:
; Sign extension for the second operand
TST.L D9
BPL.S L2
EOR.L #-1,D4
EOR.L #-1,D5
ADDQ.L #1,D4
L2:
; Result is now in D4:D5
RTS
Explanation:
-
Decompose Operands:
- The 32-bit operands in
D0:D1 and D2:D3 are decomposed into their lower and upper 16-bit components using the SWAP instruction.
-
Partial Products:
- Four 16-bit multiplications are performed using the
MULU.W instruction to obtain the partial products: LO1 * LO2, LO1 * HI2, HI1 * LO2, and HI1 * HI2.
- The results are stored in
D10, D11, D12, and D13, respectively.
-
Add Middle Terms:
- The two middle terms (
LO1 * HI2 and HI1 * LO2) are added together and stored in D14.
-
Combine Partial Products:
- The lower 32 bits of the result are initialised with
LO1 * LO2 (D10) and then the middle terms (D14) are added to it using ADDX.L to account for any carry.
- The upper 32 bits of the result are initialised with
HI1 * HI2 (D13) and the carry from the previous addition is added.
-
Sign Handling:
- The signs of the original operands are checked.
- If the first operand is negative, the result is sign-extended by setting the upper 16 bits of the lower 32-bit result and the entire upper 32-bit result to 1.
- If the second operand is negative, the result is negated by flipping all the bits and adding 1 to the lower 32-bit result.
-
Result:
- The 64-bit result is stored in
D4:D5, with D4 containing the lower 32 bits and D5 containing the upper 32 bits.