-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path04 - Tutorial Four.asm
75 lines (66 loc) · 1.93 KB
/
04 - Tutorial Four.asm
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
*=$0900
jmp MULTIPLY
divisor
WORD 115
dividend
WORD 14567
remainder
WORD 0
result = dividend ;save memory by reusing divident to store the result
divide
lda #0 ;preset remainder to 0
sta remainder
sta remainder + 1
ldx #16 ;repeat for each bit: ...
divloop
asl dividend ;dividend lb & hb*2, msb -> Carry
rol dividend + 1
rol remainder ;remainder lb & hb * 2 + msb from carry
rol remainder + 1
lda remainder
sec
sbc divisor ;substract divisor to see if it fits in
tay ;lb result -> Y, for we may need it later
lda remainder + 1
sbc divisor + 1
bcc skip ;if carry=0 then divisor didn't fit in yet
sta remainder + 1 ;else save substraction result as new remainder,
sty remainder
inc result ;and INCrement result cause divisor fit in 1 times
skip
dex
bne divloop
rts
;16-bit multiply with 32-bit Result
MULTIPLIER
WORD 12
MULTIPLICAND
WORD 200
RESULT
WORD 0,0
MULTIPLY
lda #$00 ; Load Zero
sta RESULT ; clear Lower bytes of product
sta RESULT + 1 ;
sta RESULT + 2 ; clear upper bytes of product
sta RESULT + 3 ;
ldx #$10 ; set binary count to 16
SHIFT_R
lsr MULTIPLIER + 1 ; divide multiplier by 2
ror MULTIPLIER ;
bcc ROTATE_R ; Branch if not digit put in carry
lda RESULT + 2 ; get upper half of product and add multiplicand
clc ; Clear Carry for Addition
adc MULTIPLICAND ; Add Multiplicand
sta RESULT + 2 ; Store In Result
lda RESULT + 3 ; Load High Byte
adc MULTIPLICAND + 1; Add Multiplicand
ROTATE_R
ror ; rotate partial product
sta RESULT + 3 ;
ror RESULT + 2 ;
ror RESULT + 1 ;
ror RESULT ;
dex ;
bne SHIFT_R ;
rts ;