-
Notifications
You must be signed in to change notification settings - Fork 6
/
smult4.a
67 lines (62 loc) · 1.46 KB
/
smult4.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
; smult4.a
; from https://llx.com/Neil/a2/mult.html
; this has inefficient dealing with sign. See smult5.a for a more efficient solution
;
; 8 bit x 8 bit signed multiply, 16 bit result
; Average cycles: 242.52
; 67 bytes
NUM1 = $02 ;
NUM2 = $03 ;
RESULT = $04 ; 2 bytes
* = $0200
; input in NUM1,NUM2
; output in RESULT, RESULT+1
mult
lda NUM1 ; Compute sign of result
eor NUM2
php ; Save it on the stack
lda NUM1 ; Is NUM1 negative?
bpl T1
eor #$FF ; If so, make it positive
clc
adc #1
sta NUM1
T1
lda NUM2 ; Is NUM2 negative?
bpl T2
eor #$FF ; If so, make it positive
clc
adc #1
sta NUM2
T2
jsr MUL1BYTE ; Do the unsigned multiplication
sta RESULT+1
plp ; Get sign of result
bpl T3
lda #0 ; If negative, negate result
sec
sbc RESULT
sta RESULT
lda #0
sbc RESULT+1
sta RESULT+1
T3
rts
; ---------------------------------------------------------------------------------------
MUL1BYTE
; See mult19.a
; calculate NUM1 * NUM2
; Result in A (high byte) and RESULT (low byte)
lda #0 ; Initialize RESULT to 0
ldx #8 ; There are 8 bits in NUM2
L1
lsr NUM2 ; Get low bit of NUM2
bcc L2 ; 0 or 1?
clc ; If 1, add NUM1
adc NUM1
L2
ror ; "Stairstep" shift (catching carry from add)
ror RESULT
dex
bne L1
rts