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 {
64 segment readable executable
71 cld ; Clear direction flag so LODSQ does the right thing.
72 mov rbp, return_stack_top ; Initialize return stack
79 ;; The codeword is the code that will be executed at the beginning of a forth
80 ;; word. It needs to save the old RSI and update it to point to the next word to
83 pushr rsi ; Save old value of RSI on return stack; we will continue execution there after we are done executing this word
84 lea rsi, [rax + 8] ; RAX currently points to the address of the codeword, so we want to continue at RAX+8
85 next ; Execute word pointed to by RSI
87 ;; This word is called at the end of a Forth definition. It just needs to
88 ;; restore the old value of RSI (saved by 'docol') and resume execution.
89 forth_asm EXIT, 'EXIT'
93 ;; LIT is a special word that reads the next "word pointer" and causes it to be
94 ;; placed on the stack rather than executed.
100 ;; Given a string (a pointer following by a size), return the location of the
101 ;; dictionary entry for that word. If no such word exists, return 0.
102 forth_asm FIND, 'FIND'
105 pop [find.search_length]
106 pop [find.search_buffer]
107 mov rsi, [latest_entry] ; Start with the last added word
118 ;; Given an entry in the dictionary, return a pointer to the codeword of that
120 forth_asm TCFA, '>CFA'
122 add rax, 8 ; [rax] = length of name
123 movzx rbx, byte [rax]
125 add rax, rbx ; [rax] = codeword
129 ;; BRANCH is the fundamental mechanism for branching. BRANCH reads the next word
130 ;; as a signed integer literal and jumps by that offset.
131 forth_asm BRANCH, 'BRANCH'
132 add rsi, [rsi] ; [RSI], which is the next word, contains the offset; we add this to the instruction pointer.
133 next ; Then, we can just continue execution as normal
135 ;; 0BRANCH is like BRANCH, but it jumps only if the top of the stack is zero.
136 forth_asm ZBRANCH, '0BRANCH'
137 ;; Compare top of stack to see if we should branch
144 add rsi, 8 ; We need to skip over the next word, which contains the offset.
147 ;; Duplicate the top of the stack.
148 forth_asm DUP_, 'DUP'
152 ;; Execute the codeword at the given address.
153 forth_asm EXEC, 'EXEC'
157 ;; Expects a character on the stack and prints it to standard output.
158 forth_asm EMIT, 'EMIT'
171 ;; Prints a newline to standard output.
172 forth NEWLINE, 'NEWLINE'
177 ;; Prints a space to standard output.
183 ;; Read a word from standard input and push it onto the stack as a pointer and a
184 ;; size. The pointer is valid until the next call to READ_WORD.
185 forth_asm READ_WORD, 'READ-WORD'
195 ;; Takes a string on the stack and replaces it with the decimal number that the
196 ;; string represents.
197 forth_asm PARSE_NUMBER, 'PARSE-NUMBER'
198 pop [parse_number.length] ; Length
199 pop [parse_number.buffer] ; String pointer
208 forth READ_NUMBER, 'READ-NUMBER'
213 ;; Takes a string (in the form of a pointer and a length on the stack) and
214 ;; prints it to standard output.
215 forth_asm TELL, 'TELL'
229 ;; Exit the program cleanly.
230 forth_asm TERMINATE, 'TERMINATE'
245 ;; Duplicate a pair of elements.
246 forth_asm PAIRDUP, '2DUP'
255 ;; Swap the top two elements on the stack.
256 forth_asm SWAP, 'SWAP'
263 ;; Remove the top element from the stack.
264 forth_asm DROP, 'DROP'
268 ;; The INTERPRET word reads and interprets user input. It's behavior depends on
269 ;; the current STATE. It provides special handling for integers. (TODO)
270 forth INTERPRET, 'INTERPRET'
274 ;; Stack is (word length word length).
275 dq FIND ; Try to find word
277 dq ZBRANCH, 8 * 8 ; Check if word is found
279 ;; Word is found, execute it
281 ;; Stack is (word length addr)
288 ;; No word is found, assume it is an integer literal
289 ;; Stack is (word length addr)
294 ;; .U prints the value on the stack as an unsigned integer in hexadecimal.
297 mov [.printed_length], 1
298 pop rax ; RAX = value to print
299 push rsi ; Save value of RSI
301 ;; We start by constructing the buffer to print in reverse
306 div rbx ; Put remainer in RDX and quotient in RAX
308 ;; Place the appropriate character in the buffer
317 ;; .printed_length is the number of characters that we ulitmately want to
318 ;; print. If we have printed a non-zero character, then we should update
321 je .skip_updating_real_length
323 mov [.printed_length], rbx
324 .skip_updating_real_length:
329 ;; Flip buffer around, since it is currently reversed
330 mov rcx, [.printed_length]
338 add rdi, [.printed_length]
348 mov rdx, [.printed_length]
351 ;; Restore RSI and continue execution
355 ;; Takes a value and an address, and stores the value at the given address.
362 ;; Takes an address and returns the value at the given address.
369 ;; Add two integers on the stack.
377 ;; Calculate difference between two integers on the stack. The second number is
378 ;; subtracted from the first.
386 ;; Get the location of the STATE variable. It can be set with '!' and read with
392 ;; Get the location of the LATEST variable. It can be set with '!' and read with
394 forth LATEST, 'LATEST'
398 ;; Get the location at which compiled words are expected to be added. This
399 ;; pointer is usually modified automatically when calling ',', but we can also
400 ;; read it manually with 'HERE'.
406 dq HERE, GET, PUT ; Set the memory at the address pointed to by HERE
407 dq HERE, GET, LIT, 8, PLUS ; Calculate new address for HERE to point to
408 dq HERE, PUT ; Update HERE to point to the new address
417 segment readable writable
419 ;; The LATEST variable holds a pointer to the word that was last added to the
420 ;; dictionary. This pointer is updated as new words are added, and its value is
421 ;; used by FIND to look up words.
422 latest_entry dq initial_latest_entry
424 ;; The STATE variable is 0 when the interpreter is executing, and non-zero when
433 DOTU.chars db '0123456789ABCDEF'
434 DOTU.buffer rq 16 ; 64-bit number has no more than 16 digits in hex
437 DOTU.printed_length dq ?
439 ;; Reserve space for compiled words, accessed through HERE.