Skip to content
Newer
Older
100644 248 lines (235 sloc) 4.24 KB
c9ec1a0 @M4v3R Refactoring of some static assembly code - moved it to lib.dasm
authored
1 ; get key press
2 ; also increments timer (for randomization)
3 ; uses Z register as pointer to keyboard buffer
4 ; returns the key code in A
5 :getkey
6 ADD [timer], 1
7 IFE [Z], 0
8 SET PC, POP
9 SET A, [Z]
10 SET [Z], 0
11 ADD Z, 1
12 AND Z, 0x900f ; the buffer is 16 words long
13 MUL [rnd1], [timer]
14 ADD [rnd2], O
15 SET PC, POP
16
17 ; get string length
18 ; A - string address in memory, with highest bit set
19 ; to signal string variable type
20 :strlen
21 IFG 0xF000, A ; get rid of highest bit, unless it's a stack address
22 AND A, 0x7fff
23 SET B, A
24 SET A, 0
25 :strlen1
f111b8d @M4v3R LEN function, FONT statement - loading custom fonts! (see docs)
authored
26 IFE [B], 0 ; if character is 00 - end of string
c9ec1a0 @M4v3R Refactoring of some static assembly code - moved it to lib.dasm
authored
27 SET PC, POP ; end function
28 ADD A, 1 ; increment char count
29 ADD B, 1 ; increment char address
30 SET PC, strlen1
31
b3bb1a3 @M4v3R Output line number on error; more helpful error message when forgot E…
authored
32 ; prints a single character on the screen
c9ec1a0 @M4v3R Refactoring of some static assembly code - moved it to lib.dasm
authored
33 ; A - character code
34 ; X - location on the terminal
35 ; Y - color/style mask
36 :printchar
37 SET [0x8000+X], A ; put character to video memory + current location
38 BOR [0x8000+X], Y ; apply color/style
39 ADD X, 1
40 IFG X, 0x1ff ; end of terminal - go back to first row and column
41 SET X, 0
42 SET PC, POP
43
44 ; prints an integer on the screen, converting it to string (itoa)
45 ; A - number to print
46 :printint
47 SET I, 0
48 :printint1
49 SET B, A
50 MOD A, 0xa
51 ADD A, 0x30
52 SET PUSH, A
53 SET A, B
54 DIV A, 0xa
55 ADD I, 1
56 IFN A, 0
57 SET PC, printint1
58 :printint2
59 SET A, POP
60 JSR printchar
61 SUB I, 1
62 IFN I, 0
63 SET PC, printint2
64 SET A, POP
65 SET PC, POP
66
67 ; prints a string on the screen
68 ; A - address of 0 terminated string
69 :printstr
70 IFG 0xF000, A ; see strlen function above
71 AND A, 0x7fff
72 SET I, A
73 :printstr1
74 IFE [I], 0
75 SET PC, POP
76 SET A, [I]
77 JSR printchar
78 ADD I, 1
79 SET PC, printstr1
80
81 ; sets location register X to point at new line
82 :printnl
83 SHR X, 5 ; shifting 5 bits to the right means divide by 32 - terminal width
84 ADD X, 1 ; next row
85 SHL X, 5 ; multiply by 32 to get address of next row
86 SET PC, POP
87
88 ; general purpose print function
89 ; determines which data type is being print and calls specific functions
90 ; A - address of data to be printed
91 :print
92 SET B, A
93 SHR B, 15 ; get the highest bit
94 IFE B, 0 ; if it's 0 - we have a integer
95 JSR printint
96 IFE B, 1 ; if it's 1 - we have a string
97 JSR printstr
98 SET PC, POP
99
100 ; wait for user input followed by Enter key, stores it on the stack
101 ; returns pointer to the string in A
102 :input
103 SET C, SP
104 SET PUSH, 0x0
105 SET I, SP
106 SUB I, 1
107 :input1
108 SET A, 0
109 JSR getkey
110 IFE A, 0
111 SET PC, input1
112 IFE A, 0xa
113 SET PC, input2
114 IFE A, 0x8
115 SET PC, inputbsp
116 SET PUSH, A
117 JSR printchar
118 SET PC, input1
119 :inputbsp
120 SET POP, 0
121 SUB X, 1
122 SET [0x8000+X], 0
123 BOR [0x8000+X], Y
124 SET PC, input1
125 :input2
126 SET B, SP
127 SET J, B
128 :input3
129 SET A, [B]
130 SET [B], [I]
131 SET [I], A
132 ADD B, 1
133 SUB I, 1
134 IFG B, I
135 SET PC, input4
136 SET PC, input3
137 :input4
138 SET A, J
139 BOR A, 0x8000
140 SET PC, [C]
141
142 ; compare two strings to see if they're equal
143 :comparestr
144 SET I, POP
145 ADD I, 2
146 SET PUSH, I
147 IFG 0xF000, A ; see strlen function
148 AND A, 0x7fff
149 IFG 0xF000, B
150 AND B, 0x7fff
151 SET I, 0
152 SET C, 0
153 :comparestr1
154 IFN [A], [B]
155 SET PC, comparestr2
156 IFN [A], 0
157 IFE [B], 0
158 SET PC, POP
159 ADD A, 1
160 ADD B, 1
161 SET PC, comparestr1
162 :comparestr2
163 SET C, 1
164 SET PC, POP
165
166 ; convert string representation of a number to an integer
167 ; A - address of the string
168 ; returns resulting integer in A
169 :atoi
170 SET C, 0
171 IFG 0xF000, A ; see strlen function
172 AND A, 0x7fff
173 IFE [A], 0
174 SET PC, atoi2
175 :atoi1
176 IFG [A], 47
177 IFG [A], 57
178 SET PC, atoi2
179 MUL C, 10
180 SET B, [A]
181 SUB B, 48
182 ADD C, B
183 ADD A, 1
184 IFE [A], 0
185 SET PC, atoi2
186 SET PC, atoi1
187 :atoi2
188 SET A, C
189 SET PC, POP
190
7541a07 @M4v3R Added SQR - compute square root from an integer
authored
191 ; compute square root of a number
192 ; A - input number (16-bit integer)
193 ; returns the result (integer) in A
194 ; author: Mrrl (on the 0x10cforum.com)
195 :sqrt
196 SET B, 1
197 SET I, A
198 IFG 0x100, A
199 ADD PC, 2
200 SHR A, 8
201 SET B, 0x10
202 IFG 0x10, A
203 ADD PC, 2
204 SHR A, 4
205 SHL B, 2
206 ADD A, 4
207 MUL B, A
208 SHR B, 2
209 SET A, I
210 DIV A, B
211 ADD B, A
212 SHR B, 1
213 SET A, I
214 DIV A, B
215 ADD B, A
216 SHR B, 1
217 SET A, I
218 DIV A, B
219 ADD A, B
220 SHR A, 1
221 SET B, A
222 MUL B, A
223 IFG B, I
224 SUB A, 1
225 SET PC, POP
226
c9ec1a0 @M4v3R Refactoring of some static assembly code - moved it to lib.dasm
authored
227 ; get a pseudo-random number
228 ; returns the number in A
7541a07 @M4v3R Added SQR - compute square root from an integer
authored
229 ; author: Entroper (github.com/Entroper)
c9ec1a0 @M4v3R Refactoring of some static assembly code - moved it to lib.dasm
authored
230 :rand
231 SET B, [rnd1]
232 SET A, [rnd2]
233 MUL [rnd1], 0x660D
234 SET C, O
235 MUL A, 0x660D
236 ADD A, C
237 MUL B, 0x0019
238 ADD A, B
239 ADD [rnd1], 1
240 ADD A, O
241 SET [rnd2], A
242 SET PC, POP
243
244 ; end of program
245 ; infinite loop
246 :end
247 SET PC, end
Something went wrong with that request. Please try again.