swissChili | f7f1e2b | 2021-12-31 14:42:43 -0800 | [diff] [blame] | 1 | DOS FORTH WORD REFERENCE |
| 2 | |
| 3 | |
| 4 | CONTENTS |
| 5 | |
| 6 | 1. [STACK MANIPULATION] |
| 7 | 2. [ARITHMETIC] |
| 8 | 3. [DICTIONARY/MEMORY ACCESS] |
| 9 | 4. [STRING MANIPULATION & INPUT] |
| 10 | 5. [CONTROL FLOW & EXECUTION] |
| 11 | 6. [VARIABLES] |
| 12 | |
| 13 | |
| 14 | |
| 15 | STACK MANIPULATION |
| 16 | |
| 17 | DUP ( A -- A A ) |
| 18 | |
| 19 | 2DUP ( A B -- A B A B ) |
| 20 | |
| 21 | DROP ( A -- ) |
| 22 | |
| 23 | SWAP ( A B -- B A ) |
| 24 | |
| 25 | |
| 26 | |
| 27 | ARITHMETIC |
| 28 | |
| 29 | + ( A B -- A+B ) |
| 30 | |
| 31 | - (A B -- A-B ) |
| 32 | |
| 33 | +1 (A -- A+1 ) |
| 34 | |
| 35 | +2 (A -- A+2 ) |
| 36 | |
| 37 | |
| 38 | |
| 39 | DICTIONARY/MEMORY ACCESS |
| 40 | |
| 41 | FIND ( ADDRESS LENGTH -- ENTRY ) |
| 42 | |
| 43 | Search through the dictionary for a word with the given |
| 44 | name. Address and name together make up a string. |
| 45 | |
| 46 | ENTRY = address of the matching dictionary entry if one was found, |
| 47 | 0 otherwise. |
| 48 | |
| 49 | |
| 50 | >CFA ( BASE -- ADDRESS ) |
| 51 | |
| 52 | ADDRESS = the address of the codeword for the dictionary entry |
| 53 | starting at BASE. |
| 54 | |
| 55 | SEE ALSO <README.TXT> Fig. 1: Dictionary entry |
| 56 | |
| 57 | |
| 58 | >DFA ( BASE -- ADDRESS ) |
| 59 | |
| 60 | ADDRESS = the address of the first byte in the body of the |
| 61 | dictionary entry, i.e. the first byte after the codeword. |
| 62 | |
| 63 | SEE ALSO >CFA, <README.TXT> Fig. 1: Dictionary entry |
| 64 | |
| 65 | |
| 66 | @ ( ADDRESS -- VALUE ) |
| 67 | |
| 68 | VALUE = the cell at ADDRESS. |
| 69 | |
| 70 | |
| 71 | ! ( ADDRESS VALUE -- ) |
| 72 | |
| 73 | Sets the cell at ADDRESS to VALUE. |
| 74 | |
| 75 | |
| 76 | C@ ( ADDRESS -- CHAR ) |
| 77 | |
| 78 | CHAR = the byte at ADDRESS. |
| 79 | |
| 80 | |
| 81 | CREATE ( ADDR LENGTH -- ) |
| 82 | |
| 83 | Creates a new dictionary entry with the name specified by ADDR and |
| 84 | LENGTH. HERE and LATEST are updated accordingly. |
| 85 | |
| 86 | |
| 87 | , ( VALUE -- ) |
| 88 | |
| 89 | Stores the cell VALUE at HERE, adds 2 to HERE (word size). |
| 90 | |
| 91 | This does the same thing as |
| 92 | VALUE HERE @ ! HERE 2+ HERE ! |
| 93 | |
| 94 | |
| 95 | [ ( -- ) |
| 96 | |
| 97 | Switches to interpret mode. I.e. sets STATE to 0. |
| 98 | |
| 99 | |
| 100 | ] ( -- ) |
| 101 | |
| 102 | Switches to compile mode. I.e. sets STATE to 1. |
| 103 | |
| 104 | |
| 105 | IMMEDIATE ( -- ) |
| 106 | |
| 107 | Toggles if the most recently defined word (the dictionary entry |
| 108 | pointed to by LATEST) is immediate. |
| 109 | |
| 110 | |
| 111 | HIDDEN ( ENTRY -- ) |
| 112 | |
| 113 | Toggles if the dictionary entry starting at ENTRY is hidden. |
| 114 | |
| 115 | |
| 116 | HIDE WORD ( -- ) |
| 117 | |
| 118 | Looks up WORD and toggles if it is hidden. |
| 119 | |
| 120 | This could be defined as |
| 121 | : HIDE WORD FIND HIDDEN ; |
| 122 | |
| 123 | |
| 124 | ' WORD ( -- CFA ) |
| 125 | |
| 126 | Looks up WORD in the dictionary and returns its CFA. ' reads WORD |
| 127 | at runtime. |
| 128 | |
| 129 | |
| 130 | STRING MANIPULATION & INPUT |
| 131 | |
| 132 | KEY ( -- CHAR ) |
| 133 | |
| 134 | Reads one character from the input. KEY blocks until a key press |
| 135 | if no input data is buffered. |
| 136 | |
| 137 | CHAR = the character read. |
| 138 | |
| 139 | |
| 140 | EMIT ( CHAR -- ) |
| 141 | |
| 142 | Writes the lower byte of CHAR as an ASCII character to standard |
| 143 | output. |
| 144 | |
| 145 | |
| 146 | CR ( -- ) |
| 147 | |
| 148 | Write a carriage return followed by a line feed ('\r\n') to |
| 149 | standard output. |
| 150 | |
| 151 | |
| 152 | TYPE ( ADDR LENGTH -- ) |
| 153 | |
| 154 | Writes the string starting at ADDR with length LENGTH to standard |
| 155 | output. The string does not need to be terminated with anything |
| 156 | and may contain any byte values. |
| 157 | |
| 158 | |
| 159 | WORD ( -- ADDR LENGTH ) |
| 160 | |
| 161 | Reads a word from the input. Calls KEY internally. |
| 162 | |
| 163 | ADDR = the address of the internal buffer holding the word. This |
| 164 | is overridden every time WORD is called, so copy the string if you |
| 165 | need it. |
| 166 | |
| 167 | LENGTH = the length in bytes of the parsed word. The buffer holds |
| 168 | at most 32 bytes. |
| 169 | |
| 170 | |
| 171 | NUMBER ( ADDR LENGTH -- NUMBER UNPARSED ) |
| 172 | |
| 173 | Parses the string specified by ADDR and LENGTH as a base-10 |
| 174 | integer. |
| 175 | |
| 176 | NUMBER = the parsed number, or 0 if one could not be parsed. |
| 177 | |
| 178 | UNPARSED = the number of bytes in the string that could not be |
| 179 | parsed. I.e. 0 if everything parsed successfully, LENGTH if |
| 180 | nothing could be parsed. |
| 181 | |
| 182 | |
| 183 | . ( NUMBER -- ) |
| 184 | |
| 185 | Write NUMBER as a base-10 integer to standard output. |
| 186 | |
| 187 | For example: 123 . |
| 188 | 123 ok |
| 189 | |
| 190 | |
| 191 | LITSTRING LENGTH BYTES... ( -- ADDR LENGTH ) |
| 192 | |
| 193 | Compiles to a literal string. You should rarely have to use this |
| 194 | manually. |
| 195 | |
| 196 | LENGTH = the length of the string in bytes. 1 cell wide. |
| 197 | |
| 198 | BYTES = the actual string, padded at the end to 2 bytes (1 cell). |
| 199 | |
| 200 | ADDR = the address of the string. |
| 201 | |
| 202 | LENGTH = the length of the string. |
| 203 | |
| 204 | |
| 205 | |
| 206 | CONTROL FLOW & EXECUTION |
| 207 | |
| 208 | BRANCH BYTES ( -- ) |
| 209 | |
| 210 | Branches BYTES bytes forwards or backwards, depending on the |
| 211 | sign. BRANCH 0 does nothing, BRANCH 2 skips the following word, |
| 212 | BRANCH -4 causes an infinite loop. |
| 213 | |
| 214 | |
| 215 | 0BRANCH BYTES ( PREDICATE -- ) |
| 216 | |
| 217 | Identical to BRANCH if PREDICATE is 0. Otherwise 0BRANCH does |
| 218 | nothing. |
| 219 | |
| 220 | |
| 221 | EXECUTE ( CFA -- ) |
| 222 | |
| 223 | Transfers execution to the word defined by CFA. This can be used |
| 224 | with '. |
| 225 | |
| 226 | SEE ALSO ' |
| 227 | |
| 228 | For example, ' DUP EXECUTE is identical to DUP |
| 229 | |
| 230 | |
| 231 | BYE ( -- ) |
| 232 | |
| 233 | Quit FORTH. |
| 234 | |
| 235 | |
| 236 | |
| 237 | VARIABLES |
| 238 | |
| 239 | STATE |
| 240 | |
| 241 | The current interpreter state. |
| 242 | |
| 243 | 0 = Interpret |
| 244 | 1 = Compile |
| 245 | |
| 246 | |
| 247 | HERE |
| 248 | |
| 249 | The address of the first free byte of the dictionary. This is |
| 250 | where newly compiled words are added. |
| 251 | |
| 252 | |
| 253 | LATEST |
| 254 | |
| 255 | The address of the dictionary for the most recently added word. |
| 256 | |
| 257 | SEE ALSO <README.TXT> Fig. 1: Dictionary entry |