forked from jkotlinski/durexforth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.asm
More file actions
executable file
·150 lines (137 loc) · 3.21 KB
/
math.asm
File metadata and controls
executable file
·150 lines (137 loc) · 3.21 KB
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
142
143
144
145
146
147
148
149
150
+BACKLINK
!byte 2
!text "u<"
U_LESS
ldy #0
lda MSB, x
cmp MSB + 1, x
bcc .false
bne .true
; ok, msb are equal...
lda LSB + 1, x
cmp LSB, x
bcs .false
.true
dey
.false
inx
sty MSB, x
sty LSB, x
rts
+BACKLINK
!byte 1
!text "-"
MINUS
lda LSB + 1, x
sec
sbc LSB, x
sta LSB + 1, x
lda MSB + 1, x
sbc MSB, x
sta MSB + 1, x
inx
rts
product = W
+BACKLINK
!byte 3
!text "um*"
; wastes W, W2, y
U_M_STAR
lda #$00
sta product+2 ; clear upper bits of product
sta product+3
ldy #$10 ; set binary count to 16
.shift_r
lsr MSB + 1, x ; multiplier+1 ; divide multiplier by 2
ror LSB + 1, x ; multiplier
bcc rotate_r
lda product+2 ; get upper half of product and add multiplicand
clc
adc LSB, x ; multiplicand
sta product+2
lda product+3
adc MSB, x ; multiplicand+1
rotate_r
ror ; rotate partial product
sta product+3
ror product+2
ror product+1
ror product
dey
bne .shift_r
lda product
sta LSB + 1, x
lda product + 1
sta MSB + 1, x
lda product + 2
sta LSB, x
lda product + 3
sta MSB, x
rts
; UM/MOD by Garth Wilson
; http://6502.org/source/integers/ummodfix/ummodfix.htm
+BACKLINK
!byte 6
!text "um/mod"
; ( lsw msw divisor -- rem quot )
N = W
SEC
LDA LSB+1,X ; Subtract hi cell of dividend by
SBC LSB,X ; divisor to see if there's an overflow condition.
LDA MSB+1,X
SBC MSB,X
BCS oflo ; Branch if /0 or overflow.
LDA #17 ; Loop 17x.
STA N ; Use N for loop counter.
loop: ROL LSB+2,X ; Rotate dividend lo cell left one bit.
ROL MSB+2,X
DEC N ; Decrement loop counter.
BEQ end ; If we're done, then branch to end.
ROL LSB+1,X ; Otherwise rotate dividend hi cell left one bit.
ROL MSB+1,X
lda #0
sta N+1
ROL N+1 ; Rotate the bit carried out of above into N+1.
SEC
LDA LSB+1,X ; Subtract dividend hi cell minus divisor.
SBC LSB,X
STA N+2 ; Put result temporarily in N+2 (lo byte)
LDA MSB+1,X
SBC MSB,X
TAY ; and Y (hi byte).
LDA N+1 ; Remember now to bring in the bit carried out above.
SBC #0
BCC loop
LDA N+2 ; If that didn't cause a borrow,
STA LSB+1,X ; make the result from above to
STY MSB+1,X ; be the new dividend hi cell
bcs loop ; and then branch up.
oflo: LDA #$FF ; If overflow or /0 condition found,
STA LSB+1,X ; just put FFFF in both the remainder
STA MSB+1,X
STA LSB+2,X ; and the quotient.
STA MSB+2,X
end: INX
jmp SWAP
+BACKLINK
!byte 2
!text "m+" ; ( d1 u -- d2 )
ldy #0
lda MSB,x
bpl +
dey
+ clc
lda LSB,x
adc LSB+2,x
sta LSB+2,x
lda MSB,x
adc MSB+2,x
sta MSB+2,x
tya
adc LSB+1,x
sta LSB+1,x
tya
adc MSB+1,x
sta MSB+1,x
inx
rts