# MathsStuff

Jan 12th, 2022
1,153
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. ; MATH ZP LOCATIONS
2. MATHWORKSPACE = &A0 ; 160d, START OF MATH ROUTINES WORKSPACE BLOCK
3. ; MATH WORKSPACE BLOCK LAYOUT
4. ;MATHWORKSPACE
5.     MWS_INPUT1  = MATHWORKSPACE + 0      ; (b)
6.     MWS_INPUT2  = MATHWORKSPACE + 1      ; (b)
7.     MWS_INPUT3  = MATHWORKSPACE + 2      ; (b)
8.     MWS_INPUT4  = MATHWORKSPACE + 3      ; (b)
9.     MWS_OUTPUT1 = MATHWORKSPACE + 4     ; (b)
10.     MWS_OUTPUT2 = MATHWORKSPACE + 5     ; (b)
11.     MWS_OUTPUT3 = MATHWORKSPACE + 6     ; (b)
12.     MWS_OUTPUT4 = MATHWORKSPACE + 7     ; (b)
13.
14. ;
16. ;   INPUT:  MWS_INPUT1 (w), MWS_INPUT3 (w)
17. ;   OUTPUT: OUTPUT1 (w)
19. {
20.     PHA
21.     CLC
22.     LDA MWS_INPUT1
24.     STA MWS_OUTPUT1         ; store sum of LSBs
25.     LDA MWS_INPUT2
27.     STA MWS_OUTPUT2         ; the previous calculation
28.     PLA
29.     RTS
30. }
31.
32.
33. ;
34. ; SUBTRACT16
35. ;   INPUT:  MWS_INPUT1 (w), MWS_INPUT3 (w)
36. ;   OUTPUT: OUTPUT1 (w)
37. .MATH_SUBTRACT16
38. {
39.     PHA
40.     SEC                     ; set carry for borrow purpose
41.     LDA MWS_INPUT1
42.     SBC MWS_INPUT3          ; perform subtraction on the LSBs
43.     STA MWS_OUTPUT1
44.     LDA MWS_INPUT2          ; do the same for the MSBs, with carry
45.     SBC MWS_INPUT4          ; set according to the previous result
46.     STA MWS_OUTPUT2
47.     PLA
48.     RTS
49. }
50.
51.
52. ;
53. ; MULTIPLY8
54. ;   INPUT:  MWS_INPUT1 (b), MWS_INPUT2 (b)
55. ;   OUTPUT: OUTPUT1 (w)
56. .MATH_MULTIPLY8
57. {
58.     ; A * 256 + X = MWS_INPUT1 * MWS_INPUT2
59.     PUSHAXY
60.     LDA #\$00
61.     LDX #\$08
62.     CLC
63. .m0
64.     BCC m1
65.     CLC
67. .m1
68.     ROR A
69.     ROR MWS_INPUT1
70.     DEX
71.     BPL m0
72.     LDX MWS_INPUT1
73.     ; store A, X in MWS_OUTPUT2, MWS_OUTPUT1
74.     STA MWS_OUTPUT2
75.     STX MWS_OUTPUT1
76.     POPAXY
77.     RTS
78. }
79.
80.