OSDN Git Service

38c253a958a6ef5fe5f23aa6b20ae7760707328c
[fast-forth/master.git] / forthMSP430FR_CONDCOMP.asm
1
2
3 ; https://forth-standard.org/standard/tools/BracketTHEN
4 ; [THEN]
5         FORTHWORDIMM "[THEN]"   ; do nothing
6         MOV @IP+,PC
7
8 ; ; https://forth-standard.org/standard/string/COMPARE
9 ; ; COMPARE ( c-addr1 u1 c-addr2 u2 -- n )
10 ; ;Compare the string specified by c-addr1 u1 to the string specified by c-addr2 u2. 
11 ; ;The strings are compared, beginning at the given addresses, character by character, 
12 ; ;up to the length of the shorter string or until a difference is found. 
13 ; ;If the two strings are identical, n is zero. 
14 ; ;If the two strings are identical up to the length of the shorter string, 
15 ; ;   n is minus-one (-1) if u1 is less than u2 and one (1) otherwise. 
16 ; ;If the two strings are not identical up to the length of the shorter string, 
17 ; ;   n is minus-one (-1) if the first non-matching character in the string specified by c-addr1 u1 
18 ; ;   has a lesser numeric value than the corresponding character in the string specified by c-addr2 u2 and one (1) otherwise.
19 ;         FORTHWORD "COMPARE"
20 ; COMPARE
21 ;         MOV TOS,S       ;1 S = u2
22 ;         MOV @PSP+,Y     ;2 Y = addr2
23 ;         MOV @PSP+,T     ;2 T = u1     
24 ;         MOV @PSP+,X     ;2 X = addr1
25 ; COMPAR1 MOV T,TOS       ;1
26 ;         ADD S,TOS       ;1 TOS = u1+u2
27 ;         JZ  COMPEQUAL   ;2 u1=u2=0, Z=1,  end of all successfull comparisons
28 ;         SUB #1,T        ;1
29 ;         JN COMPLESS     ;2 u1<u2 if u1 < 0
30 ;         SUB #1,S        ;1
31 ;         JN COMPGREATER  ;2 u1>u2 if u2 < 0
32 ;         ADD #1,X        ;1 
33 ;         CMP.B @Y+,-1(X) ;4 char1-char2
34 ;         JZ COMPAR1      ;2 char1=char2  17~ loop
35 ;         JC  COMPGREATER ;2 char1>char2
36 ; COMPLESS                ;  char1<char2
37 ;         MOV #-1,TOS     ;1 Z=0
38 ;         MOV @IP+,PC     ;4
39 ; COMPGREATER
40 ;         MOV #1,TOS      ;1 Z=0
41 ; COMPEQUAL
42 ;         MOV @IP+,PC     ;4     20 + 5 words def'n
43
44 ; ; https://forth-standard.org/standard/tools/BracketELSE
45 ; ; [ELSE]      a few (smaller and faster) definition
46 ; ;Compilation:
47 ; ;Perform the execution semantics given below.
48 ; ;Execution:
49 ; ;( "<spaces>name ..." -- )
50 ; ;Skipping leading spaces, parse and discard space-delimited words from the parse area, 
51 ; ;including nested occurrences of [IF] ... [THEN] and [IF] ... [ELSE] ... [THEN], 
52 ; ;until the word [THEN] has been parsed and discarded. 
53 ; ;If the parse area becomes exhausted, it is refilled as with REFILL. 
54 ;         FORTHWORDIMM  "[ELSE]"          ; or [IF] if isnogood...
55 ; BRACKETELSE
56 ;         mDOCOL
57 ;         .word   lit,0                   
58 ; BRACKETELSE0
59 ;         .word   ONEPLUS                 ; 
60 ; BRACKETELSE1                            ;
61 ;         .word   FBLANK,WORDD,COUNT      ; -- addr u
62 ;         .word   DUP,QFBRAN,BRACKETELSE5 ;       u = 0 if end of line --> refill buffer then loop back
63 ;         .word   TWODUP                  ;
64 ;         .word   XSQUOTE                 ;
65 ;         .byte   6,"[THEN]"              ;
66 ;         .word   COMPARE                 ;
67 ;         .word   QTBRAN,BRACKETELSE2     ; if bad comparaison, jump for next comparaison
68 ;         .word   TWODROP,ONEMINUS        ; 2DROP, decrement count
69 ;         .word   QDUP,QTBRAN,BRACKETELSE1; loop back if count <> 0
70 ;         .word   EXIT                    ; else exit
71 ; BRACKETELSE2                            ;
72 ;         .word   TWODUP                  ;
73 ;         .word   XSQUOTE                 ;
74 ;         .byte   6,"[ELSE]"              ;
75 ;         .word   COMPARE                 ;
76 ;         .word   QTBRAN,BRACKETELSE3     ; if bad comparaison, jump for next comparaison
77 ;         .word   TWODROP,ONEMINUS        ; 2DROP, decrement count
78 ;         .word   QDUP,QTBRAN,BRACKETELSE0; if count <> 0 restore old count with loop back increment
79 ;         .word   EXIT                    ; else exit
80 ; BRACKETELSE3                            ;
81 ;         .word   XSQUOTE                 ;
82 ;         .byte   4,"[IF]"                ;
83 ;         .word   COMPARE                 ;
84 ;         .word   QTBRAN,BRACKETELSE1     ; if bad comparaison, loop back
85 ;         .word   BRAN,BRACKETELSE0       ; else increment loop back
86 ; BRACKETELSE5                            ;
87 ;         .word   TWODROP                 ;
88 ; ;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^;
89 ; ; OPTION                                ; plus 5 words option
90 ; ;vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv;
91 ;         .word   XSQUOTE                 ;
92 ;         .byte   5,13,10,"ko "           ;
93 ;         .word   TYPE                    ; CR+LF ." ko "     to show false branch of conditionnal compilation
94 ; ;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^;
95 ;         .word   REFILL                  ; REFILL Input Buffer with next line
96 ;         .word   SETIB                   ; SET Input Buffer pointers SOURCE_LEN, SOURCE_ORG and clear >IN
97 ;         .word   BRAN,BRACKETELSE1       ; then loop back   54 words without options
98
99 BADCOMPBR               ; branch if string compare is false; [COMPARE,QTBRAN] replacement
100         MOV TOS,S       ;1 S = u2
101         MOV @PSP+,Y     ;2 Y = addr2
102         MOV @PSP+,T     ;2 T = u1     
103         MOV @PSP+,X     ;2 X = addr1
104 COMPAR1 MOV T,TOS       ;1
105         ADD S,TOS       ;1 TOS = u1+u2
106         JZ  COMPEQU     ;2 u1=u2=0, Z=1,  end of all successfull comparisons
107         SUB #1,T        ;1
108         JN COMPDIF      ;2 u1<u2 if u1 < 0
109         SUB #1,S        ;1
110         JN COMPDIF      ;2 u1>u2 if u2 < 0
111         ADD #1,X        ;1 
112         CMP.B @Y+,-1(X) ;4 char1-char2
113         JZ COMPAR1      ;2 char1=char2  17~ loop
114 COMPDIF MOV @IP,IP      ; take branch
115 CMPEND  MOV @PSP+,TOS
116         MOV @IP+,PC     ;4
117
118 TOQTB                   ; [TWODROP,ONEMINUS,?DUP,QTBRAN] replacement
119         ADD #2,PSP      ;1   -- savedTOS TOS
120         SUB #1,0(PSP)   ;3   -- savedTOS-1 TOS
121         JNZ COMPDIF     ;2   -- cnt     take branch
122         ADD #2,PSP      ;1   --
123 COMPEQU ADD #2,IP       ;               skip branch
124         JMP CMPEND      ; 25 words
125
126 ; https://forth-standard.org/standard/tools/BracketELSE
127 ; [ELSE]      a few (smaller and faster) definition
128 ;Compilation:
129 ;Perform the execution semantics given below.
130 ;Execution:
131 ;( "<spaces>name ..." -- )
132 ;Skipping leading spaces, parse and discard space-delimited words from the parse area, 
133 ;including nested occurrences of [IF] ... [THEN] and [IF] ... [ELSE] ... [THEN], 
134 ;until the word [THEN] has been parsed and discarded. 
135 ;If the parse area becomes exhausted, it is refilled as with REFILL. 
136         FORTHWORDIMM  "[ELSE]"          ; or [IF] if isnogood...
137 BRACKETELSE
138         mDOCOL
139         .word   lit,0                   
140 BRACKETELSE0
141         .word   ONEPLUS                 ; 
142 BRACKETELSE1                            ;
143         .word   FBLANK,WORDD,COUNT      ; -- addr u
144         .word   DUP,QFBRAN,BRACKETELSE5 ;       u = 0 if end of line --> refill buffer then loop back
145         .word   TWODUP                  ;
146         .word   XSQUOTE                 ;
147         .byte   6,"[THEN]"              ;
148         .word   BADCOMPBR,BRACKETELSE2  ; if bad string comparaison, jump for next comparaison
149         .word   TOQTB,BRACKETELSE1      ; 2DROP,  count-1, loop back if count <> 0, else DROP
150         .word   EXIT                    ; then exit
151 BRACKETELSE2                            ;
152         .word   TWODUP                  ;
153         .word   XSQUOTE                 ;
154         .byte   6,"[ELSE]"              ;
155         .word   BADCOMPBR,BRACKETELSE3  ; if bad string comparaison, jump for next comparaison
156         .word   TOQTB,BRACKETELSE0      ; 2DROP, count-1, loop back with count+1 if count <> 0, else DROP
157         .word   EXIT                    ; then exit
158 BRACKETELSE3                            ;
159         .word   XSQUOTE                 ;
160         .byte   4,"[IF]"                ;
161         .word   BADCOMPBR,BRACKETELSE1  ; if bad string comparaison, loop back
162         .word   BRAN,BRACKETELSE0       ; else loop back with count+1
163 BRACKETELSE5                            ;
164         .word   TWODROP                 ;
165 ;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^;
166 ; OPTION                                ; plus 5 words option
167 ;vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv;
168         .word   XSQUOTE                 ;
169         .byte   5,13,10,"ko "           ;
170         .word   TYPE                    ; CR+LF ." ko "     to show false branch of conditionnal compilation
171 ;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^;
172         .word   REFILL                  ; REFILL Input Buffer with next line
173         .word   SETIB                   ; SET Input Buffer pointers SOURCE_LEN, SOURCE_ORG and clear >IN
174         .word   BRAN,BRACKETELSE1       ; then loop back   44 words without options
175
176 ; https://forth-standard.org/standard/tools/BracketIF
177 ; [IF]
178 ;Compilation:
179 ;Perform the execution semantics given below.
180 ;Execution: ;( flag | flag "<spaces>name ..." -- )
181 ;If flag is true, do nothing. Otherwise, skipping leading spaces, 
182 ;   parse and discard space-delimited words from the parse area, 
183 ;   including nested occurrences of [IF] ... [THEN] and [IF] ... [ELSE] ... [THEN],
184 ;   until either the word [ELSE] or the word [THEN] has been parsed and discarded. 
185 ;If the parse area becomes exhausted, it is refilled as with REFILL. [IF] is an immediate word.
186 ;An ambiguous condition exists if [IF] is POSTPONEd, 
187 ;   or if the end of the input buffer is reached and cannot be refilled before the terminating [ELSE] or [THEN] is parsed.
188         FORTHWORDIMM "[IF]" ; flag -- 
189 BRACKETIF
190         CMP #0,TOS
191         MOV @PSP+,TOS
192         JZ BRACKETELSE
193         MOV @IP+,PC
194
195 ; https://forth-standard.org/standard/core/NIP
196 ; NIP      x1 x2 -- x2         Drop the first item below the top of stack
197     .IFNDEF NIP
198 NIP         ADD #2,PSP      ; 1
199             MOV @IP+,PC     ; 4
200     .ENDIF
201
202 ; https://forth-standard.org/standard/tools/BracketDEFINED
203 ; [DEFINED]
204 ;Compilation:
205 ;Perform the execution semantics given below.
206 ;Execution:
207 ;( "<spaces>name ..." -- flag )
208 ;Skip leading space delimiters. Parse name delimited by a space. 
209 ;Return a true flag if name is the name of a word that can be found,
210 ;otherwise return a false flag. [DEFINED] is an immediate word.
211
212         FORTHWORDIMM  "[DEFINED]"
213 DEFINED mDOCOL
214         .word   FBLANK,WORDD,FIND,NIP,EXIT
215
216 ; https://forth-standard.org/standard/tools/BracketUNDEFINED
217 ; [UNDEFINED]
218 ;Compilation:
219 ;Perform the execution semantics given below.
220 ;Execution: ( "<spaces>name ..." -- flag )
221 ;Skip leading space delimiters. Parse name delimited by a space. 
222 ;Return a false flag if name is the name of a word that can be found,
223 ;otherwise return a true flag.
224         FORTHWORDIMM  "[UNDEFINED]"
225         mDOCOL
226         .word   DEFINED
227         .word   $+2
228         MOV @RSP+,IP
229         MOV #ZEROEQUAL,PC