1 format ELF64 executable
3 ;; The code in this macro is placed at the end of each Forth word. When we are
4 ;; executing a definition, this code is what causes execution to resume at the
5 ;; next word in that definition.
7 ;; RSI points to the address of the definition of the next word to execute.
8 lodsq ; Load value at RSI into RAX and increment RSI
9 ;; Now RAX contains the location of the next word to execute. The first 8
10 ;; bytes of this word is the address of the codeword, which is what we want
12 jmp qword [rax] ; Jump to the codeword of the current word
15 ;; pushr and popr work on the return stack, whose location is stored in the
26 ;; The following macro generates the dictionary header. It updates the
27 ;; initial_latest_entry variable, which is used as the initial value of the
28 ;; latest_entry variable that is made available at runtime.
30 ;; The header contains a link to the previous entry, the length of the name of
31 ;; the word and the word itself as a string literal.
33 ;; This macro also defines a label LABEL_entry.
34 initial_latest_entry = 0
35 macro header label, name {
39 dq initial_latest_entry
40 db .string_end - ($ + 1)
45 initial_latest_entry = label#_entry
48 ;; Define a Forth word that is implemented in assembly. See 'header' for details.
49 macro forth_asm label, name {
55 ;; Define a Forth word that is implemented in Forth. (The body will be a list of
57 macro forth label, name {
62 segment readable executable
69 cld ; Clear direction flag so LODSQ does the right thing.
70 mov rbp, return_stack_top ; Initialize return stack
77 ;; The codeword is the code that will be executed at the beginning of a forth
78 ;; word. It needs to save the old RSI and update it to point to the next word to
81 pushr rsi ; Save old value of RSI on return stack; we will continue execution there after we are done executing this word
82 lea rsi, [rax + 8] ; RAX currently points to the address of the codeword, so we want to continue at RAX+8
83 next ; Execute word pointed to by RSI
85 ;; This word is called at the end of a Forth definition. It just needs to
86 ;; restore the old value of RSI (saved by 'DOCOL') and resume execution.
87 forth_asm EXIT, 'EXIT'
91 ;; LIT is a special word that reads the next "word pointer" and causes it to be
92 ;; placed on the stack rather than executed.
98 ;; Given a string (a pointer following by a size), return the location of the
99 ;; dictionary entry for that word. If no such word exists, return 0.
100 forth_asm FIND, 'FIND'
103 pop [find.search_length]
104 pop [find.search_buffer]
105 mov rsi, [latest_entry] ; Start with the last added word
116 ;; Given an entry in the dictionary, return a pointer to the codeword of that
118 forth_asm TCFA, '>CFA'
120 add rax, 8 ; [rax] = length of name
121 movzx rbx, byte [rax]
123 add rax, rbx ; [rax] = codeword
127 ;; BRANCH is the fundamental mechanism for branching. BRANCH reads the next word
128 ;; as a signed integer literal and jumps by that offset.
129 forth_asm BRANCH, 'BRANCH'
130 add rsi, [rsi] ; [RSI], which is the next word, contains the offset; we add this to the instruction pointer.
131 next ; Then, we can just continue execution as normal
133 ;; 0BRANCH is like BRANCH, but it jumps only if the top of the stack is zero.
134 forth_asm ZBRANCH, '0BRANCH'
135 ;; Compare top of stack to see if we should branch
142 add rsi, 8 ; We need to skip over the next word, which contains the offset.
145 ;; Duplicate the top of the stack.
146 forth_asm DUP_, 'DUP'
150 ;; Execute the codeword at the given address.
151 forth_asm EXEC, 'EXEC'
155 ;; Expects a character on the stack and prints it to standard output.
156 forth_asm EMIT, 'EMIT'
169 ;; Prints a newline to standard output.
170 forth NEWLINE, 'NEWLINE'
175 ;; Prints a space to standard output.
181 ;; Read a word from standard input and push it onto the stack as a pointer and a
182 ;; size. The pointer is valid until the next call to READ_WORD.
183 forth_asm READ_WORD, 'READ-WORD'
193 ;; Takes a string on the stack and replaces it with the decimal number that the
194 ;; string represents.
195 forth_asm PARSE_NUMBER, 'PARSE-NUMBER'
196 pop [parse_number.length] ; Length
197 pop [parse_number.buffer] ; String pointer
206 forth READ_NUMBER, 'READ-NUMBER'
211 ;; Takes a string (in the form of a pointer and a length on the stack) and
212 ;; prints it to standard output.
213 forth_asm TELL, 'TELL'
227 ;; Exit the program cleanly.
228 forth_asm TERMINATE, 'TERMINATE'
243 ;; Duplicate a pair of elements.
244 forth_asm PAIRDUP, '2DUP'
253 ;; Swap the top two elements on the stack.
254 forth_asm SWAP, 'SWAP'
261 ;; Remove the top element from the stack.
262 forth_asm DROP, 'DROP'
266 ;; The INTERPRET word reads and interprets user input. It's behavior depends on
267 ;; the current STATE. It provides special handling for integers. (TODO)
268 forth INTERPRET, 'INTERPRET'
272 ;; Stack is (word length word length).
273 dq FIND ; Try to find word
275 dq ZBRANCH, 8 * 8 ; Check if word is found
277 ;; Word is found, execute it
279 ;; Stack is (word length addr)
286 ;; No word is found, assume it is an integer literal
287 ;; Stack is (word length addr)
292 ;; .U prints the value on the stack as an unsigned integer in hexadecimal.
295 mov [.printed_length], 1
296 pop rax ; RAX = value to print
297 push rsi ; Save value of RSI
299 ;; We start by constructing the buffer to print in reverse
304 div rbx ; Put remainer in RDX and quotient in RAX
306 ;; Place the appropriate character in the buffer
315 ;; .printed_length is the number of characters that we ulitmately want to
316 ;; print. If we have printed a non-zero character, then we should update
319 je .skip_updating_real_length
321 mov [.printed_length], rbx
322 .skip_updating_real_length:
327 ;; Flip buffer around, since it is currently reversed
328 mov rcx, [.printed_length]
336 add rdi, [.printed_length]
346 mov rdx, [.printed_length]
349 ;; Restore RSI and continue execution
353 ;; Takes a value and an address, and stores the value at the given address.
360 ;; Takes an address and returns the value at the given address.
367 ;; Add two integers on the stack.
375 ;; Calculate difference between two integers on the stack. The second number is
376 ;; subtracted from the first.
384 ;; Get the location of the STATE variable. It can be set with '!' and read with
390 ;; Get the location of the LATEST variable. It can be set with '!' and read with
392 forth LATEST, 'LATEST'
396 ;; Get the location at which compiled words are expected to be added. This
397 ;; pointer is usually modified automatically when calling ',', but we can also
398 ;; read it manually with 'HERE'.
404 dq HERE, GET, PUT ; Set the memory at the address pointed to by HERE
405 dq HERE, GET, LIT, 8, PLUS ; Calculate new address for HERE to point to
406 dq HERE, PUT ; Update HERE to point to the new address
409 ;; Read user input until next " character is found. Push a string containing the
410 ;; input on the stack as (buffer length). Note that the buffer is only valid
411 ;; until the next call to S" and that no more than 255 character can be read.
412 forth_asm READ_STRING, 'S"'
420 mov rsi, .char_buffer
424 mov al, [.char_buffer]
442 ;; CREATE inserts a new header in the dictionary, and updates LATEST so that it
443 ;; points to the header. To compile a word, the user can then call ',' to
444 ;; continue to append data after the header.
446 ;; It takes the name of the word as a string (address length) on the stack.
447 forth_asm CREATE, 'CREATE'
448 pop rcx ; Word string length
449 pop rdx ; Word string pointer
451 mov rdi, [here] ; rdi = Address at which to insert this entry
452 mov rax, [latest_entry] ; rax = Address of the previous entry
453 mov [rdi], rax ; Insert link to previous entry
454 mov [latest_entry], rdi ; Update LATEST to point to this word
457 mov [rdi], rcx ; Insert length
459 ;; Insert word string
463 mov rsi, rdx ; rsi = Word string pointer
478 segment readable writable
480 ;; The LATEST variable holds a pointer to the word that was last added to the
481 ;; dictionary. This pointer is updated as new words are added, and its value is
482 ;; used by FIND to look up words.
483 latest_entry dq initial_latest_entry
485 ;; The STATE variable is 0 when the interpreter is executing, and non-zero when
494 READ_STRING.char_buffer db ?
495 READ_STRING.buffer rb $FF
496 READ_STRING.length dq ?
498 DOTU.chars db '0123456789ABCDEF'
499 DOTU.buffer rq 16 ; 64-bit number has no more than 16 digits in hex
502 DOTU.printed_length dq ?
504 ;; Reserve space for compiled words, accessed through HERE.