added WORDS
[rrq/rrqforth.git] / wordlists.asm
1 ;;; This file contains words dealing with word lists (vocabularies)
2
3         WORD p_wordlist,'WORDLIST',dovariable
4         ;; VARIABLE WORDLIST is the currently active wordlist.
5         dq last_word
6
7         WORD p_words,'WORDS',fasm
8         ;; ( -- )
9         ;; Dump all words
10         pushr rsi
11         push qword [p_wordlist_DFA]
12 p_words_LOOP:
13         mov rax,qword [rsp]
14         cmp rax,0
15         je p_words_END
16         ;; tfa>pfa
17         add rax,9
18         ;; pfa@ => ( chars* length)
19         xor rbx,rbx
20         mov bl,[rax]
21         inc rax
22         push 1
23         push rax
24         push rbx
25         DOFORTH sys_write
26         pop rax                 ; ignore errors
27         push 10
28         DOFORTH p_emit
29         mov rax,qword [rsp]
30         mov rax,qword [rax]
31         mov qword [rsp],rax
32         jmp p_words_LOOP
33
34 p_words_END:
35         popr rsi
36         next
37
38         WORD p_strncmp,'STRNCMP',fasm
39         ;; ( chars1 chars2 n -- flag )
40         ;; Compare bytes until one is NUL, return <0, =0 or >0 to
41         ;; indicate that chars1 is lesser, they are equal, or chars2
42         ;; is lesser in ascii ordering respectively.
43         pop rdx
44         pop rbx
45         pop rax
46         xor rcx,rcx
47         ;; rax = chars1, rbx = chars2, cl = byte acc, rdx = length
48         inc rdx
49 p_strncmp_loop:
50         dec rdx
51         je p_strncmp_end
52         mov cl,[rax]
53         inc rax
54         sub cl,[rbx]
55         inc rbx
56         je p_strncmp_loop
57 p_strncmp_end:
58         push rcx
59         next
60
61         WORD p_find,'FIND'
62         ;; ( chars length -- [ chars 0 | cfa 1 )
63         ;; Search the current wordlists for the given pname
64         mov rcx,[p_wordlist_DFA]
65 p_find_loop:
66         cmp rcx,0
67         je p_find_done
68         mov rbx,[rsp]
69         mov rax,[rsp+8]
70         cmp bl,byte [rcx+10]
71         jne p_find_nextword
72         push rcx
73         ;; check word
74         push rax
75         push rcx+11
76         push rbx
77         DOFORTH p_strncmp
78         pop rax                 ; return value
79         pop rcx
80         cmp rax,0
81         je p_find_done
82 p_find_nextword:
83         mov rcx,[rcx]
84         jmp p_find_loop
85 p_find_found:
86         mov qword [rsp+8],rcx   ; replace chars with tfa
87         mov rcx,1
88 p_find_done:
89         push rcx
90         next