5 ;; Print a string of a given length.
8 ;; - RCX = Pointer to buffer
9 ;; - RDX = Buffer length
11 ;; Clobbers: RAX, RCX, R11, RDI, RSI
12 macro sys_print_string {
17 call uefi_print_string
24 ;; Read a character from the user into the given buffer.
27 ;; - RSI = Character buffer
30 ;; - BYTE [RSI] = Character
32 ;; Clobbers: RAX, RCX, R11, RDI, RSI, RDX
50 macro sys_terminate code {
55 ;; The code in this macro is placed at the end of each Forth word. When we are
56 ;; executing a definition, this code is what causes execution to resume at the
57 ;; next word in that definition.
59 ;; RSI points to the address of the definition of the next word to execute.
60 lodsq ; Load value at RSI into RAX and increment RSI
61 ;; Now RAX contains the location of the next word to execute. The first 8
62 ;; bytes of this word is the address of the codeword, which is what we want
64 jmp qword [rax] ; Jump to the codeword of the current word
67 ;; pushr and popr work on the return stack, whose location is stored in the
78 ;; The following macro generates the dictionary header. It updates the
79 ;; initial_latest_entry variable, which is used as the initial value of the
80 ;; latest_entry variable that is made available at runtime.
82 ;; The header contains a link to the previous entry, the length of the name of
83 ;; the word and the word itself as a string literal.
85 ;; This macro also defines a label LABEL_entry.
86 initial_latest_entry = 0
87 macro header label, name, immediate {
91 dq initial_latest_entry
97 db .string_end - ($ + 1)
102 initial_latest_entry = label#_entry
105 ;; Define a Forth word that is implemented in assembly. See 'header' for details.
106 macro forth_asm label, name, immediate {
107 header label, name, immediate
112 section '.text' code readable executable
114 include "impl.asm" ; Misc. subroutines
115 include "bootstrap.asm" ; Forth words encoded in Assembly
118 cld ; Clear direction flag so LODSQ does the right thing.
119 mov rbp, return_stack_top ; Initialize return stack
128 ;; The codeword is the code that will be executed at the beginning of a forth
129 ;; word. It needs to save the old RSI and update it to point to the next word to
131 header DOCOL, 'DOCOL'
132 pushr rsi ; Save old value of RSI on return stack; we will continue execution there after we are done executing this word
133 lea rsi, [rax + 8] ; RAX currently points to the address of the codeword, so we want to continue at RAX+8
134 next ; Execute word pointed to by RSI
136 ;; This word is called at the end of a Forth definition. It just needs to
137 ;; restore the old value of RSI (saved by 'DOCOL') and resume execution.
138 forth_asm EXIT, 'EXIT'
142 ;; LIT is a special word that reads the next "word pointer" and causes it to be
143 ;; placed on the stack rather than executed.
149 ;; When LITSTRING is encountered while executing a word, it instead reads a
150 ;; string from the definition of that word, and places that string on the stack
151 ;; as (buffer, length).
152 forth_asm LITSTRING, 'LITSTRING'
157 add rsi, rax ; Skip over string before resuming execution
160 ;; Given a string (a pointer following by a size), return the location of the
161 ;; dictionary entry for that word. If no such word exists, return 0.
162 forth_asm FIND, 'FIND'
165 pop [find.search_length]
166 pop [find.search_buffer]
167 mov rsi, [latest_entry] ; Start with the last added word
178 ;; Given an entry in the dictionary, return a pointer to the codeword of that
180 forth_asm TCFA, '>CFA'
182 add rax, 8 + 1 ; [rax] = length of name
183 movzx rbx, byte [rax]
185 add rax, rbx ; [rax] = codeword
189 ;; BRANCH is the fundamental mechanism for branching. BRANCH reads the next word
190 ;; as a signed integer literal and jumps by that offset.
191 forth_asm BRANCH, 'BRANCH'
192 add rsi, [rsi] ; [RSI], which is the next word, contains the offset; we add this to the instruction pointer.
193 next ; Then, we can just continue execution as normal
195 ;; 0BRANCH is like BRANCH, but it jumps only if the top of the stack is zero.
196 forth_asm ZBRANCH, '0BRANCH'
197 ;; Compare top of stack to see if we should branch
204 add rsi, 8 ; We need to skip over the next word, which contains the offset.
207 ;; Duplicate the top of the stack.
208 forth_asm DUP_, 'DUP'
212 ;; Execute the codeword at the given address.
213 forth_asm EXEC, 'EXEC'
217 ;; Expects a character on the stack and prints it to standard output.
218 forth_asm EMIT, 'EMIT'
231 ;; Read a single character from the current input stream. Usually, this will wait
232 ;; for the user to press a key, and then return the corresponding character. When
233 ;; reading from a special buffer, it will instead return the next characater from
236 ;; The ASCII character code is placed on the stack.
244 ;; Are we reading from user input or from the input buffer?
245 cmp [input_buffer], 0
248 ;; Reading user input
254 movzx rax, byte [.buffer]
258 ;; Reading from buffer
259 mov rax, [input_buffer]
260 movzx rax, byte [rax]
263 dec [input_buffer_length]
266 ;; Read a word and push it onto the stack as a pointer and a size. The pointer
267 ;; is valid until the next call to READ_WORD.
268 forth_asm READ_WORD, 'READ-WORD'
271 ;; Read characters until one of them is not whitespace.
273 ;; We consider newlines and spaces to be whitespace.
279 ;; We got a character that wasn't whitespace. Now read the actual word.
304 ;; Takes a string on the stack and replaces it with the decimal number that the
305 ;; string represents.
306 forth_asm PARSE_NUMBER, 'PARSE-NUMBER'
308 pop rdi ; String pointer
317 ;; Takes a string (in the form of a pointer and a length on the stack) and
318 ;; prints it to standard output.
319 forth_asm TELL, 'TELL'
331 ;; Exit the program cleanly.
332 forth_asm TERMINATE, 'TERMINATE'
335 ;; Duplicate a pair of elements.
336 forth_asm PAIRDUP, '2DUP'
345 ;; Swap the top two elements on the stack.
346 forth_asm SWAP, 'SWAP'
353 ;; Remove the top element from the stack.
354 forth_asm DROP, 'DROP'
358 forth_asm NOT_, 'NOT'
369 ;; .U prints the value on the stack as an unsigned integer in hexadecimal.
372 mov [.printed_length], 1
373 pop rax ; RAX = value to print
374 push rsi ; Save value of RSI
376 ;; We start by constructing the buffer to print in reverse
381 div rbx ; Put remainer in RDX and quotient in RAX
383 ;; Place the appropriate character in the buffer
392 ;; .printed_length is the number of characters that we ulitmately want to
393 ;; print. If we have printed a non-zero character, then we should update
396 je .skip_updating_real_length
398 mov [.printed_length], rbx
399 .skip_updating_real_length:
404 ;; Flip buffer around, since it is currently reversed
405 mov rcx, [.printed_length]
413 add rdi, [.printed_length]
421 mov rdx, [.printed_length]
424 ;; Restore RSI and continue execution
428 ;; Takes a value and an address, and stores the value at the given address.
435 ;; Takes an address and returns the value at the given address.
442 forth_asm PUT_BYTE, 'C!'
448 forth_asm GET_BYTE, 'C@'
450 movzx rax, byte [rax]
454 ;; Add two integers on the stack.
462 ;; Calculate difference between two integers on the stack. The second number is
463 ;; subtracted from the first.
471 ;; Given two integers a and b on the stack, pushes the quotient and remainder of
472 ;; division of a by b.
473 forth_asm TIMESMOD, '/MOD'
482 ;; Read input until next " character is found. Push a string containing the
483 ;; input on the stack as (buffer length). Note that the buffer is only valid
484 ;; until the next call to S" and that no more than 255 characters can be read.
485 forth_asm READ_STRING, 'S"'
486 ;; If the input buffer is set, we should read from there instead.
487 cmp [input_buffer], 0
488 jne read_string_buffer
495 mov rsi, .char_buffer
498 mov al, [.char_buffer]
519 ;; We borrow READ_STRING's buffer. They won't mind.
520 mov [READ_STRING.length], 0
523 mov rbx, [input_buffer]
528 mov rdx, READ_STRING.buffer
529 add rdx, [READ_STRING.length]
531 inc [READ_STRING.length]
534 dec [input_buffer_length]
543 dec [input_buffer_length]
545 push READ_STRING.buffer
546 push [READ_STRING.length]
550 ;; CREATE inserts a new header in the dictionary, and updates LATEST so that it
551 ;; points to the header. To compile a word, the user can then call ',' to
552 ;; continue to append data after the header.
554 ;; It takes the name of the word as a string (address length) on the stack.
555 forth_asm CREATE, 'CREATE'
556 pop rcx ; Word string length
557 pop rdx ; Word string pointer
559 mov rdi, [here] ; rdi = Address at which to insert this entry
560 mov rax, [latest_entry] ; rax = Address of the previous entry
561 mov [rdi], rax ; Insert link to previous entry
562 mov [latest_entry], rdi ; Update LATEST to point to this word
565 mov [rdi], byte 0 ; Insert immediate flag
568 mov [rdi], byte cl ; Insert length
570 ;; Insert word string
574 mov rsi, rdx ; rsi = Word string pointer
597 forth_asm PICK, 'PICK'
599 lea rax, [rsp + 8 * rax]
623 ;; Built-in variables:
629 forth LATEST, 'LATEST'
637 forth SYSCODE, 'SYSCODE'
642 forth INPUT_BUFFER, 'INPUT-BUFFER'
646 forth INPUT_LENGTH, 'INPUT-LENGTH'
647 dq LIT, input_buffer_length
650 section '.data' readable writable
652 ;; The LATEST variable holds a pointer to the word that was last added to the
653 ;; dictionary. This pointer is updated as new words are added, and its value is
654 ;; used by FIND to look up words.
655 latest_entry dq initial_latest_entry
657 ;; The STATE variable is 0 when the interpreter is executing, and non-zero when
661 ;; The interpreter can read either from standard input or from a buffer. When
662 ;; input-buffer is set (non-null), words like READ-WORD and S" will use this
663 ;; buffer instead of reading user input.
665 input_buffer_length dq 0
672 READ_STRING.char_buffer db ?
673 READ_STRING.buffer rb $FF
674 READ_STRING.length dq ?
676 DOTU.chars db '0123456789ABCDEF'
677 DOTU.buffer rq 16 ; 64-bit number has no more than 16 digits in hex
680 DOTU.printed_length dq ?
684 READ_WORD.buffer rb $FF
685 READ_WORD.length db ?
687 ;; Reserve space for compiled words, accessed through HERE.
695 ;; We store some Forth code in sys.f that defined common words that the user
696 ;; would expect to have available at startup. To execute these words, we just
697 ;; include the file directly in the binary, and then interpret it at startup.