2 ; 2010-11-02, Ullrich von Bassewitz
3 ; 2014-09-10, Greg King
5 ; CC65 runtime: 8x8 => 16 signed multiplication
8 .export imul8x8r16, imul8x8r16m
9 .importzp ptr1, ptr3, tmp1
13 ;---------------------------------------------------------------------------
14 ; 8x8 => 16 signed multiplication routine.
16 ; multiplicand multiplier product
18 ; -------------------------------------------------------------
19 ; .A (ptr3-low) ptr1-low .XA
30 @L7: stx ptr3+1 ; Extend sign of Left-Hand Side
31 ldy #<$0000 ; Clear .XY accumulator
35 bpl @L2 ; Branch always
37 @L0: tya ; Add current multiplicand
46 @L2: lsr ptr1 ; Get next bit of Right-Hand Side into carry
48 bnz @L1 ; Loop if more one-bits in multiplier
50 tya ; Put result into cc65's accumulator
53 ; The multiplier is negative.
54 ; Therefore, make it positive; and, subtract when multiplying.
59 bnz @L2 ; Branch always
61 @L0: tya ; Subtract current multiplicand
70 @L2: lsr ptr1 ; Get next bit of Right-Hand Side into carry
72 bnz @L1 ; Loop if more one-bits in multiplier
74 tya ; Put result into cc65's accumulator