-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathconditional.s
More file actions
447 lines (407 loc) · 9.24 KB
/
conditional.s
File metadata and controls
447 lines (407 loc) · 9.24 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/**
* @brief
* Conditional compilation/execution
*
* Idea similar to http://lars.nocrew.org/dpans/dpansa15.htm#A.15.6.2.2532.
* In included files it works only on the same line:
* [IFNDEF] .( : .( ( -- ) [char] ) ['] ." $1E + call, parse string, immediate 0-foldable ; [THEN]
*
* Or use line continuation with a backslash at the EOL.
* Be aware: the max. length of the concatenated string is 255 chars.
* [IFNDEF] .( \
* : .( ( -- ) \
* [char] ) ['] ." $1E + call, parse string, \
* immediate 0-foldable ; \
* [THEN]
* @file
* conditional.s
* @author
* Peter Schmid, peter@spyr.ch
* @date
* 2025-12-20
* @remark
* Language: ARM Assembler, STM32CubeIDE GCC
* @copyright
* Peter Schmid, Switzerland
*
* This project Mecrsip-Cube is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* Mecrsip-Cube is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Mecrsip-Cube. If not, see http://www.gnu.org/licenses/.
*/
@ -----------------------------------------------------------------------------
Wortbirne Flag_visible, "tolower"
/*
: tolower ( C -- c ) \ only 7 bit ASCII
dup dup [char] A >= swap [char] Z <= and if
32 or
then
;
*/
tolower:
@ -----------------------------------------------------------------------------
cmp tos, #'A'
blo.n 1f
cmp tos, #'Z'
bhi.n 1f
orrs tos, #32
1:
bx lr
@ -----------------------------------------------------------------------------
Wortbirne Flag_visible, "toupper"
/*
: toupper ( c -- C ) \ only 7 bit ASCII
dup dup [char] a >= swap [char] z <= and if
32 not and
then
;
*/
toupper:
@ -----------------------------------------------------------------------------
cmp tos, #'a'
blo.n 1f
cmp tos, #'z'
bhi.n 1f
ands tos, #0x5f
1:
bx lr
@ -----------------------------------------------------------------------------
Wortbirne Flag_visible, "lower"
/*
: lower ( addr len -- )
over + swap
do
i c@ tolower i c!
loop
;
*/
lower:
@ -----------------------------------------------------------------------------
push {lr}
movs r0, tos // len
drop
movs r1, tos // addr
1:
cmp r0, #0
ble.n 2f
ldrb tos, [r1]
bl tolower // does not change r0 and r1
strb tos, [r1]
adds r1, #1
subs r0, #1
b.n 1b
2:
drop
pop {pc}
@ -----------------------------------------------------------------------------
Wortbirne Flag_visible, "upper"
/*
: upper ( addr len -- )
over + swap
do
i c@ toupper i c!
loop
;
*/
upper:
@ -----------------------------------------------------------------------------
push {lr}
movs r0, tos // len
drop
movs r1, tos // addr
1:
cmp r0, #0
ble.n 2f
ldrb tos, [r1]
bl toupper // does not change r0 and r1
strb tos, [r1]
adds r1, #1
subs r0, #1
b.n 1b
2:
drop
pop {pc}
@ -----------------------------------------------------------------------------
Wortbirne Flag_visible, "nexttoken"
/*
: nexttoken ( -- addr len )
begin
token \ fetch new token.
dup 0= while \ if length of token is zero, end of line is reached.
2drop cr query \ fetch new line, this will not work in evaluate
repeat
;
*/
nexttoken:
@ -----------------------------------------------------------------------------
push {lr}
1: bl token
cmp tos, #0
bne 2f
ddrop
bl cr
bl query
b.n 1b
2: pop {pc}
@ -----------------------------------------------------------------------------
Wortbirne Flag_visible, "[IF*]?"
/*
: [IF*]? ( addr len -- flag ) \ true if string is "[IF]", "[IFDEF]", or "[IFNDEF]"
2dup s" [IF]" compare
>r 2dup s" [IFDEF]" compare r> or
>r s" [IFNDEF]" compare r> or
;
*/
bracket_if_star:
@ -----------------------------------------------------------------------------
push {lr}
ddup
bl dotsfuesschen
.byte 8f - 7f // length of name field
7: .ascii "[IF]"
8: .p2align 1
bl compare
to_r
ddup
bl dotsfuesschen
.byte 8f - 7f // length of name field
7: .ascii "[IFDEF]"
8: .p2align 1
bl compare
r_from
ldm psp!, {r0}
orrs tos, r0
to_r
bl dotsfuesschen
.byte 8f - 7f // length of name field
7: .ascii "[IFNDEF]"
8: .p2align 1
bl compare
r_from
ldm psp!, {r0}
orrs tos, r0
pop {pc}
@ -----------------------------------------------------------------------------
Wortbirne Flag_visible, "[ENDIF*]?"
/*
: [ENDIF*]? ( addr len -- flag ) \ true if string is "[THEN]", or "[ENDIF]"
2dup s" [THEN]" compare
>r s" [ENDIF]" compare r> or
;
*/
bracket_endif_star:
@ -----------------------------------------------------------------------------
push {lr}
ddup
bl dotsfuesschen
.byte 8f - 7f // length of name field
7: .ascii "[THEN]"
8: .p2align 1
bl compare
to_r
bl dotsfuesschen
.byte 8f - 7f // length of name field
7: .ascii "[ENDIF]"
8: .p2align 1
bl compare
r_from
ldm psp!, {r0}
orrs tos, r0
pop {pc}
@ -----------------------------------------------------------------------------
Wortbirne Flag_immediate|Flag_foldable_0, "[ELSE]"
/*
: [ELSE] ( -- )
1 \ Initial level of nesting
begin
nexttoken ( level addr len )
2dup [IF*]? if
2drop 1+ \ One more level of nesting
else
2dup s" [ELSE]" compare if
2drop 1- dup if
1+ \ Finished if [ELSE] is reached in level 1. Skip [ELSE] branch otherwise.
then
else
\ 4:
[ENDIF*]? if
1- \ Level completed.
then
then
then
\ 3:
?dup 0=
until
immediate 0-foldable
;
*/
bracket_else:
@ -----------------------------------------------------------------------------
push {lr}
pushdaconst 1 // Initial level of nesting
1:
bl nexttoken
ddup
bl bracket_if_star
cmp tos, #-1
drop
bne 2f
ddrop
adds tos, #1 // One more level of nesting
b.n 3f
2:
ddup
bl dotsfuesschen
.byte 8f - 7f // length of name field
7: .ascii "[ELSE]"
8: .p2align 1
bl compare
cmp tos, #-1
drop
bne 4f
ddrop
subs tos, #1
beq 3f
adds tos, #1 // Finished if [ELSE] is reached in level 1. Skip [ELSE] branch otherwise.
b.n 3f
4:
bl bracket_endif_star
cmp tos, #-1
drop
bne 3f
subs tos, #1 // level completed
3:
cmp tos, #0
bne 1b
drop
pop {pc}
@ -----------------------------------------------------------------------------
Wortbirne Flag_immediate|Flag_foldable_0, "[THEN]"
/*
: [THEN] ( -- ) immediate 0-foldable ;
*/
bracket_then:
@ -----------------------------------------------------------------------------
bx lr
@ -----------------------------------------------------------------------------
Wortbirne Flag_immediate|Flag_foldable_0, "[ENDIF]"
/*
: [ENDIF] ( -- ) immediate 0-foldable ;
*/
bracket_endif:
@ -----------------------------------------------------------------------------
bx lr
@ -----------------------------------------------------------------------------
Wortbirne Flag_immediate|Flag_foldable_1, "[IF]"
/*
: [IF] ( flag -- )
0= if
postpone [ELSE]
then
immediate 1-foldable
;
*/
bracket_if:
@ -----------------------------------------------------------------------------
push {lr}
cmp tos, #0
drop
bne 1f
// bl postpone
bl bracket_else
1:
pop {pc}
@ -----------------------------------------------------------------------------
Wortbirne Flag_immediate|Flag_foldable_0, "[IFDEF]"
/*
: [IFDEF] ( "<spaces>name" -- )
token find drop
0= if
postpone [ELSE]
then
immediate 0-foldable
;
*/
bracket_ifdef:
@ -----------------------------------------------------------------------------
push {lr}
bl token
bl find
drop
cmp tos, #0
drop
bne 1f
// bl postpone
bl bracket_else
1:
pop {pc}
@ -----------------------------------------------------------------------------
Wortbirne Flag_immediate|Flag_foldable_0, "[IFNDEF]"
/*
: [IFNDEF] ( "<spaces>name" -- )
token find drop
0<> if
postpone [ELSE]
then
immediate 0-foldable
;
*/
bracket_ifndef:
@ -----------------------------------------------------------------------------
push {lr}
bl token
bl find
drop
cmp tos, #0
drop
beq 1f
// bl postpone
bl bracket_else
1:
pop {pc}
@ -----------------------------------------------------------------------------
Wortbirne Flag_immediate|Flag_foldable_0, "[UNDEFINED]"
/*
: [UNDEFINED] ( "<spaces>name" -- flag )
token find drop
0=
immediate
;
*/
bracket_undefined:
@ -----------------------------------------------------------------------------
push {lr}
bl token
bl find
drop
subs tos, #1 // 0=
sbcs tos, tos
pop {pc}
@ -----------------------------------------------------------------------------
Wortbirne Flag_immediate|Flag_foldable_0, "[DEFINED]"
/*
: [DEFINED] ( "<spaces>name" -- flag )
token find drop
0<>
immediate
;
*/
bracket_defined:
@ -----------------------------------------------------------------------------
push {lr}
bl token
bl find
drop
subs tos, #1 // 0<>
sbcs tos, tos
mvns tos, tos
pop {pc}
.ltorg