1 ;; The UEFI module defines the following functions. Each of these
2 ;; functions preserve the value of RSI and RSP. They may use other
3 ;; registers as they like.
6 ;; Called at initialization.
9 ;; Takes a string buffer in RCX and the length in RDX, and prints the string
13 ;; Wait for the user to type a key, and then put the corresponding ASCII byte
17 ;; Shut down the system, returning the error code given in RAX.
19 include 'src/uefi.asm'
21 ;; The code in this macro is placed at the end of each Forth word. When we are
22 ;; executing a definition, this code is what causes execution to resume at the
23 ;; next word in that definition.
25 ;; RSI points to the address of the definition of the next word to execute.
26 lodsq ; Load value at RSI into RAX and increment RSI
27 ;; Now RAX contains the location of the next word to execute. The first 8
28 ;; bytes of this word is the address of the codeword, which is what we want
30 jmp qword [rax] ; Jump to the codeword of the current word
33 ;; pushr and popr work on the return stack, whose location is stored in the
44 ;; The following macro generates the dictionary header. It updates the
45 ;; initial_latest_entry variable, which is used as the initial value of the
46 ;; latest_entry variable that is made available at runtime.
48 ;; The header contains a link to the previous entry, the length of the name of
49 ;; the word and the word itself as a string literal.
51 ;; This macro also defines a label LABEL_entry.
52 initial_latest_entry = 0
53 macro header label, name, immediate {
57 dq initial_latest_entry
63 db .string_end - ($ + 1)
68 initial_latest_entry = label#_entry
71 ;; Define a Forth word that is implemented in assembly. See 'header'
73 macro forth_asm label, name, immediate {
74 header label, name, immediate
79 section '.text' code readable executable
81 include "impl.asm" ; Misc. subroutines
82 include "bootstrap.asm" ; Forth words encoded in Assembly
85 cld ; Clear direction flag so LODSQ does the right thing.
86 mov rbp, return_stack_top ; Initialize return stack
95 ;; The codeword is the code that will be executed at the beginning of
96 ;; a forth word. It needs to save the old RSI and update it to point
97 ;; to the next word to execute.
99 pushr rsi ; Save old value of RSI on return stack; we
100 ; will continue execution there after we are
101 ; done executing this word
102 lea rsi, [rax + 8] ; RAX currently points to the address of the
103 ; codeword, so we want to continue at RAX+8
104 next ; Execute word pointed to by RSI
106 ;; This word is called at the end of a Forth definition. It just needs to
107 ;; restore the old value of RSI (saved by 'DOCOL') and resume execution.
108 forth_asm EXIT, 'EXIT'
112 ;; LIT is a special word that reads the next "word pointer" and causes
113 ;; it to be placed on the stack rather than executed.
119 ;; When LITSTRING is encountered while executing a word, it instead
120 ;; reads a string from the definition of that word, and places that
121 ;; string on the stack as (buffer, length).
122 forth_asm LITSTRING, 'LITSTRING'
127 add rsi, rax ; Skip over string before resuming execution
130 ;; Given a string (a pointer following by a size), return the location
131 ;; of the dictionary entry for that word. If no such word exists,
133 forth_asm FIND, 'FIND'
136 pop [find.search_length]
137 pop [find.search_buffer]
138 mov rsi, [latest_entry] ; Start with the last added word
149 ;; Given an entry in the dictionary, return a pointer to the codeword
151 forth_asm TCFA, '>CFA'
153 add rax, 8 + 1 ; [rax] = length of name
154 movzx rbx, byte [rax]
156 add rax, rbx ; [rax] = codeword
160 ;; BRANCH is the fundamental mechanism for branching. BRANCH reads the
161 ;; next word as a signed integer literal and jumps by that offset.
163 forth_asm BRANCH, 'BRANCH'
164 add rsi, [rsi] ; [RSI], which is the next word, contains the offset
165 ; we add this to the instruction pointer.
166 next ; Then, we can just continue execution as normal
168 ;; 0BRANCH is like BRANCH, but it jumps only if the top of the stack
170 forth_asm ZBRANCH, '0BRANCH'
172 cmp rax, 0 ; Compare top of stack to see if we should branch
177 add rsi, 8 ; We need to skip over the next word, which contains
181 ;; Duplicate the top of the stack.
182 forth_asm DUP_, 'DUP'
186 ;; Execute the codeword at the given address.
187 forth_asm EXEC, 'EXEC'
191 ;; Expects a character on the stack and prints it to standard output.
192 forth_asm EMIT, 'EMIT'
205 ;; Read a single character from the current input stream. Usually,
206 ;; this will wait for the user to press a key, and then return the
207 ;; corresponding character. When reading from a special buffer, it
208 ;; will instead return the next characater from that buffer.
210 ;; The ASCII character code is placed on the stack.
218 ;; Are we reading from user input or from the input buffer?
219 cmp [input_buffer], 0
222 ;; Reading user input
227 ;; Reading from buffer
228 mov rax, [input_buffer]
229 movzx rax, byte [rax]
232 dec [input_buffer_length]
235 ;; Read a word and push it onto the stack as a pointer and a size. The
236 ;; pointer is valid until the next call to READ_WORD.
237 forth_asm READ_WORD, 'READ-WORD'
240 ;; Read characters until one of them is not whitespace.
242 ;; We consider newlines and spaces to be whitespace.
248 ;; We got a character that wasn't whitespace. Now read the actual word.
273 ;; Takes a string on the stack and replaces it with the decimal number
274 ;; that the string represents.
275 forth_asm PARSE_NUMBER, 'PARSE-NUMBER'
277 pop rdi ; String pointer
286 ;; Takes a string (in the form of a pointer and a length on the stack) and
287 ;; prints it to standard output.
288 forth_asm TELL, 'TELL'
300 ;; Exit the program cleanly.
301 forth_asm TERMINATE, 'TERMINATE'
305 ;; Duplicate a pair of elements.
306 forth_asm PAIRDUP, '2DUP'
315 ;; Swap the top two elements on the stack.
316 forth_asm SWAP, 'SWAP'
323 ;; Remove the top element from the stack.
324 forth_asm DROP, 'DROP'
328 forth_asm NOT_, 'NOT'
339 ;; .U prints the value on the stack as an unsigned integer in hexadecimal.
342 mov [.printed_length], 1
343 pop rax ; RAX = value to print
344 push rsi ; Save value of RSI
346 ;; We start by constructing the buffer to print in reverse
351 div rbx ; Put remainer in RDX and quotient in RAX
353 ;; Place the appropriate character in the buffer
362 ;; .printed_length is the number of characters that we ulitmately want to
363 ;; print. If we have printed a non-zero character, then we should update
366 je .skip_updating_real_length
368 mov [.printed_length], rbx
369 .skip_updating_real_length:
374 ;; Flip buffer around, since it is currently reversed
375 mov rcx, [.printed_length]
383 add rdi, [.printed_length]
391 mov rdx, [.printed_length]
394 ;; Restore RSI and continue execution
398 ;; Takes a value and an address, and stores the value at the given address.
405 ;; Takes an address and returns the value at the given address.
412 forth_asm PUT_BYTE, 'C!'
418 forth_asm GET_BYTE, 'C@'
420 movzx rax, byte [rax]
424 ;; Add two integers on the stack.
432 ;; Calculate difference between two integers on the stack. The second
433 ;; number is subtracted from the first.
441 ;; Given two integers a and b on the stack, pushes the quotient and remainder of
442 ;; division of a by b.
443 forth_asm TIMESMOD, '/MOD'
452 ;; Read input until next " character is found. Push a string
453 ;; containing the input on the stack as (buffer length). Note that the
454 ;; buffer is only valid until the next call to S" and that no more
455 ;; than 255 characters can be read.
456 forth_asm READ_STRING, 'S"'
457 ;; If the input buffer is set, we should read from there instead.
458 cmp [input_buffer], 0
459 jne read_string_buffer
487 ;; We borrow READ_STRING's buffer. They won't mind.
488 mov [READ_STRING.length], 0
491 mov rbx, [input_buffer]
496 mov rdx, READ_STRING.buffer
497 add rdx, [READ_STRING.length]
499 inc [READ_STRING.length]
502 dec [input_buffer_length]
511 dec [input_buffer_length]
513 push READ_STRING.buffer
514 push [READ_STRING.length]
518 ;; CREATE inserts a new header in the dictionary, and updates LATEST
519 ;; so that it points to the header. To compile a word, the user can
520 ;; then call ',' to continue to append data after the header.
522 ;; It takes the name of the word as a string (address length) on the
524 forth_asm CREATE, 'CREATE'
525 pop rcx ; Word string length
526 pop rdx ; Word string pointer
528 mov rdi, [here] ; rdi = Address at which to insert this entry
529 mov rax, [latest_entry] ; rax = Address of the previous entry
530 mov [rdi], rax ; Insert link to previous entry
531 mov [latest_entry], rdi ; Update LATEST to point to this word
534 mov [rdi], byte 0 ; Insert immediate flag
537 mov [rdi], byte cl ; Insert length
539 ;; Insert word string
543 mov rsi, rdx ; rsi = Word string pointer
566 forth_asm PICK, 'PICK'
568 lea rax, [rsp + 8 * rax]
594 forth EFI_SYSTEM_TABLE_CONSTANT, 'SystemTable'
595 dq LIT, system_table, GET
598 forth_asm EFICALL1, 'EFICALL1'
599 pop rax ; function pointer
600 pop rcx ; 1st argument
608 forth_asm EFICALL2, 'EFICALL2'
609 pop rax ; function pointer
610 pop rdx ; 2nd argument
611 pop rcx ; 1st argument
619 forth_asm EFICALL3, 'EFICALL3'
620 pop rax ; function pointer
621 pop r8 ; 3rd argument
622 pop rdx ; 2nd argument
623 pop rcx ; 1st argument
633 forth_asm EFICALL10, 'EFICALL10'
634 pop rax ; function pointer
636 mov rcx, [rsp + 8 * 9]
637 mov rdx, [rsp + 8 * 8]
638 mov r8, [rsp + 8 * 7]
639 mov r9, [rsp + 8 * 6]
641 ;; Reverse order of stack arguments
642 mov r10, [rsp + 8 * 5]
643 mov r11, [rsp + 8 * 0]
644 mov [rsp + 8 * 5], r11
645 mov [rsp + 8 * 0], r10
647 mov r10, [rsp + 8 * 4]
648 mov r11, [rsp + 8 * 1]
649 mov [rsp + 8 * 4], r11
650 mov [rsp + 8 * 1], r10
652 mov r10, [rsp + 8 * 3]
653 mov r11, [rsp + 8 * 2]
654 mov [rsp + 8 * 3], r11
655 mov [rsp + 8 * 2], r10
665 ;; Built-in variables:
671 forth LATEST, 'LATEST'
679 forth SYSCODE, 'SYSCODE'
684 forth INPUT_BUFFER, 'INPUT-BUFFER'
688 forth INPUT_LENGTH, 'INPUT-LENGTH'
689 dq LIT, input_buffer_length
692 section '.data' readable writable
694 ;; The LATEST variable holds a pointer to the word that was last added
695 ;; to the dictionary. This pointer is updated as new words are added,
696 ;; and its value is used by FIND to look up words.
697 latest_entry dq initial_latest_entry
699 ;; The STATE variable is 0 when the interpreter is executing, and
700 ;; non-zero when it is compiling.
703 ;; The interpreter can read either from standard input or from a
704 ;; buffer. When input-buffer is set (non-null), words like READ-WORD
705 ;; and S" will use this buffer instead of reading user input.
707 input_buffer_length dq 0
714 READ_STRING.char_buffer db ?
715 READ_STRING.buffer rb $FF
716 READ_STRING.length dq ?
718 DOTU.chars db '0123456789ABCDEF'
719 DOTU.buffer rq 16 ; 64-bit number has no more than 16 digits in hex
722 DOTU.printed_length dq ?
726 READ_WORD.buffer rb $FF
727 READ_WORD.length db ?
729 ;; Reserve space for compiled words, accessed through HERE.
737 ;; We store some Forth code in sys.f that defined common words that
738 ;; the user would expect to have available at startup. To execute
739 ;; these words, we just include the file directly in the binary, and
740 ;; then interpret it at startup.
743 file '../init/uefi.f'