Say hello to D-Pad Hero 2 repository
[dpadhero2.git] / multiply.asm
blob2bebe6758521cbca427371d15e9197339d7c0152
1 ; from http://www.6502.org/source/integers/32muldiv.htm
3 .dataseg
5 PROD .db[8]
6 MULR .db[4]
7 MULND .db[4]
9 .public PROD
10 .public MULR
11 .public MULND
13 .codeseg
15 .public MULTIPLY
17 MULTIPLY: lda #$00
18 sta PROD+4 ;Clear upper half of
19 sta PROD+5 ;product
20 sta PROD+6
21 sta PROD+7
22 ldx #$20 ;Set binary count to 32
23 @@SHIFT_R: lsr MULR+3 ;Shift multiplyer right
24 ror MULR+2
25 ror MULR+1
26 ror MULR
27 bcc @@ROTATE_R ;Go rotate right if c = 0
28 lda PROD+4 ;Get upper half of product
29 clc ; and add multiplicand to
30 adc MULND ; it
31 sta PROD+4
32 lda PROD+5
33 adc MULND+1
34 sta PROD+5
35 lda PROD+6
36 adc MULND+2
37 sta PROD+6
38 lda PROD+7
39 adc MULND+3
40 @@ROTATE_R: ror a ;Rotate partial product
41 sta PROD+7 ; right
42 ror PROD+6
43 ror PROD+5
44 ror PROD+4
45 ror PROD+3
46 ror PROD+2
47 ror PROD+1
48 ror PROD
49 dex ;Decrement bit count and
50 bne @@SHIFT_R ; loop until 32 bits are
51 ; clc ; done
52 ; lda MULXP1 ;Add dps and put sum in MULXP2
53 ; adc MULXP2
54 ; sta MULXP2
55 rts
57 .end