-
Notifications
You must be signed in to change notification settings - Fork 6
/
mult69.a
55 lines (50 loc) · 1.66 KB
/
mult69.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
; mult69.a
; from 'Machine Language Routines for the Commodore 64 and 128' by Todd D Heimarck and Patrick Parrish (1987) https://archive.org/details/Compute_s_Machine_Language_Routines_for_the_Commodore_64_and_128/page/363/mode/2up
;
; 16 bit x 16 bit unsigned multiply, 32 bit result
; Average cycles: 946.52
; 65 bytes
* = $0200
b1 = $02 ; 2 bytes
b2 = $04 ; 2 bytes
work = $06 ; 2 bytes
total = $08 ; 4 bytes
countr = $0c
mulshf
ldy #3 ; four bytes
lda #0 ; zero out total
zout
sta total,y ; store it
dey ; count down
bpl zout ; and loop back
lda b2 ; copy b2 to work
sta work
lda b2+1
sta work+1
lda #16 ; there are 16 shifts, so
sta countr ; set up a counter
mullp
asl work ; shift the low byte
rol work+1 ; into the high byte
bcc bigshf ; if the bit is off, skip the add
clc ; clear carry before add
lda b1 ; low byte
adc total ; add to total (low)
sta total ; store it
lda b1+1 ; second byte of four
adc total+1 ; add it
sta total+1 ; store it
bcc bigshf ; if carry clear, branch forward
inc total+2 ; else add 1 to third byte
bne bigshf ; if not zero, skip the fourth
inc total+3 ; else, get [*increment*] the fourth
bigshf
dec countr ; count down
bne shifit ; shift if there's more
rts ; else, quit
shifit
asl total ; multiply by 2
rol total+1 ; all...
rol total+2 ; four...
rol total+3 ; bytes
jmp mullp ; repeat it again