2 ; Ullrich von Bassewitz, 2009-08-17
4 ; CC65 runtime: multiplication for ints
7 .export tosumulax, tosmulax
8 .import mul8x16, mul8x16a ; in mul8.s
10 .importzp sreg, tmp1, ptr4
13 ;---------------------------------------------------------------------------
14 ; 16x16 multiplication routine
20 beq @L3 ; Do 8x16 multiplication if high byte zero
21 stx ptr4+1 ; Save right operand
22 jsr popsreg ; Get left operand
24 ; Do ptr4:ptr4+1 * sreg:sreg+1 --> AX
27 ldx sreg+1 ; Get high byte into register for speed
28 beq @L4 ; -> we can do 8x16 after swap
30 ldy #16 ; Number of bits
33 ror ptr4 ; Get first bit into carry
39 txa ; hi byte of left op
51 lda ptr4 ; Load the result
55 ; High byte of rhs is zero, jump to the 8x16 routine instead
59 ; If the high byte of rhs is zero, swap the operands and use the 8x16
60 ; routine. On entry, A and X are zero
62 @L4: ldy sreg ; Save right operand (8 bit)
63 ldx ptr4 ; Copy left 16 bit operand to right
65 ldx ptr4+1 ; Don't store, this is done later
66 sty ptr4 ; Copy low 8 bit of right op to left