3 ;; At compile-time we load the module given by the environment variable
4 ;; OS_INCLUDE. This module should define the following macros:
6 ;; Each of these functions should preserve the value of RSI and RSP. They may
7 ;; use other registers as they like.
10 ;; Called at initialization.
13 ;; Takes a string buffer in RCX and the length in RDX, and prints the string
17 ;; Wait for the user to type a key, and then put the corresponding ASCII byte
21 ;; Shut down the system, returning the error code given in RAX.
22 include '%OS_INCLUDE%'
24 ;; The code in this macro is placed at the end of each Forth word. When we are
25 ;; executing a definition, this code is what causes execution to resume at the
26 ;; next word in that definition.
28 ;; RSI points to the address of the definition of the next word to execute.
29 lodsq ; Load value at RSI into RAX and increment RSI
30 ;; Now RAX contains the location of the next word to execute. The first 8
31 ;; bytes of this word is the address of the codeword, which is what we want
33 jmp qword [rax] ; Jump to the codeword of the current word
36 ;; pushr and popr work on the return stack, whose location is stored in the
47 ;; The following macro generates the dictionary header. It updates the
48 ;; initial_latest_entry variable, which is used as the initial value of the
49 ;; latest_entry variable that is made available at runtime.
51 ;; The header contains a link to the previous entry, the length of the name of
52 ;; the word and the word itself as a string literal.
54 ;; This macro also defines a label LABEL_entry.
55 initial_latest_entry = 0
56 macro header label, name, immediate {
60 dq initial_latest_entry
66 db .string_end - ($ + 1)
71 initial_latest_entry = label#_entry
74 ;; Define a Forth word that is implemented in assembly. See 'header' for details.
75 macro forth_asm label, name, immediate {
76 header label, name, immediate
81 section '.text' code readable executable
83 include "impl.asm" ; Misc. subroutines
84 include "bootstrap.asm" ; Forth words encoded in Assembly
87 cld ; Clear direction flag so LODSQ does the right thing.
88 mov rbp, return_stack_top ; Initialize return stack
97 ;; The codeword is the code that will be executed at the beginning of a forth
98 ;; word. It needs to save the old RSI and update it to point to the next word to
100 header DOCOL, 'DOCOL'
101 pushr rsi ; Save old value of RSI on return stack; we will continue execution there after we are done executing this word
102 lea rsi, [rax + 8] ; RAX currently points to the address of the codeword, so we want to continue at RAX+8
103 next ; Execute word pointed to by RSI
105 ;; This word is called at the end of a Forth definition. It just needs to
106 ;; restore the old value of RSI (saved by 'DOCOL') and resume execution.
107 forth_asm EXIT, 'EXIT'
111 ;; LIT is a special word that reads the next "word pointer" and causes it to be
112 ;; placed on the stack rather than executed.
118 ;; When LITSTRING is encountered while executing a word, it instead reads a
119 ;; string from the definition of that word, and places that string on the stack
120 ;; as (buffer, length).
121 forth_asm LITSTRING, 'LITSTRING'
126 add rsi, rax ; Skip over string before resuming execution
129 ;; Given a string (a pointer following by a size), return the location of the
130 ;; dictionary entry for that word. If no such word exists, return 0.
131 forth_asm FIND, 'FIND'
134 pop [find.search_length]
135 pop [find.search_buffer]
136 mov rsi, [latest_entry] ; Start with the last added word
147 ;; Given an entry in the dictionary, return a pointer to the codeword of that
149 forth_asm TCFA, '>CFA'
151 add rax, 8 + 1 ; [rax] = length of name
152 movzx rbx, byte [rax]
154 add rax, rbx ; [rax] = codeword
158 ;; BRANCH is the fundamental mechanism for branching. BRANCH reads the next word
159 ;; as a signed integer literal and jumps by that offset.
160 forth_asm BRANCH, 'BRANCH'
161 add rsi, [rsi] ; [RSI], which is the next word, contains the offset; we add this to the instruction pointer.
162 next ; Then, we can just continue execution as normal
164 ;; 0BRANCH is like BRANCH, but it jumps only if the top of the stack is zero.
165 forth_asm ZBRANCH, '0BRANCH'
166 ;; Compare top of stack to see if we should branch
173 add rsi, 8 ; We need to skip over the next word, which contains the offset.
176 ;; Duplicate the top of the stack.
177 forth_asm DUP_, 'DUP'
181 ;; Execute the codeword at the given address.
182 forth_asm EXEC, 'EXEC'
186 ;; Expects a character on the stack and prints it to standard output.
187 forth_asm EMIT, 'EMIT'
200 ;; Read a single character from the current input stream. Usually, this will wait
201 ;; for the user to press a key, and then return the corresponding character. When
202 ;; reading from a special buffer, it will instead return the next characater from
205 ;; The ASCII character code is placed on the stack.
213 ;; Are we reading from user input or from the input buffer?
214 cmp [input_buffer], 0
217 ;; Reading user input
222 ;; Reading from buffer
223 mov rax, [input_buffer]
224 movzx rax, byte [rax]
227 dec [input_buffer_length]
230 ;; Read a word and push it onto the stack as a pointer and a size. The pointer
231 ;; is valid until the next call to READ_WORD.
232 forth_asm READ_WORD, 'READ-WORD'
235 ;; Read characters until one of them is not whitespace.
237 ;; We consider newlines and spaces to be whitespace.
243 ;; We got a character that wasn't whitespace. Now read the actual word.
268 ;; Takes a string on the stack and replaces it with the decimal number that the
269 ;; string represents.
270 forth_asm PARSE_NUMBER, 'PARSE-NUMBER'
272 pop rdi ; String pointer
281 ;; Takes a string (in the form of a pointer and a length on the stack) and
282 ;; prints it to standard output.
283 forth_asm TELL, 'TELL'
295 ;; Exit the program cleanly.
296 forth_asm TERMINATE, 'TERMINATE'
300 ;; Duplicate a pair of elements.
301 forth_asm PAIRDUP, '2DUP'
310 ;; Swap the top two elements on the stack.
311 forth_asm SWAP, 'SWAP'
318 ;; Remove the top element from the stack.
319 forth_asm DROP, 'DROP'
323 forth_asm NOT_, 'NOT'
334 ;; .U prints the value on the stack as an unsigned integer in hexadecimal.
337 mov [.printed_length], 1
338 pop rax ; RAX = value to print
339 push rsi ; Save value of RSI
341 ;; We start by constructing the buffer to print in reverse
346 div rbx ; Put remainer in RDX and quotient in RAX
348 ;; Place the appropriate character in the buffer
357 ;; .printed_length is the number of characters that we ulitmately want to
358 ;; print. If we have printed a non-zero character, then we should update
361 je .skip_updating_real_length
363 mov [.printed_length], rbx
364 .skip_updating_real_length:
369 ;; Flip buffer around, since it is currently reversed
370 mov rcx, [.printed_length]
378 add rdi, [.printed_length]
386 mov rdx, [.printed_length]
389 ;; Restore RSI and continue execution
393 ;; Takes a value and an address, and stores the value at the given address.
400 ;; Takes an address and returns the value at the given address.
407 forth_asm PUT_BYTE, 'C!'
413 forth_asm GET_BYTE, 'C@'
415 movzx rax, byte [rax]
419 ;; Add two integers on the stack.
427 ;; Calculate difference between two integers on the stack. The second number is
428 ;; subtracted from the first.
436 ;; Given two integers a and b on the stack, pushes the quotient and remainder of
437 ;; division of a by b.
438 forth_asm TIMESMOD, '/MOD'
447 ;; Read input until next " character is found. Push a string containing the
448 ;; input on the stack as (buffer length). Note that the buffer is only valid
449 ;; until the next call to S" and that no more than 255 characters can be read.
450 forth_asm READ_STRING, 'S"'
451 ;; If the input buffer is set, we should read from there instead.
452 cmp [input_buffer], 0
453 jne read_string_buffer
481 ;; We borrow READ_STRING's buffer. They won't mind.
482 mov [READ_STRING.length], 0
485 mov rbx, [input_buffer]
490 mov rdx, READ_STRING.buffer
491 add rdx, [READ_STRING.length]
493 inc [READ_STRING.length]
496 dec [input_buffer_length]
505 dec [input_buffer_length]
507 push READ_STRING.buffer
508 push [READ_STRING.length]
512 ;; CREATE inserts a new header in the dictionary, and updates LATEST so that it
513 ;; points to the header. To compile a word, the user can then call ',' to
514 ;; continue to append data after the header.
516 ;; It takes the name of the word as a string (address length) on the stack.
517 forth_asm CREATE, 'CREATE'
518 pop rcx ; Word string length
519 pop rdx ; Word string pointer
521 mov rdi, [here] ; rdi = Address at which to insert this entry
522 mov rax, [latest_entry] ; rax = Address of the previous entry
523 mov [rdi], rax ; Insert link to previous entry
524 mov [latest_entry], rdi ; Update LATEST to point to this word
527 mov [rdi], byte 0 ; Insert immediate flag
530 mov [rdi], byte cl ; Insert length
532 ;; Insert word string
536 mov rsi, rdx ; rsi = Word string pointer
559 forth_asm PICK, 'PICK'
561 lea rax, [rsp + 8 * rax]
585 ;; Built-in variables:
591 forth LATEST, 'LATEST'
599 forth SYSCODE, 'SYSCODE'
604 forth INPUT_BUFFER, 'INPUT-BUFFER'
608 forth INPUT_LENGTH, 'INPUT-LENGTH'
609 dq LIT, input_buffer_length
612 section '.data' readable writable
614 ;; The LATEST variable holds a pointer to the word that was last added to the
615 ;; dictionary. This pointer is updated as new words are added, and its value is
616 ;; used by FIND to look up words.
617 latest_entry dq initial_latest_entry
619 ;; The STATE variable is 0 when the interpreter is executing, and non-zero when
623 ;; The interpreter can read either from standard input or from a buffer. When
624 ;; input-buffer is set (non-null), words like READ-WORD and S" will use this
625 ;; buffer instead of reading user input.
627 input_buffer_length dq 0
634 READ_STRING.char_buffer db ?
635 READ_STRING.buffer rb $FF
636 READ_STRING.length dq ?
638 DOTU.chars db '0123456789ABCDEF'
639 DOTU.buffer rq 16 ; 64-bit number has no more than 16 digits in hex
642 DOTU.printed_length dq ?
646 READ_WORD.buffer rb $FF
647 READ_WORD.length db ?
649 ;; Reserve space for compiled words, accessed through HERE.
657 ;; We store some Forth code in sys.f that defined common words that the user
658 ;; would expect to have available at startup. To execute these words, we just
659 ;; include the file directly in the binary, and then interpret it at startup.