3 format ELF64 executable
5 ;; The code in this macro is placed at the end of each Forth word. When we are
6 ;; executing a definition, this code is what causes execution to resume at the
7 ;; next word in that definition.
9 ;; RSI points to the address of the definition of the next word to execute.
10 lodsq ; Load value at RSI into RAX and increment RSI
11 ;; Now RAX contains the location of the next word to execute. The first 8
12 ;; bytes of this word is the address of the codeword, which is what we want
14 jmp qword [rax] ; Jump to the codeword of the current word
17 ;; pushr and popr work on the return stack, whose location is stored in the
28 ;; The following macro generates the dictionary header. It updates the
29 ;; initial_latest_entry variable, which is used as the initial value of the
30 ;; latest_entry variable that is made available at runtime.
32 ;; The header contains a link to the previous entry, the length of the name of
33 ;; the word and the word itself as a string literal.
35 ;; This macro also defines a label LABEL_entry.
36 initial_latest_entry = 0
37 macro header label, name, immediate {
41 dq initial_latest_entry
47 db .string_end - ($ + 1)
52 initial_latest_entry = label#_entry
55 ;; Define a Forth word that is implemented in assembly. See 'header' for details.
56 macro forth_asm label, name, immediate {
57 header label, name, immediate
62 ;; Define a Forth word that is implemented in Forth. (The body will be a list of
64 macro forth label, name, immediate {
65 header label, name, immediate
69 segment readable executable
76 cld ; Clear direction flag so LODSQ does the right thing.
77 mov rbp, return_stack_top ; Initialize return stack
84 ;; The codeword is the code that will be executed at the beginning of a forth
85 ;; word. It needs to save the old RSI and update it to point to the next word to
88 pushr rsi ; Save old value of RSI on return stack; we will continue execution there after we are done executing this word
89 lea rsi, [rax + 8] ; RAX currently points to the address of the codeword, so we want to continue at RAX+8
90 next ; Execute word pointed to by RSI
92 ;; This word is called at the end of a Forth definition. It just needs to
93 ;; restore the old value of RSI (saved by 'DOCOL') and resume execution.
94 forth_asm EXIT, 'EXIT'
98 ;; LIT is a special word that reads the next "word pointer" and causes it to be
99 ;; placed on the stack rather than executed.
105 ;; Given a string (a pointer following by a size), return the location of the
106 ;; dictionary entry for that word. If no such word exists, return 0.
107 forth_asm FIND, 'FIND'
110 pop [find.search_length]
111 pop [find.search_buffer]
112 mov rsi, [latest_entry] ; Start with the last added word
123 ;; Given an entry in the dictionary, return a pointer to the codeword of that
125 forth_asm TCFA, '>CFA'
127 add rax, 8 + 1 ; [rax] = length of name
128 movzx rbx, byte [rax]
130 add rax, rbx ; [rax] = codeword
134 ;; BRANCH is the fundamental mechanism for branching. BRANCH reads the next word
135 ;; as a signed integer literal and jumps by that offset.
136 forth_asm BRANCH, 'BRANCH'
137 add rsi, [rsi] ; [RSI], which is the next word, contains the offset; we add this to the instruction pointer.
138 next ; Then, we can just continue execution as normal
140 ;; 0BRANCH is like BRANCH, but it jumps only if the top of the stack is zero.
141 forth_asm ZBRANCH, '0BRANCH'
142 ;; Compare top of stack to see if we should branch
149 add rsi, 8 ; We need to skip over the next word, which contains the offset.
152 ;; Duplicate the top of the stack.
153 forth_asm DUP_, 'DUP'
157 ;; Execute the codeword at the given address.
158 forth_asm EXEC, 'EXEC'
162 ;; Expects a character on the stack and prints it to standard output.
163 forth_asm EMIT, 'EMIT'
176 ;; Prints a newline to standard output.
177 forth NEWLINE, 'NEWLINE'
182 ;; Prints a space to standard output.
188 ;; Read a word from standard input and push it onto the stack as a pointer and a
189 ;; size. The pointer is valid until the next call to READ_WORD.
190 forth_asm READ_WORD, 'READ-WORD'
200 ;; Takes a string on the stack and replaces it with the decimal number that the
201 ;; string represents.
202 forth_asm PARSE_NUMBER, 'PARSE-NUMBER'
204 pop rdi ; String pointer
213 forth READ_NUMBER, 'READ-NUMBER'
218 ;; Takes a string (in the form of a pointer and a length on the stack) and
219 ;; prints it to standard output.
220 forth_asm TELL, 'TELL'
234 ;; Exit the program cleanly.
235 forth_asm TERMINATE, 'TERMINATE'
240 ;; Duplicate a pair of elements.
241 forth_asm PAIRDUP, '2DUP'
250 ;; Swap the top two elements on the stack.
251 forth_asm SWAP, 'SWAP'
258 ;; Remove the top element from the stack.
259 forth_asm DROP, 'DROP'
263 forth_asm NOT_, 'NOT'
274 ;; The INTERPRET word reads and interprets user input. It's behavior depends on
275 ;; the current STATE. It provides special handling for integers.
276 forth INTERPRET, 'INTERPRET'
280 ;; Stack is (word length word length).
281 dq FIND ; Try to find word
283 dq ZBRANCH, 8 * 22 ; Check if word is found
287 dq STATE, GET, ZBRANCH, 8 * 11 ; Check whether we are in compilation or immediate mode
289 ;; (Word found, compilation mode)
290 dq DUP_, IS_IMMEDIATE, NOT_, ZBRANCH, 8 * 6 ; If the word is immediate, continue as we would in immediate mode
292 ;; Otherwise, we want to compile this word
298 ;; (Word found, immediate mode)
301 ;; Stack is (word length addr)
308 ;; - No word is found, assume it is an integer literal -
309 ;; Stack is (word length addr)
313 dq STATE, GET, ZBRANCH, 8 * 5 ; Check whether we are in compilation or immediate mode
315 ;; (Number, compilation mode)
320 ;; (Number, immediate mode)
323 ;; .U prints the value on the stack as an unsigned integer in hexadecimal.
326 mov [.printed_length], 1
327 pop rax ; RAX = value to print
328 push rsi ; Save value of RSI
330 ;; We start by constructing the buffer to print in reverse
335 div rbx ; Put remainer in RDX and quotient in RAX
337 ;; Place the appropriate character in the buffer
346 ;; .printed_length is the number of characters that we ulitmately want to
347 ;; print. If we have printed a non-zero character, then we should update
350 je .skip_updating_real_length
352 mov [.printed_length], rbx
353 .skip_updating_real_length:
358 ;; Flip buffer around, since it is currently reversed
359 mov rcx, [.printed_length]
367 add rdi, [.printed_length]
377 mov rdx, [.printed_length]
380 ;; Restore RSI and continue execution
384 ;; Takes a value and an address, and stores the value at the given address.
391 ;; Takes an address and returns the value at the given address.
398 forth_asm PUT_BYTE, 'C!'
404 forth_asm GET_BYTE, 'C@'
406 movzx rax, byte [rax]
410 ;; Add two integers on the stack.
418 ;; Calculate difference between two integers on the stack. The second number is
419 ;; subtracted from the first.
427 ;; Given two integers a and b on the stack, pushes the quotient and remainder of
428 ;; division of a by b.
429 forth_asm TIMESMOD, '/MOD'
438 ;; Get the location of the STATE variable. It can be set with '!' and read with
444 ;; Get the location of the LATEST variable. It can be set with '!' and read with
446 forth LATEST, 'LATEST'
450 ;; Get the location at which compiled words are expected to be added. This
451 ;; pointer is usually modified automatically when calling ',', but we can also
452 ;; read it manually with 'HERE'.
458 dq HERE, GET, PUT ; Set the memory at the address pointed to by HERE
459 dq HERE, GET, LIT, 8, PLUS ; Calculate new address for HERE to point to
460 dq HERE, PUT ; Update HERE to point to the new address
463 ;; Read user input until next " character is found. Push a string containing the
464 ;; input on the stack as (buffer length). Note that the buffer is only valid
465 ;; until the next call to S" and that no more than 255 character can be read.
466 forth_asm READ_STRING, 'S"'
474 mov rsi, .char_buffer
478 mov al, [.char_buffer]
496 ;; CREATE inserts a new header in the dictionary, and updates LATEST so that it
497 ;; points to the header. To compile a word, the user can then call ',' to
498 ;; continue to append data after the header.
500 ;; It takes the name of the word as a string (address length) on the stack.
501 forth_asm CREATE, 'CREATE'
502 pop rcx ; Word string length
503 pop rdx ; Word string pointer
505 mov rdi, [here] ; rdi = Address at which to insert this entry
506 mov rax, [latest_entry] ; rax = Address of the previous entry
507 mov [rdi], rax ; Insert link to previous entry
508 mov [latest_entry], rdi ; Update LATEST to point to this word
511 mov [rdi], byte 0 ; Insert immediate flag
514 mov [rdi], byte cl ; Insert length
516 ;; Insert word string
520 mov rsi, rdx ; rsi = Word string pointer
529 ;; Mark the last added word as immediate.
530 forth IMMEDIATE, 'IMMEDIATE', 1
537 ;; Given the address of a word, return 0 if the given word is not immediate.
538 forth IS_IMMEDIATE, 'IMMEDIATE?'
543 ;; Enter immediate mode, immediately
544 forth INTO_IMMEDIATE, '[', 1
545 dq LIT, 0, STATE, PUT_BYTE
548 ;; Enter compilation mode
549 forth OUTOF_IMMEDIATE, ']'
550 dq LIT, 1, STATE, PUT_BYTE
567 forth_asm PICK, 'PICK'
569 lea rax, [rsp + 8 * rax]
591 segment readable writable
593 ;; The LATEST variable holds a pointer to the word that was last added to the
594 ;; dictionary. This pointer is updated as new words are added, and its value is
595 ;; used by FIND to look up words.
596 latest_entry dq initial_latest_entry
598 ;; The STATE variable is 0 when the interpreter is executing, and non-zero when
607 READ_STRING.char_buffer db ?
608 READ_STRING.buffer rb $FF
609 READ_STRING.length dq ?
611 DOTU.chars db '0123456789ABCDEF'
612 DOTU.buffer rq 16 ; 64-bit number has no more than 16 digits in hex
615 DOTU.printed_length dq ?
617 ;; Reserve space for compiled words, accessed through HERE.