forked from M2IHP13-admin/JonesForth-arm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jonesforth.S
1315 lines (1040 loc) · 33.8 KB
/
jonesforth.S
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@
@ Jones' Forth port for ARM EABI
@ Copyright (C) 2013 M2IHP'13 class, see AUTHORS for the full list of
@ contributors.
@
@ Original x86 and forth code: Richard W.M. Jones <[email protected]>
@
@ The extensive comments from Jones' x86 version have been removed. You should
@ check them out, they are really detailed, well written and pedagogical.
@
@ The DIVMOD routine is taken from the ARM Software Development Toolkit User
@ Guide 2.50.
@
@ This program is free software: you can redistribute it and/or modify it under
@ the terms of the GNU Lesser General Public License as published by the Free
@ Software Foundation, either version 3 of the License, or (at your option) any
@ later version.
@
@ This program 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 Lesser General Public License for more
@ details.
@
@ You should have received a copy of the GNU Lesser General Public License
@ along with this program. If not, see <http://www.gnu.org/licenses/>.
@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
.set JONES_VERSION,47
#include <asm/unistd.h>
@ Reserve three special registers:
@ DSP (r13) points to the top of the data stack
@ RSP (r11) points to the top of the return stack
@ IP (r10) points to the next forth word that will be executed
#define DSP r13
#define RSP r11
#define IP r10
@ Define stdin, stdout, stderr file descriptors numbers
.set stdin, 0
.set stdout, 1
.set stderr, 2
@ Implement NEXT, which:
@ 1. finds the address of the forth word to execute by
@ dereferencing the IP
@ 2. increment IP
@ 3. executes the forth word
.macro NEXT
ldr r0, [IP], #4
ldr r1, [r0]
bx r1
.endm
@ Define macros to push and pop from the data
@ and return stacks
.macro PUSHRSP reg
str \reg, [RSP, #-4]!
.endm
.macro POPRSP reg
ldr \reg, [RSP], #4
.endm
.macro push reg
str \reg, [DSP, #-4]!
.endm
.macro pop reg
ldr \reg, [DSP], #4
.endm
@ DOCOL is the assembly subroutine that is called
@ at the start of every forth word execution.
@ It saves the old IP on the return stack, and
@ makes IP point to the first codeword.
@ Then it calls NEXT to start interpreting the word.
.text
.align 2
DOCOL:
PUSHRSP IP
add IP, r0, #4
NEXT
@ _start is the program entry point
.text
.align 2
.global _start
_start:
ldr r0, =var_S0
str DSP, [r0] @ Save the original stack position in var_S0
ldr RSP, =return_stack_top @ Set the initial return stack position
bl set_up_data_segment @ Set up the data segment
ldr IP, =cold_start @ Make the IP point to cold_start
NEXT @ Start the interpreter
@ Allocate a data segment to define new words and data
@ structures
.set INITIAL_DATA_SEGMENT_SIZE,65536
.text
.align 2
set_up_data_segment:
mov r1, #0
mov r7, #__NR_brk
swi 0 @ Call brk(0) to get value of Program Break
ldr r1, =var_HERE
str r0, [r1] @ Initialize HERE to point at the beginning
@ of data segment
add r0, #INITIAL_DATA_SEGMENT_SIZE
swi 0 @ Allocate Memory
bx lr @ Return
@ cold_start is used to bootstrap the interpreter, the first word executed
@ is QUIT
.section .rodata
cold_start:
.int QUIT
@@ Now we define a set of helper macros that are syntactic sugar
@@ to ease the declaration of Forth words, Native words, Forth variables
@@ and Forth constants.
@ define the word flags
.set F_IMMED,0x80
.set F_HIDDEN,0x20
.set F_LENMASK,0x1f
@ link is used to chain the words in the dictionary as they are defined
.set link,0
@ defword macro helps defining new forth words in assembly
.macro defword name, namelen, flags=0, label
.section .rodata
.align 2
.global name_\label
name_\label :
.int link @ link
.set link,name_\label
.byte \flags+\namelen @ flags + length byte
.ascii "\name" @ the name
.align 2 @ padding to next 4 byte boundary
.global \label
\label :
.int DOCOL @ codeword - the interpreter
@ list of word pointers follow
.endm
@ defcode macro helps defining new native words in assembly
.macro defcode name, namelen, flags=0, label
.section .rodata
.align 2
.globl name_\label
name_\label :
.int link @ link
.set link,name_\label
.byte \flags+\namelen @ flags + length byte
.ascii "\name" @ the name
.align 2 @ padding to next 4 byte boundary
.global \label
\label :
.int code_\label @ codeword
.text
.global code_\label
code_\label : @ assembler code follows
.endm
@ defvar macro helps defining Forth variables in assembly
.macro defvar name, namelen, flags=0, label, initial=0
defcode \name,\namelen,\flags,\label
ldr r0, =var_\name
push r0
NEXT
.data
.align 2
var_\name :
.int \initial
.endm
@defconst macro helps defining Forth constants in assembly
.macro defconst name, namelen, flags=0, label, value
defcode \name,\namelen,\flags,\label
ldr r0, =\value
push r0
NEXT
.endm
@ EXIT is the last codeword of a forth word.
@ It restores the IP and returns to the caller using NEXT.
@ (See DOCOL)
defcode "EXIT",4,,EXIT
POPRSP IP
NEXT
@ DIVMOD computes the unsigned integer division and remainder
@ The implementation is based upon the algorithm extracted from 'ARM Software
@ Development Toolkit User Guide v2.50' published by ARM in 1997-1998
@ The algorithm is split in two steps: search the biggest divisor b^(2^n)
@ lesser than a and then subtract it and all b^(2^i) (for i from 0 to n)
@ to a.
@ ( a b -- r q ) where a = q * b + r
defcode "/MOD",4,,DIVMOD
pop r1 @ Get b
pop r0 @ Get a
mov r3, r1 @ Put b in tmp
cmp r3, r0, LSR #1
1: movls r3, r3, LSL #1 @ Double tmp
cmp r3, r0, LSR #1
bls 1b @ Jump until 2 * tmp > a
mov r2, #0 @ Initialize q
2: cmp r0, r3 @ If a - tmp > 0
subcs r0, r0, r3 @ a <= a - tmp
adc r2, r2, r2 @ Increment q
mov r3, r3, LSR #1 @ Halve tmp
cmp r3, r1 @ Jump until tmp < b
bhs 2b
push r0 @ Put r
push r2 @ Put q
NEXT
@ Alternative to DIVMOD: signed implementation using Euclidean division.
defcode "S/MOD",5,,SDIVMOD
@ Denominator
pop r2
@ Numerator
pop r1
bl _DIVMOD
@ Remainder
push r1
@ Quotient
push r0
NEXT
_DIVMOD:
@ Division by 0.
cmp r2, #0
beq 4f
@ r0 will store the quotient at the end.
mov r0, #0
@ r3 will be 1 if numerator and denominator have the same
@ sign, -1 otherwise.
@ r4 will be 1 if the numerator is positive, -1 otherwise.
mov r3, #1
mov r4, #1
rsblt r3, r3, #0 @ r3 = -r3 if negative denominator
rsblt r2, r2, #0 @ denominator = abs(denominator)
cmp r1, #0
rsblt r4, r4, #0 @ r4 = sign(numerator)
rsblt r3, r3, #0 @ r3 = -r3 if negative numerator
rsblt r1, r1, #0 @ numerator = abs(numerator)
cmp r3, #-1
beq 2f
1: @ Case where denominator and numerator have the same sign.
cmp r1, r2
blt 3f
11:
add r0, r0, #1
sub r1, r1, r2
cmp r1, r2
bge 11b
b 3f
2: @ Case where denominator and numerator have different sign.
cmp r1, #0
beq 3f
21:
sub r0, r0, #1
sub r1, r1, r2
cmp r1, #0
bgt 21b
3:
@ If numerator and denominator were negative:
@ remainder = -remainder
cmp r4, #-1
rsbeq r1, r1, #0
b 5f
4: @ Error, division by 0.
# Display error message on stderr.
mov r0, #stderr
ldr r1, =div0msg
mov r2, #div0msgend-div0msg
mov r7, #__NR_write
swi 0
5:
bx lr
.section .rodata
div0msg: .ascii "Division by 0!\n"
div0msgend:
@ DROP ( a -- ) drops the top element of the stack
defcode "DROP",4,,DROP
pop r0 @( )
NEXT
@ SWAP ( a b -- b a ) swaps the two top elements
defcode "SWAP",4,,SWAP
@ ( a b -- )
pop r0 @ ( a ) , r0 = b
pop r1 @ ( ) , r0 = b, r1 = a
push r0 @ ( b ) , r0 = b, r1 = a
push r1 @ ( b a ) , r0 = b, r1 = a
NEXT
@ DUP ( a -- a a ) duplicates the top element
defcode "DUP",3,,DUP
@ ( a -- )
pop r0 @ ( ) , r0 = a
push r0 @ ( a ) , r0 = a
push r0 @ ( a a ) , r0 = a
NEXT
@ OVER ( a b c -- a b c b ) pushes the second element on top
defcode "OVER",4,,OVER
@ ( a b c) r0 = b we take the element at DSP + 4
@ and since DSP is the top of the stack we will load
@ the second element of the stack in r0
ldr r0, [DSP, #4]
push r0 @ ( a b c b )
NEXT
@ ROT ( a b c -- b c a) rotation
defcode "ROT",3,,ROT
pop r0 @ ( a b ) r0 = c
pop r1 @ ( a ) r1 = b
pop r2 @ ( ) r2 = a
push r1 @ ( b )
push r0 @ ( b c )
push r2 @ ( b c a )
NEXT
@ -ROT ( a b c -- c a b ) backwards rotation
defcode "-ROT",4,,NROT
pop r0 @ ( a b ) r0 = c
pop r1 @ ( a ) r1 = b
pop r2 @ ( ) r2 = a
push r0 @ ( c )
push r2 @ ( c a )
push r1 @ ( c a b )
NEXT
@ ?DUP ( 0 -- 0 | a -- a a ) duplicates if non-zero
defcode "?DUP", 4,,QDUP
@ (x --)
ldr r0, [DSP] @ r0 = x
cmp r0, #0 @ test if x==0
beq 1f @ if x==0 we jump to 1
push r0 @ ( a a ) it's now duplicated
1: NEXT @ ( a a / 0 )
@ 1+ ( a | a+1 ) increments the top element
defcode "1+",2,,INCR
pop r0
add r0,r0,#1
push r0
NEXT
@ 1- ( a | a-1 ) decrements the top element
defcode "1-",2,,DECR
pop r0
sub r0,r0,#1
push r0
NEXT
@ 4+ ( a | a+4 ) increments by 4 the top element
defcode "4+",2,,INCR4
pop r0
add r0,r0,#4
push r0
NEXT
@ 4- ( a | a-4 ) decrements by 4 the top element
defcode "4-",2,,DECR4
pop r0
sub r0,r0,#4
push r0
NEXT
@ + ( a b | a+b)
defcode "+",1,,ADD
pop r0
pop r1
add r0,r0,r1
push r0
NEXT
@ + ( a b | a-b)
defcode "-",1,,SUB
pop r1
pop r0
sub r0,r0,r1
push r0
NEXT
@ + ( a b | a*b)
defcode "*",1,,MUL
pop r0
pop r1
mul r2,r0,r1
push r2
NEXT
@ = ( a b | p ) where p is 1 when a and b are equal (0 otherwise)
defcode "=",1,,EQU
pop r1
pop r0
cmp r0, r1
moveq r0, #1
movne r0, #0
push r0
NEXT
@ <> ( a b | p) where p = a <> b
defcode "<>",2,,NEQU
pop r1
pop r0
cmp r0, r1
movne r0, #1
moveq r0, #0
push r0
NEXT
@ < ( a b | p) where p = a < b
defcode "<",1,,LT
pop r1
pop r0
cmp r0, r1
movlt r0, #1
movge r0, #0
push r0
NEXT
@ < ( a b | p) where p = a < b
defcode ">",1,,GT
pop r1
pop r0
cmp r0, r1
movgt r0, #1
movle r0, #0
push r0
NEXT
@ <= ( a b | p) where p = a <= b
defcode "<=",2,,LE
pop r1
pop r0
cmp r0, r1
movle r0, #1
movgt r0, #0
push r0
NEXT
@ >= ( a b | p) where p = a >= b
defcode ">=",2,,GE
pop r1
pop r0
cmp r0, r1
movge r0, #1
movlt r0, #0
push r0
NEXT
@ AND ( a b | a&b) bitwise and
defcode "AND",3,,AND
pop r0
pop r1
and r0, r1, r0
push r0
NEXT
@ OR ( a b | a|b) bitwise or
defcode "OR",2,,OR
pop r0
pop r1
orr r0, r1, r0
push r0
NEXT
@ XOR ( a b | a^b) bitwise xor
defcode "XOR",3,,XOR
pop r0
pop r1
eor r0, r1, r0
push r0
NEXT
@ INVERT ( a | ~a ) bitwise not
defcode "INVERT",6,,INVERT
pop r0
mvn r0, r0
push r0
NEXT
@ LIT is used to compile literals in forth word.
@ When LIT is executed it pushes the literal (which is the next codeword)
@ into the stack and skips it (since the literal is not executable).
defcode "LIT", 3,, LIT
ldr r1, [IP], #4
push r1
NEXT
@ ! ( value address -- ) write value at address
defcode "!",1,,STORE
pop r0
pop r1
str r1, [r0]
NEXT
@ @ ( address -- value ) reads value from address
defcode "@",1,,FETCH
pop r1
ldr r0, [r1]
push r0
NEXT
@ C! and @! are the same for bytes
defcode "C!",2,,STOREBYTE
pop r0
pop r1
strb r1, [r0]
NEXT
defcode "C@",2,,FETCHBYTE
pop r0
mov r1, #0
ldrb r1, [r0]
push r1
NEXT
@ CMOVE ( source dest length -- ) copies a chunk of length bytes from source
@ address to dest address
defcode "CMOVE",5,,CMOVE
pop r0
pop r1
pop r2
1:
cmp r0, #0 @ while length > 0
ldrgtb r3, [r2], #1 @ read character from source
strgtb r3, [r1], #1 @ and write it to dest (increment both pointers)
subgt r0, r0, #1 @ decrement length
bgt 1b
NEXT
@ Define some variables and constants needed by the Forth interpreter
defvar "STATE",5,,STATE
defvar "HERE",4,,HERE
defvar "LATEST",6,,LATEST,name_SYSCALL0 @ must point to the last word
@ defined in assembly, SYSCALL0
defvar "S0",2,,SZ
defvar "BASE",4,,BASE,10
defconst "VERSION",7,,VERSION,JONES_VERSION
defconst "R0",2,,RZ,return_stack_top
defconst "DOCOL",5,,__DOCOL,DOCOL
defconst "F_IMMED",7,,__F_IMMED,F_IMMED
defconst "F_HIDDEN",8,,__F_HIDDEN,F_HIDDEN
defconst "F_LENMASK",9,,__F_LENMASK,F_LENMASK
defconst "SYS_EXIT",8,,SYS_EXIT,__NR_exit
defconst "SYS_OPEN",8,,SYS_OPEN,__NR_open
defconst "SYS_CLOSE",9,,SYS_CLOSE,__NR_close
defconst "SYS_READ",8,,SYS_READ,__NR_read
defconst "SYS_WRITE",9,,SYS_WRITE,__NR_write
defconst "SYS_CREAT",9,,SYS_CREAT,__NR_creat
defconst "SYS_BRK",7,,SYS_BRK,__NR_brk
defconst "O_RDONLY",8,,__O_RDONLY,0
defconst "O_WRONLY",8,,__O_WRONLY,1
defconst "O_RDWR",6,,__O_RDWR,2
defconst "O_CREAT",7,,__O_CREAT,0100
defconst "O_EXCL",6,,__O_EXCL,0200
defconst "O_TRUNC",7,,__O_TRUNC,01000
defconst "O_APPEND",8,,__O_APPEND,02000
defconst "O_NONBLOCK",10,,__O_NONBLOCK,04000
@ >R ( a -- ) move the top element from the data stack to the return stack
defcode ">R",2,,TOR
pop r0
PUSHRSP r0
NEXT
@ R> ( -- a ) move the top element from the return stack to the data stack
defcode "R>",2,,FROMR
POPRSP r0
push r0
NEXT
@ RDROP drops the top element from the return stack
defcode "RDROP",5,,RDROP
add RSP,RSP,#4
NEXT
@ RSP@, RSP!, DSP@, DSP! manipulate the return and data stack pointers
defcode "RSP@",4,,RSPFETCH
push RSP
NEXT
defcode "RSP!",4,,RSPSTORE
pop RSP
NEXT
defcode "DSP@",4,,DSPFETCH
mov r0, DSP
push r0
NEXT
defcode "DSP!",4,,DSPSTORE
pop r0
mov r0, DSP
NEXT
@ KEY ( -- c ) Reads a key from the user
@ the implementation uses a cached buffer that is
@ refilled, when empty, with a read syscall.
defcode "KEY",3,,KEY
bl _KEY @ Call _KEY
push r0 @ push the return value on the stack
NEXT
_KEY:
ldr r3, =currkey @ Load the address of currkey
ldr r1, [r3] @ Get the value of currkey
ldr r3, =bufftop @ Load the address of bufftop
ldr r2, [r3] @ Get the value of bufftop
cmp r2, r1
ble 1f @ if bufftop <= currkey
ldrb r0, [r1] @ load the first byte of currkey
ldr r3, =currkey
add r1, #1 @ Increments CURRKEY
str r1, [r3]
bx lr @ return
1:
ldr r3, =currkey
mov r0, #0 @ 1st arg: STDIN
ldr r1, =buffer @ 2nd arg : buffer add
str r1, [r3] @ CURRKEY := BUFFER
mov r2, #BUFFER_SIZE @ 3rd arg : buffer sz
mov r7, #__NR_read @ read syscall flag
swi 0 @ call
cmp r0, #0
ble 2f @ if errors goto 2
add r1,r0 @ Set bufftop at the end of the word
ldr r4, =bufftop
str r1, [r4] @ update bufftop
b _KEY
2: @ read syscall returned with an error
mov r0, #0
mov r7, #__NR_exit @ exit(0)
swi 0
@ buffer for KEY
.data
.align 2
currkey:
.int buffer
bufftop:
.int buffer
@ EMIT ( c -- ) outputs character c to stdout
defcode "EMIT",4,,EMIT
pop r0
bl _EMIT
NEXT
_EMIT:
ldr r2, =emit_scratch
str r0, [r2] @ write character to memory
mov r1, r2
mov r2, #1 @ write 1 byte
mov r0, #stdout @ write on standard output
mov r7, #__NR_write @ write syscall flag
swi 0 @ write syscall
bx lr
.data
emit_scratch:
.space 1
@ WORD ( -- addr length ) reads next word from stdin
@ skips spaces and comments, limited to 32 characters
defcode "WORD",4,,WORD
bl _WORD
push r0 @ adress
push r1 @ length
NEXT
_WORD:
stmfd sp!, {r6,lr} @ preserve r6 and lr
1:
bl _KEY @ read a character
cmp r0, #'\\'
beq 3f @ skip comments until end of line
cmp r0, #' '
ble 1b @ skip blank character
ldr r6, =word_buffer
2:
strb r0, [r6], #1 @ store character in word buffer
bl _KEY @ read more characters until a space is found
cmp r0, #' '
bgt 2b
ldr r0, =word_buffer @ r0, address of word
sub r1, r6, r0 @ r1, length of word
ldmfd sp!, {r6,lr} @ restore r6 and lr
bx lr
3:
bl _KEY @ skip all characters until end of line
cmp r0, #'\n'
bne 3b
b 1b
@ word_buffer for WORD
.data
word_buffer:
.space 32
@ NUMBER ( addr length -- n e ) converts string to number
@ n is the parsed number
@ e is the number of unparsed characters
defcode "NUMBER",6,,NUMBER
pop r1
pop r0
bl _NUMBER
push r0
push r1
NEXT
_NUMBER:
stmfd sp!, {r4-r6, lr}
@ Save address of the string.
mov r2, r0
@ r0 will store the result after conversion.
mov r0, #0
@ Check if length is positive, otherwise this is an error.
cmp r1, #0
ble 5f
@ Load current base.
ldr r3, =var_BASE
ldr r3, [r3]
@ Load first character and increment pointer.
ldrb r4, [r2], #1
@ Check trailing '-'.
mov r5, #0
cmp r4, #45 @ 45 in '-' en ASCII
@ Number is positive.
bne 2f
@ Number is negative.
mov r5, #1
sub r1, r1, #1
@ Check if we have more than just '-' in the string.
cmp r1, #0
@ No, proceed with conversion.
bgt 1f
@ Error.
mov r1, #1
b 5f
1:
@ number *= BASE
@ Arithmetic shift right.
@ On ARM we need to use an additional register for MUL.
mul r6, r0, r3
mov r0, r6
@ Load the next character.
ldrb r4, [r2], #1
2:
@ Convert the character into a digit.
sub r4, r4, #48 @ r4 = r4 - '0'
cmp r4, #0
blt 4f @ End, < 0
cmp r4, #9
ble 3f @ chiffre compris entre 0 et 9
@ Test if hexadecimal character.
sub r4, r4, #17 @ 17 = 'A' - '0'
cmp r4, #0
blt 4f @ End, < 'A'
add r4, r4, #10
3:
@ Compare to the current base.
cmp r4, r3
bge 4f @ End, > BASE
@ Everything is fine.
@ Add the digit to the result.
add r0, r0, r4
sub r1, r1, #1
@ Continue processing while there are still characters to read.
cmp r1, #0
bgt 1b
4:
@ Negate result if we had a '-'.
cmp r5, #1
rsbeq r0, r0, #0
5:
@ Back to the caller.
ldmfd sp!, {r4-r6, pc}
@ FIND ( addr length -- dictionary_address )
@ Tries to find a word in the dictionary and returns its address.
@ If the word is not found, NULL is returned.
defcode "FIND",4,,FIND
pop r1 @length
pop r0 @addr
bl _FIND
push r0
NEXT
_FIND:
stmfd sp!, {r5,r6,r8,r9} @ save callee save registers
ldr r2, =var_LATEST
ldr r3, [r2] @ get the last defined word address
1:
cmp r3, #0 @ did we check all the words ?
beq 4f @ then exit
ldrb r2, [r3, #4] @ read the length field
and r2, r2, #(F_HIDDEN|F_LENMASK) @ keep only length + hidden bits
cmp r2, r1 @ do the lengths match ?
@ (note that if a word is hidden,
@ the test will be always negative)
bne 3f @ branch if they do not match
@ Now we compare strings characters
mov r5, r0 @ r5 contains searched string
mov r6, r3 @ r6 contains dict string
add r6, r6, #5 @ (we skip link and length fields)
@ r2 contains the length
2:
ldrb r8, [r5], #1 @ compare character per character
ldrb r9, [r6], #1
cmp r8,r9
bne 3f @ if they do not match, branch to 3
subs r2,r2,#1 @ decrement length
bne 2b @ loop
@ here, strings are equal
b 4f @ branch to 4
3:
ldr r3, [r3] @ Mismatch, follow link to the next
b 1b @ dictionary word
4:
mov r0, r3 @ move result to r0
ldmfd sp!, {r5,r6,r8,r9} @ restore callee save registers
bx lr
@ >CFA ( dictionary_address -- executable_address )
@ Transformat a dictionary address into a code field address
defcode ">CFA",4,,TCFA
pop r0
bl _TCFA
push r0
NEXT
_TCFA:
add r0,r0,#4 @ skip link field
ldrb r1, [r0], #1 @ load and skip the length field
and r1,r1,#F_LENMASK @ keep only the length
add r0,r0,r1 @ skip the name field
add r0,r0,#3 @ find the next 4-byte boundary
and r0,r0,#~3
bx lr
@ >DFA ( dictionary_address -- data_field_address )
@ Return the address of the first data field
defword ">DFA",4,,TDFA
.int TCFA
.int INCR4
.int EXIT
@ CREATE ( address length -- ) Creates a new dictionary entry
@ in the data segment.
@ CREATE ( address length -- ) Creates a new dictionary entry
@ in the data segment.
defcode "CREATE",6,,CREATE
pop r1 @ length of the word to insert into the dictionnary
pop r0 @ address of the word to insert into the dictionnary
ldr r2,=var_HERE
ldr r3,[r2] @ load into r3 and r8 the location of the header
mov r8,r3
ldr r4,=var_LATEST
ldr r5,[r4] @ load into r5 the link pointer
str r5,[r3] @ store link here -> last
add r3,r3,#4 @ skip link adress
strb r1,[r3] @ store the length of the word