Certainly! Below is a 68000 assembly function that multiplies two signed 32-bit integers (in registers) and produces a 64-bit signed result in two registers. The function does not use shift-and-add multiplication and instead uses the decomposition of the multiplication into four 16x16-bit multiplications, combining the middle terms first before final addition.
Explanation
Given two signed 32-bit integers:
D0 = multiplicand (signed 32-bit)
D1 = multiplier (signed 32-bit)
We want to compute:
code Copy code
Result = D0 * D1 (64-bit signed)
Represent each 32-bit number as two 16-bit halves:
code Copy code
D0 = (D0_hi << 16) + D0_lo
D1 = (D1_hi << 16) + D1_lo
Where:
D0_hi = high 16 bits of D0 (signed)
D0_lo = low 16 bits of D0 (unsigned)
D1_hi = high 16 bits of D1 (signed)
D1_lo = low 16 bits of D1 (unsigned)
The product expands to:
code Copy code
D0 * D1 = (D0_hi * D1_hi << 32) + ((D0_hi * D1_lo + D0_lo * D1_hi) << 16) + (D0_lo * D1_lo)
We add the middle terms first, then combine.
Registers used
Input: D0 and D1 (signed 32-bit)
Output: D2:D3 = 64-bit signed result (D2 = high 32 bits, D3 = low 32 bits)
Temporaries: D4, D5, D6, D7
Code
asm Copy code
; Multiply signed 32-bit D0 * D1 -> 64-bit result in D2:D3
; No shift-and-add method, middle terms added first
mul32x32_64:
; Extract high and low 16 bits of D0
move.w D0, D4 ; D4 = low 16 bits of D0 (unsigned)
swap D0 ; swap halves of D0
move.w D0, D5 ; D5 = high 16 bits of D0 (signed)
swap D0 ; restore D0
; Extract high and low 16 bits of D1
move.w D1, D6 ; D6 = low 16 bits of D1 (unsigned)
swap D1
move.w D1, D7 ; D7 = high 16 bits of D1 (signed)
swap D1 ; restore D1
; Sign extend high 16 bits to 32 bits
ext.w D5 ; D5 = signed high 16 bits of D0
ext.w D7 ; D7 = signed high 16 bits of D1
; Zero extend low 16 bits to 32 bits
andi.l #$FFFF, D4 ; D4 = unsigned low 16 bits of D0
andi.l #$FFFF, D6 ; D6 = unsigned low 16 bits of D1
; Calculate partial products
; p0 = D4 * D6 (low * low) unsigned * unsigned -> 32-bit
muls.w D6, D4 ; D4 = p0 (32-bit)
; p1 = D5 * D6 (high * low) signed * unsigned -> 32-bit
muls.w D6, D5 ; D5 = p1 (32-bit)
; p2 = D4 * D7 (low * high) unsigned * signed -> 32-bit
muls.w D7, D6 ; D6 = p2 (32-bit)
; p3 = D5 * D7 (high * high) signed * signed -> 32-bit
muls.w D7, D5 ; Wait, D5 is used for p1, we need to save p1 before overwriting
; We need to save p1 and p2 before computing p3
; Let's rearrange to avoid overwriting registers:
; Re-extract and recompute partial products with better register usage:
mul32x32_64_revised:
; Extract high and low 16 bits of D0
move.w D0, D4 ; D4 = low 16 bits of D0 (unsigned)
swap D0
move.w D0, D5 ; D5 = high 16 bits of D0 (signed)
swap D0
; Extract high and low 16 bits of D1
move.w D1, D6 ; D6 = low 16 bits of D1 (unsigned)
swap D1
move.w D1, D7 ; D7 = high 16 bits of D1 (signed)
swap D1
; Sign extend high 16 bits to 32 bits
ext.w D5 ; D5 = signed high 16 bits of D0
ext.w D7 ; D7 = signed high 16 bits of D1
; Zero extend low 16 bits to 32 bits
andi.l #$FFFF, D4 ; D4 = unsigned low 16 bits of D0
andi.l #$FFFF, D6 ; D6 = unsigned low 16 bits of D1
; p0 = D4 * D6 (low * low)
muls.w D6, D4 ; D4 = p0
; p1 = D5 * D6 (high * low)
move.l D5, D1 ; save D5 in D1 temporarily
muls.w D6, D5 ; D5 = p1
; p2 = D4(low) * D7(high)
move.l D4, D0 ; save p0 in D0 temporarily
muls.w D7, D6 ; D6 = p2
; p3 = D1(high) * D7(high)
move.l D1, D0 ; restore D5 original value in D0
muls.w D7, D1 ; D1 = p3
; Now add middle terms p1 + p2
add.l D6, D5 ; D5 = p1 + p2
; Now combine results:
; Result low 32 bits = p0 + ((p1 + p2) << 16)
; Result high 32 bits = p3 + carry from low addition + ((p1 + p2) >> 16)
; Add (p1+p2) << 16 to p0
; To do this, split p5 (p1+p2) into high and low 16 bits
; Extract low 16 bits of p1+p2
move.w D5, D6 ; D6 = low 16 bits of p1+p2
lsl.l #16, D6 ; D6 = (p1+p2 low 16 bits) << 16
; Add to p0 (D4)
add.l D6, D4 ; D4 = p0 + ((p1+p2 low 16 bits) << 16)
; Check carry from addition
bcc no_carry1
addq.l #1, D1 ; carry to high 32 bits (p3)
no_carry1:
; Extract high 16 bits of p1+p2
lsr.l #16, D5 ; D5 = (p1+p2) >> 16
; Add to high 32 bits (p3)
add.l D5, D1 ; D1 = p3 + ((p1+p2) >> 16)
; Store result
move.l D1, D2 ; high 32 bits
move.l D4, D3 ; low 32 bits
rts
Summary
Inputs: D0 and D1 (signed 32-bit)
Outputs: D2:D3 (signed 64-bit result)
No shift-and-add multiplication used
Middle terms added first before final combination
If you want me to explain or optimize further, just ask!