-
Notifications
You must be signed in to change notification settings - Fork 6
/
mult60.a
85 lines (75 loc) · 2.31 KB
/
mult60.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
; mult60.a
; based on Dr Jefyll, http://forum.6502.org/viewtopic.php?f=9&t=689&start=0#p19958
; - adjusted to use fixed zero page addresses
; - removed 'decrement to avoid clc' as this is slower on average
; - rearranged memory use to remove final memory copy and give LSB first order to result
; - X counter counts up from $fe to avoid cpx
;
;
; multiplicand
; +------+------+
; | +1 | +0 |
; +------+------+
; ||
; _||_ add
; \ /
; \/ initially set to
; result multiplier
; +------+------+ +------+------+
; | +3 | +2 | | +1 | +0 |
; +------+------+ +------+------+
;
; (1) first 8 times around loop, shift right: result+3 into +2 into +0:
;
; --------> shift >
; \_______/
;
; (2) final 8 times around loop, shift right: result+3 into +2 into +1:
;
; --------> shift ->
;
; 16 bit x 16 bit unsigned multiply, 32 bit result
; Average cycles: 527.00
; 39 bytes
multiplicand = $02 ; 2 bytes
multiplier = $04 ; 2 bytes
result = $04 ; 4 bytes (note: shares memory with multiplier)
* = $0200
; 16 bit x 16 bit unsigned multiply, 32 bit result
;
; On Entry:
; multiplier: two byte value
; multiplicand: two byte value
; On Exit:
; result: four byte product (note: 'result' shares memory with 'multiplier')
mult
lda #0 ;
sta result+2 ; 16 bits of zero in A, result+2
; (think of A as a local cache of result+3)
; Note: First 8 shifts are A -> result+2 -> result
; Final 8 shifts are A -> result+2 -> result+1
ldx #$fe ; count for outer loop. Loops twice.
; outer loop (2 times)
outer_loop
ldy #8 ; count for inner loop
lsr result+2,x ; think "result" then later "result+1"
; inner loop (8 times)
inner_loop
bcc +
sta result+3 ; remember A
lda result+2
clc
adc multiplicand
sta result+2
lda result+3 ; recall A
adc multiplicand+1
+
ror ; shift
ror result+2
ror result+2,x ; think "result" then later "result+1"
dey
bne inner_loop ; go back for 1 more shift?
inx
bne outer_loop ; go back for 8 more shifts?
sta result+3 ; ms byte of hi-word of result
rts