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
18 ;; into the buffer pointed to by RCX.
21 ;; Shut down the system.
22 include '%OS_INCLUDE%'
24 ;; Print a string of a given length.
27 ;; - RCX = Pointer to buffer
28 ;; - RDX = Buffer length
30 ;; Clobbers: RAX, RCX, R11, RDI, RSI
31 macro sys_print_string {
43 ;; Read a character from the user into the given buffer.
46 ;; - RSI = Character buffer
49 ;; - BYTE [RSI] = Character
51 ;; Clobbers: RAX, RCX, R11, RDI, RSI, RDX
69 macro sys_terminate code {
74 ;; The code in this macro is placed at the end of each Forth word. When we are
75 ;; executing a definition, this code is what causes execution to resume at the
76 ;; next word in that definition.
78 ;; RSI points to the address of the definition of the next word to execute.
79 lodsq ; Load value at RSI into RAX and increment RSI
80 ;; Now RAX contains the location of the next word to execute. The first 8
81 ;; bytes of this word is the address of the codeword, which is what we want
83 jmp qword [rax] ; Jump to the codeword of the current word
86 ;; pushr and popr work on the return stack, whose location is stored in the
97 ;; The following macro generates the dictionary header. It updates the
98 ;; initial_latest_entry variable, which is used as the initial value of the
99 ;; latest_entry variable that is made available at runtime.
101 ;; The header contains a link to the previous entry, the length of the name of
102 ;; the word and the word itself as a string literal.
104 ;; This macro also defines a label LABEL_entry.
105 initial_latest_entry = 0
106 macro header label, name, immediate {
110 dq initial_latest_entry
116 db .string_end - ($ + 1)
121 initial_latest_entry = label#_entry
124 ;; Define a Forth word that is implemented in assembly. See 'header' for details.
125 macro forth_asm label, name, immediate {
126 header label, name, immediate
131 section '.text' code readable executable
133 include "impl.asm" ; Misc. subroutines
134 include "bootstrap.asm" ; Forth words encoded in Assembly
137 cld ; Clear direction flag so LODSQ does the right thing.
138 mov rbp, return_stack_top ; Initialize return stack
147 ;; The codeword is the code that will be executed at the beginning of a forth
148 ;; word. It needs to save the old RSI and update it to point to the next word to
150 header DOCOL, 'DOCOL'
151 pushr rsi ; Save old value of RSI on return stack; we will continue execution there after we are done executing this word
152 lea rsi, [rax + 8] ; RAX currently points to the address of the codeword, so we want to continue at RAX+8
153 next ; Execute word pointed to by RSI
155 ;; This word is called at the end of a Forth definition. It just needs to
156 ;; restore the old value of RSI (saved by 'DOCOL') and resume execution.
157 forth_asm EXIT, 'EXIT'
161 ;; LIT is a special word that reads the next "word pointer" and causes it to be
162 ;; placed on the stack rather than executed.
168 ;; When LITSTRING is encountered while executing a word, it instead reads a
169 ;; string from the definition of that word, and places that string on the stack
170 ;; as (buffer, length).
171 forth_asm LITSTRING, 'LITSTRING'
176 add rsi, rax ; Skip over string before resuming execution
179 ;; Given a string (a pointer following by a size), return the location of the
180 ;; dictionary entry for that word. If no such word exists, return 0.
181 forth_asm FIND, 'FIND'
184 pop [find.search_length]
185 pop [find.search_buffer]
186 mov rsi, [latest_entry] ; Start with the last added word
197 ;; Given an entry in the dictionary, return a pointer to the codeword of that
199 forth_asm TCFA, '>CFA'
201 add rax, 8 + 1 ; [rax] = length of name
202 movzx rbx, byte [rax]
204 add rax, rbx ; [rax] = codeword
208 ;; BRANCH is the fundamental mechanism for branching. BRANCH reads the next word
209 ;; as a signed integer literal and jumps by that offset.
210 forth_asm BRANCH, 'BRANCH'
211 add rsi, [rsi] ; [RSI], which is the next word, contains the offset; we add this to the instruction pointer.
212 next ; Then, we can just continue execution as normal
214 ;; 0BRANCH is like BRANCH, but it jumps only if the top of the stack is zero.
215 forth_asm ZBRANCH, '0BRANCH'
216 ;; Compare top of stack to see if we should branch
223 add rsi, 8 ; We need to skip over the next word, which contains the offset.
226 ;; Duplicate the top of the stack.
227 forth_asm DUP_, 'DUP'
231 ;; Execute the codeword at the given address.
232 forth_asm EXEC, 'EXEC'
236 ;; Expects a character on the stack and prints it to standard output.
237 forth_asm EMIT, 'EMIT'
250 ;; Read a single character from the current input stream. Usually, this will wait
251 ;; for the user to press a key, and then return the corresponding character. When
252 ;; reading from a special buffer, it will instead return the next characater from
255 ;; The ASCII character code is placed on the stack.
263 ;; Are we reading from user input or from the input buffer?
264 cmp [input_buffer], 0
267 ;; Reading user input
273 movzx rax, byte [.buffer]
277 ;; Reading from buffer
278 mov rax, [input_buffer]
279 movzx rax, byte [rax]
282 dec [input_buffer_length]
285 ;; Read a word and push it onto the stack as a pointer and a size. The pointer
286 ;; is valid until the next call to READ_WORD.
287 forth_asm READ_WORD, 'READ-WORD'
290 ;; Read characters until one of them is not whitespace.
292 ;; We consider newlines and spaces to be whitespace.
298 ;; We got a character that wasn't whitespace. Now read the actual word.
323 ;; Takes a string on the stack and replaces it with the decimal number that the
324 ;; string represents.
325 forth_asm PARSE_NUMBER, 'PARSE-NUMBER'
327 pop rdi ; String pointer
336 ;; Takes a string (in the form of a pointer and a length on the stack) and
337 ;; prints it to standard output.
338 forth_asm TELL, 'TELL'
350 ;; Exit the program cleanly.
351 forth_asm TERMINATE, 'TERMINATE'
354 ;; Duplicate a pair of elements.
355 forth_asm PAIRDUP, '2DUP'
364 ;; Swap the top two elements on the stack.
365 forth_asm SWAP, 'SWAP'
372 ;; Remove the top element from the stack.
373 forth_asm DROP, 'DROP'
377 forth_asm NOT_, 'NOT'
388 ;; .U prints the value on the stack as an unsigned integer in hexadecimal.
391 mov [.printed_length], 1
392 pop rax ; RAX = value to print
393 push rsi ; Save value of RSI
395 ;; We start by constructing the buffer to print in reverse
400 div rbx ; Put remainer in RDX and quotient in RAX
402 ;; Place the appropriate character in the buffer
411 ;; .printed_length is the number of characters that we ulitmately want to
412 ;; print. If we have printed a non-zero character, then we should update
415 je .skip_updating_real_length
417 mov [.printed_length], rbx
418 .skip_updating_real_length:
423 ;; Flip buffer around, since it is currently reversed
424 mov rcx, [.printed_length]
432 add rdi, [.printed_length]
440 mov rdx, [.printed_length]
443 ;; Restore RSI and continue execution
447 ;; Takes a value and an address, and stores the value at the given address.
454 ;; Takes an address and returns the value at the given address.
461 forth_asm PUT_BYTE, 'C!'
467 forth_asm GET_BYTE, 'C@'
469 movzx rax, byte [rax]
473 ;; Add two integers on the stack.
481 ;; Calculate difference between two integers on the stack. The second number is
482 ;; subtracted from the first.
490 ;; Given two integers a and b on the stack, pushes the quotient and remainder of
491 ;; division of a by b.
492 forth_asm TIMESMOD, '/MOD'
501 ;; Read input until next " character is found. Push a string containing the
502 ;; input on the stack as (buffer length). Note that the buffer is only valid
503 ;; until the next call to S" and that no more than 255 characters can be read.
504 forth_asm READ_STRING, 'S"'
505 ;; If the input buffer is set, we should read from there instead.
506 cmp [input_buffer], 0
507 jne read_string_buffer
514 mov rsi, .char_buffer
517 mov al, [.char_buffer]
538 ;; We borrow READ_STRING's buffer. They won't mind.
539 mov [READ_STRING.length], 0
542 mov rbx, [input_buffer]
547 mov rdx, READ_STRING.buffer
548 add rdx, [READ_STRING.length]
550 inc [READ_STRING.length]
553 dec [input_buffer_length]
562 dec [input_buffer_length]
564 push READ_STRING.buffer
565 push [READ_STRING.length]
569 ;; CREATE inserts a new header in the dictionary, and updates LATEST so that it
570 ;; points to the header. To compile a word, the user can then call ',' to
571 ;; continue to append data after the header.
573 ;; It takes the name of the word as a string (address length) on the stack.
574 forth_asm CREATE, 'CREATE'
575 pop rcx ; Word string length
576 pop rdx ; Word string pointer
578 mov rdi, [here] ; rdi = Address at which to insert this entry
579 mov rax, [latest_entry] ; rax = Address of the previous entry
580 mov [rdi], rax ; Insert link to previous entry
581 mov [latest_entry], rdi ; Update LATEST to point to this word
584 mov [rdi], byte 0 ; Insert immediate flag
587 mov [rdi], byte cl ; Insert length
589 ;; Insert word string
593 mov rsi, rdx ; rsi = Word string pointer
616 forth_asm PICK, 'PICK'
618 lea rax, [rsp + 8 * rax]
642 ;; Built-in variables:
648 forth LATEST, 'LATEST'
656 forth SYSCODE, 'SYSCODE'
661 forth INPUT_BUFFER, 'INPUT-BUFFER'
665 forth INPUT_LENGTH, 'INPUT-LENGTH'
666 dq LIT, input_buffer_length
669 section '.data' readable writable
671 ;; The LATEST variable holds a pointer to the word that was last added to the
672 ;; dictionary. This pointer is updated as new words are added, and its value is
673 ;; used by FIND to look up words.
674 latest_entry dq initial_latest_entry
676 ;; The STATE variable is 0 when the interpreter is executing, and non-zero when
680 ;; The interpreter can read either from standard input or from a buffer. When
681 ;; input-buffer is set (non-null), words like READ-WORD and S" will use this
682 ;; buffer instead of reading user input.
684 input_buffer_length dq 0
691 READ_STRING.char_buffer db ?
692 READ_STRING.buffer rb $FF
693 READ_STRING.length dq ?
695 DOTU.chars db '0123456789ABCDEF'
696 DOTU.buffer rq 16 ; 64-bit number has no more than 16 digits in hex
699 DOTU.printed_length dq ?
703 READ_WORD.buffer rb $FF
704 READ_WORD.length db ?
706 ;; Reserve space for compiled words, accessed through HERE.
714 ;; We store some Forth code in sys.f that defined common words that the user
715 ;; would expect to have available at startup. To execute these words, we just
716 ;; include the file directly in the binary, and then interpret it at startup.