2000-05-28 13:40:48 +00:00
|
|
|
;
|
2009-08-17 20:48:28 +00:00
|
|
|
; Ullrich von Bassewitz, 2009-08-17
|
2000-05-28 13:40:48 +00:00
|
|
|
;
|
|
|
|
; CC65 runtime: multiplication for ints
|
|
|
|
;
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.export tosumulax, tosmulax
|
2009-08-17 20:48:28 +00:00
|
|
|
.import mul8x16, mul8x16a ; in mul8.s
|
2018-05-21 16:18:01 +00:00
|
|
|
.import popptr1
|
|
|
|
.importzp tmp1, ptr1, ptr4
|
2009-08-17 20:48:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
;---------------------------------------------------------------------------
|
|
|
|
; 16x16 multiplication routine
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
tosmulax:
|
|
|
|
tosumulax:
|
2013-05-09 11:56:54 +00:00
|
|
|
sta ptr4
|
2009-08-17 20:48:28 +00:00
|
|
|
txa ; High byte zero
|
|
|
|
beq @L3 ; Do 8x16 multiplication if high byte zero
|
2013-05-09 11:56:54 +00:00
|
|
|
stx ptr4+1 ; Save right operand
|
2018-05-28 23:47:04 +00:00
|
|
|
jsr popptr1 ; Get left operand (Y=0, X untouched by popptr1)
|
2009-08-17 20:48:28 +00:00
|
|
|
|
2018-05-21 16:18:01 +00:00
|
|
|
; Do ptr4:ptr4+1 * ptr1:ptr1+1 --> AX
|
2009-08-17 20:48:28 +00:00
|
|
|
|
2022-04-17 14:06:22 +00:00
|
|
|
tya ; A = 0
|
2018-05-28 23:47:04 +00:00
|
|
|
ldy ptr1+1 ; check if lhs is 8 bit only
|
2009-08-17 20:48:28 +00:00
|
|
|
beq @L4 ; -> we can do 8x16 after swap
|
2013-05-09 11:56:54 +00:00
|
|
|
sta tmp1
|
|
|
|
ldy #16 ; Number of bits
|
2009-08-17 20:48:28 +00:00
|
|
|
|
|
|
|
lsr ptr4+1
|
|
|
|
ror ptr4 ; Get first bit into carry
|
|
|
|
@L0: bcc @L1
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
clc
|
2018-05-21 16:18:01 +00:00
|
|
|
adc ptr1
|
|
|
|
tax
|
2022-04-17 14:06:22 +00:00
|
|
|
lda ptr1+1 ; Hi byte of left op
|
2013-05-09 11:56:54 +00:00
|
|
|
adc tmp1
|
|
|
|
sta tmp1
|
2018-05-21 16:18:01 +00:00
|
|
|
txa
|
2009-08-17 20:48:28 +00:00
|
|
|
|
|
|
|
@L1: ror tmp1
|
2013-05-09 11:56:54 +00:00
|
|
|
ror a
|
|
|
|
ror ptr4+1
|
|
|
|
ror ptr4
|
2009-08-17 20:48:28 +00:00
|
|
|
dey
|
|
|
|
bne @L0
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
lda ptr4 ; Load the result
|
|
|
|
ldx ptr4+1
|
|
|
|
rts ; Done
|
2009-08-17 20:48:28 +00:00
|
|
|
|
|
|
|
; High byte of rhs is zero, jump to the 8x16 routine instead
|
|
|
|
|
|
|
|
@L3: jmp mul8x16
|
|
|
|
|
2018-05-28 23:47:04 +00:00
|
|
|
; If the high byte of lhs is zero, swap the operands in ptr1/4 and
|
|
|
|
; use the 8x16 routine. On entry, A and Y are zero and X has the value
|
|
|
|
; of ptr4+1
|
2009-08-17 20:48:28 +00:00
|
|
|
|
2018-05-28 23:47:04 +00:00
|
|
|
@L4: stx ptr1+1 ; Store hi-byte from ptr4
|
|
|
|
ldy ptr1 ; Save right operand (8 bit)
|
2009-08-17 20:48:28 +00:00
|
|
|
ldx ptr4 ; Copy left 16 bit operand to right
|
2018-05-21 16:18:01 +00:00
|
|
|
stx ptr1
|
2009-08-17 20:48:28 +00:00
|
|
|
sty ptr4 ; Copy low 8 bit of right op to left
|
|
|
|
ldy #8
|
2018-05-28 23:47:04 +00:00
|
|
|
jmp mul8x16a ; There, ptr4+1 will be also cleared
|
2000-05-28 13:40:48 +00:00
|
|
|
|