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
34 ;; register RBP. Always allocates an extra 8 bytes as "local frame
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'
76 macro forth_asm label, name, immediate {
77 header label, name, immediate
82 ;; ############################################################
83 section '.text' code readable executable
85 include "impl.asm" ; Misc. subroutines
86 include "bootstrap.asm" ; Forth words encoded in Assembly
89 cld ; Clear direction flag so LODSQ does the right thing.
90 mov rbp, return_stack_top ; Initialize return stack
99 ;; The codeword is the code that will be executed at the beginning of
100 ;; a forth word. It needs to save the old RSI and update it to point
101 ;; to the next word to execute.
102 header DOCOL, 'DOCOL'
103 pushr rsi ; Save old value of RSI on return stack; we
104 ; will continue execution there after we are
105 ; done executing this word
106 lea rsi, [rax + 8] ; RAX currently points to the address of the
107 ; codeword, so we want to continue at RAX+8
108 next ; Execute word pointed to by RSI
110 ;; This word is called at the end of a Forth definition. It just needs to
111 ;; restore the old value of RSI (saved by 'DOCOL') and resume execution.
112 forth_asm EXIT, 'EXIT'
116 ;; LIT is a special word that reads the next "word pointer" and causes
117 ;; it to be placed on the stack rather than executed.
123 ;; When LITSTRING is encountered while executing a word, it instead
124 ;; reads a string from the definition of that word, and places that
125 ;; string on the stack as (buffer, length).
126 forth_asm LITSTRING, 'LITSTRING'
131 add rsi, rax ; Skip over string before resuming execution
134 ;; Given a string (a pointer following by a size), return the location
135 ;; of the dictionary entry for that word. If no such word exists,
137 forth_asm FIND, 'FIND'
140 pop [find.search_length]
141 pop [find.search_buffer]
142 mov rsi, [latest_entry] ; Start with the last added word
153 ;; Given an entry in the dictionary, return a pointer to the codeword
155 forth_asm TCFA, '>CFA'
157 add rax, 8 + 1 ; [rax] = length of name
158 movzx rbx, byte [rax]
160 add rax, rbx ; [rax] = codeword
164 ;; BRANCH is the fundamental mechanism for branching. BRANCH reads the
165 ;; next word as a signed integer literal and jumps by that offset.
167 forth_asm BRANCH, 'BRANCH'
168 add rsi, [rsi] ; [RSI], which is the next word, contains the offset
169 ; we add this to the instruction pointer.
170 next ; Then, we can just continue execution as normal
172 ;; 0BRANCH is like BRANCH, but it jumps only if the top of the stack
174 forth_asm ZBRANCH, '0BRANCH'
176 cmp rax, 0 ; Compare top of stack to see if we should branch
182 add rsi, 8 ; We need to skip over the next word, which contains
186 ;; Push the return stack pointer address. "grows" negatively
187 forth_asm RSPGET, 'R='
192 ;; Push the address of the frame quad
198 ;; The return stack "grows" negatively, and rbp is the address of the top
199 ;; Move rbp by n (from stack) bytes
200 forth_asm RSPADD, 'R+'
205 ;; Push top of the stack.
206 forth_asm TOP_, 'TOP'
211 ;; Duplicate the top of the stack.
212 forth_asm DUP_, 'DUP'
216 ;; Execute the codeword at the given address.
217 forth_asm EXEC, 'EXEC'
221 ;; This word skips a word without exectuing, but pushes its address
222 forth_asm SKIP_, 'SKIP'
224 add rsi, 8 ; We need to skip over the next word, which contains
228 ;; Expects a character on the stack and prints it to standard output.
229 forth_asm EMIT, 'EMIT'
242 ;; Read a single character from the current input stream. Usually,
243 ;; this will wait for the user to press a key, and then return the
244 ;; corresponding character. When reading from a special buffer, it
245 ;; will instead return the next characater from that buffer.
247 ;; The ASCII character code is placed on the stack.
255 ;; Are we reading from user input or from the input buffer?
256 cmp [input_buffer], 0
259 ;; Reading user input
264 ;; Reading from buffer
265 mov rax, [input_buffer]
266 movzx rax, byte [rax]
269 dec [input_buffer_length]
272 ;; Read a word and push it onto the stack as a pointer and a size. The
273 ;; pointer is valid until the next call to READ_WORD.
274 forth_asm READ_WORD, 'READ-WORD'
277 ;; Read characters until one of them is not whitespace.
279 ;; We consider newlines, tabs and spaces to be whitespace.
287 ;; We got a character that wasn't whitespace. Now read the actual word.
314 ;; Takes a string on the stack and replaces it with the decimal number
315 ;; that the string represents.
316 forth_asm PARSE_NUMBER, 'PARSE-NUMBER'
318 pop rdi ; String pointer
327 ;; Takes a string (in the form of a pointer and a length on the stack) and
328 ;; prints it to standard output.
329 forth_asm TELL, 'TELL'
341 ;; Exit the program cleanly.
342 forth_asm TERMINATE, 'TERMINATE'
346 ;; Duplicate a pair of elements.
347 forth_asm PAIRDUP, '2DUP'
356 ;; Swap the top two elements on the stack.
357 forth_asm SWAP, 'SWAP'
364 ;; Remove the top element from the stack.
365 forth_asm DROP, 'DROP'
369 ;; Takes a value and an address, and stores the value at the given address.
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
482 ;; number is subtracted from the first.
490 ;; Multiply two integers on the stack ignoring overflow
498 ;; Given two integers a and b on the stack, pushes the quotient and
499 ;; remainder of division of a by b.
500 forth_asm TIMESMOD, '/MOD'
509 ;; Read input until next " character is found. Push a string
510 ;; containing the input on the stack as (buffer length). Note that the
511 ;; buffer is only valid until the next call to S" and that no more
512 ;; than 255 characters can be read.
513 forth_asm READ_STRING, 'S"'
514 ;; If the input buffer is set, we should read from there instead.
515 cmp [input_buffer], 0
516 jne read_string_buffer
544 ;; We borrow READ_STRING's buffer. They won't mind.
545 mov [READ_STRING.length], 0
548 mov rbx, [input_buffer]
553 mov rdx, READ_STRING.buffer
554 add rdx, [READ_STRING.length]
556 inc [READ_STRING.length]
559 dec [input_buffer_length]
568 dec [input_buffer_length]
570 push READ_STRING.buffer
571 push [READ_STRING.length]
575 ;; CREATE inserts a new header in the dictionary, and updates LATEST
576 ;; so that it points to the header. To compile a word, the user can
577 ;; then call ',' to continue to append data after the header.
579 ;; It takes the name of the word as a string (address length) on the
581 forth_asm CREATE, 'CREATE'
582 pop rcx ; Word string length
583 pop rdx ; Word string pointer
585 mov rdi, [here] ; rdi = Address at which to insert this entry
586 mov rax, [latest_entry] ; rax = Address of the previous entry
587 mov [rdi], rax ; Insert link to previous entry
588 mov [latest_entry], rdi ; Update LATEST to point to this word
591 mov [rdi], byte 0 ; Insert immediate flag
594 mov [rdi], byte cl ; Insert length
596 ;; Insert word string
600 mov rsi, rdx ; rsi = Word string pointer
623 forth_asm PICK, 'PICK'
625 lea rax, [rsp + 8 * rax]
630 forth_asm OVER, 'OVER'
679 forth EFI_SYSTEM_TABLE_CONSTANT, 'SystemTable'
680 dq LIT, system_table, GET
683 forth_asm EFICALL1, 'EFICALL1'
684 pop rax ; function pointer
685 pop rcx ; 1st argument
691 forth_asm EFICALL2, 'EFICALL2'
692 pop rax ; function pointer
693 pop rdx ; 2nd argument
694 pop rcx ; 1st argument
700 forth_asm EFICALL3, 'EFICALL3'
701 pop rax ; function pointer
702 pop r8 ; 3rd argument
703 pop rdx ; 2nd argument
704 pop rcx ; 1st argument
711 forth_asm EFICALL4, 'EFICALL4'
712 pop rax ; function pointer
713 pop r9 ; 4th argument
714 pop r8 ; 3rd argument
715 pop rdx ; 2nd argument
716 pop rcx ; 1st argument
723 forth_asm EFICALL5, 'EFICALL5'
724 pop rax ; function pointer
725 pop r10 ; 5th argument
726 pop r9 ; 4th argument
727 pop r8 ; 3rd argument
728 pop rdx ; 2nd argument
729 pop rcx ; 1st argument
730 push r10 ; restore as stack argument
737 forth_asm EFICALL10, 'EFICALL10'
738 pop rax ; function pointer
739 mov rcx, [rsp + 8 * 9] ; 1st
740 mov rdx, [rsp + 8 * 8] ; 2nd
741 mov r8, [rsp + 8 * 7]
742 mov r9, [rsp + 8 * 6]
743 ;; Reverse order of stack arguments
744 mov r10, [rsp + 8 * 5]
745 mov r11, [rsp + 8 * 0]
746 mov [rsp + 8 * 5], r11
747 mov [rsp + 8 * 0], r10
748 mov r10, [rsp + 8 * 4]
749 mov r11, [rsp + 8 * 1]
750 mov [rsp + 8 * 4], r11
751 mov [rsp + 8 * 1], r10
752 mov r10, [rsp + 8 * 3]
753 mov r11, [rsp + 8 * 2]
754 mov [rsp + 8 * 3], r11
755 mov [rsp + 8 * 2], r10
762 ;; Built-in variables:
768 forth LATEST, 'LATEST'
776 forth SYSCODE, 'SYSCODE'
781 forth INPUT_BUFFER, 'INPUT-BUFFER'
785 forth INPUT_LENGTH, 'INPUT-LENGTH'
786 dq LIT, input_buffer_length
789 section '.data' readable writable
791 ;; The LATEST variable holds a pointer to the word that was last added
792 ;; to the dictionary. This pointer is updated as new words are added,
793 ;; and its value is used by FIND to look up words.
794 latest_entry dq initial_latest_entry
796 ;; The STATE variable is 0 when the interpreter is executing, and
797 ;; non-zero when it is compiling.
800 ;; The interpreter can read either from standard input or from a
801 ;; buffer. When input-buffer is set (non-null), words like READ-WORD
802 ;; and S" will use this buffer instead of reading user input.
804 input_buffer_length dq 0
811 READ_STRING.char_buffer db ?
812 READ_STRING.buffer rb $FF
813 READ_STRING.length dq ?
815 DOTU.chars db '0123456789abcdef'
816 DOTU.buffer rq 16 ; 64-bit number has no more than 16 digits in hex
819 DOTU.printed_length dq ?
823 READ_WORD.buffer rb $FF
824 READ_WORD.length db ?
826 ;; Reserve space for compiled words, accessed through HERE.
834 ;; We store some Forth code in sys.f that defined common words that
835 ;; the user would expect to have available at startup. To execute
836 ;; these words, we just include the file directly in the binary, and
837 ;; then interpret it at startup.
840 file '../init/uefi.f'
841 file '../init/blurb.f'