-
Notifications
You must be signed in to change notification settings - Fork 6
/
mult42.a
141 lines (125 loc) · 2.61 KB
/
mult42.a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
; mult42.a
; based on the 8 bit multiply of mult16.a
; I combined the results of four calls to this to get the 16x16 bit multiply
;
; 16 bit x 16 bit unsigned multiply, 32 bit result
; Average cycles: 403.83
; 647 bytes
inputA = $02 ; 2 bytes (a0,a1)
inputB = $04 ; 2 bytes (b0,b1)
result = $06 ; 4 bytes
fac = $0a
rlo = $0b
* = $0200
; align tables to start of a page for speed
sqrlo
!for i, 0, 255 {
!byte <(i*i)
}
sqrhi
!for i, 0, 255 {
!byte >(i*i)
}
; ***************************************************************************************
; 16 bit x 16 bit unsigned multiply, 32 bit result
;
; On Entry:
; inputA: multiplier (2 bytes)
; inputB: multiplicand (2 bytes)
;
; On Exit:
; result: product (4 bytes)
mult
lda inputA ;
ldx inputB ;
jsr mult8 ; (a0*b0)
sta result ; a is low byte
stx result+1 ; x is high byte
lda inputA+1 ;
ldx inputB ;
jsr mult8 ; (a1*b0)
clc ;
adc result+1 ;
sta result+1 ;
bcc + ;
inx ;
+
stx result+2 ;
lda inputA ;
ldx inputB+1 ;
jsr mult8 ; (a0*b1)
clc ;
adc result+1 ;
sta result+1 ;
txa ;
adc result+2 ;
sta result+2 ;
lda #0 ;
rol ; remember the carry for result+3
sta result+3 ;
lda inputA+1 ;
ldx inputB+1 ;
jsr mult8 ; (a1*b1)
clc ;
adc result+2 ;
sta result+2 ;
txa ;
adc result+3 ;
sta result+3 ;
rts ;
; ***************************************************************************************
; mult16.a tweaked for output parameters
; Calculate A*X with the product returned in A(low) and X (high)
; unsigned.
;
; 8 bit x 8 bit multiply, 16 bit result
;
; On Entry:
; A: multiplier
; X: multiplicand
;
; On Exit:
; A: low byte of product
; X: high byte of product
mult8
stx fac
cmp fac
bcs l1
sta fac ; if AC<fac
sec
txa
l1
tax
sbc fac
lsr
tay
txa
clc
adc fac
ror
tax
lda sqrlo,x
bcc l2
sbc sqrlo,y ; odd
sta rlo
lda sqrhi,x
sbc sqrhi,y
tax
clc
lda fac
adc rlo
sta rlo
bcc +
inx
+
lda rlo
rts
l2
sec ; even
sbc sqrlo,y
sta rlo
lda sqrhi,x
sbc sqrhi,y
tax
lda rlo
rts