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, immediate {
39 dq initial_latest_entry
45 db .string_end - ($ + 1)
50 initial_latest_entry = label#_entry
53 ;; Define a Forth word that is implemented in assembly. See 'header' for details.
54 macro forth_asm label, name, immediate {
55 header label, name, immediate
60 ;; Define a Forth word that is implemented in Forth. (The body will be a list of
62 macro forth label, name, immediate {
63 header label, name, immediate
67 segment readable executable
74 cld ; Clear direction flag so LODSQ does the right thing.
75 mov rbp, return_stack_top ; Initialize return stack
82 ;; The codeword is the code that will be executed at the beginning of a forth
83 ;; word. It needs to save the old RSI and update it to point to the next word to
86 pushr rsi ; Save old value of RSI on return stack; we will continue execution there after we are done executing this word
87 lea rsi, [rax + 8] ; RAX currently points to the address of the codeword, so we want to continue at RAX+8
88 next ; Execute word pointed to by RSI
90 ;; This word is called at the end of a Forth definition. It just needs to
91 ;; restore the old value of RSI (saved by 'DOCOL') and resume execution.
92 forth_asm EXIT, 'EXIT'
96 ;; LIT is a special word that reads the next "word pointer" and causes it to be
97 ;; placed on the stack rather than executed.
103 ;; Given a string (a pointer following by a size), return the location of the
104 ;; dictionary entry for that word. If no such word exists, return 0.
105 forth_asm FIND, 'FIND'
108 pop [find.search_length]
109 pop [find.search_buffer]
110 mov rsi, [latest_entry] ; Start with the last added word
121 ;; Given an entry in the dictionary, return a pointer to the codeword of that
123 forth_asm TCFA, '>CFA'
125 add rax, 8 + 1 ; [rax] = length of name
126 movzx rbx, byte [rax]
128 add rax, rbx ; [rax] = codeword
132 ;; BRANCH is the fundamental mechanism for branching. BRANCH reads the next word
133 ;; as a signed integer literal and jumps by that offset.
134 forth_asm BRANCH, 'BRANCH'
135 add rsi, [rsi] ; [RSI], which is the next word, contains the offset; we add this to the instruction pointer.
136 next ; Then, we can just continue execution as normal
138 ;; 0BRANCH is like BRANCH, but it jumps only if the top of the stack is zero.
139 forth_asm ZBRANCH, '0BRANCH'
140 ;; Compare top of stack to see if we should branch
147 add rsi, 8 ; We need to skip over the next word, which contains the offset.
150 ;; Duplicate the top of the stack.
151 forth_asm DUP_, 'DUP'
155 ;; Execute the codeword at the given address.
156 forth_asm EXEC, 'EXEC'
160 ;; Expects a character on the stack and prints it to standard output.
161 forth_asm EMIT, 'EMIT'
174 ;; Prints a newline to standard output.
175 forth NEWLINE, 'NEWLINE'
180 ;; Prints a space to standard output.
186 ;; Read a word from standard input and push it onto the stack as a pointer and a
187 ;; size. The pointer is valid until the next call to READ_WORD.
188 forth_asm READ_WORD, 'READ-WORD'
198 ;; Takes a string on the stack and replaces it with the decimal number that the
199 ;; string represents.
200 forth_asm PARSE_NUMBER, 'PARSE-NUMBER'
202 pop rdi ; String pointer
211 forth READ_NUMBER, 'READ-NUMBER'
216 ;; Takes a string (in the form of a pointer and a length on the stack) and
217 ;; prints it to standard output.
218 forth_asm TELL, 'TELL'
232 ;; Exit the program cleanly.
233 forth_asm TERMINATE, 'TERMINATE'
248 ;; Duplicate a pair of elements.
249 forth_asm PAIRDUP, '2DUP'
258 ;; Swap the top two elements on the stack.
259 forth_asm SWAP, 'SWAP'
266 ;; Remove the top element from the stack.
267 forth_asm DROP, 'DROP'
271 forth_asm NOT_, 'NOT'
282 ;; The INTERPRET word reads and interprets user input. It's behavior depends on
283 ;; the current STATE. It provides special handling for integers.
284 forth INTERPRET, 'INTERPRET'
288 ;; Stack is (word length word length).
289 dq FIND ; Try to find word
291 dq ZBRANCH, 8 * 22 ; Check if word is found
295 dq STATE, GET, ZBRANCH, 8 * 11 ; Check whether we are in compilation or immediate mode
297 ;; (Word found, compilation mode)
298 dq DUP_, IS_IMMEDIATE, NOT_, ZBRANCH, 8 * 6 ; If the word is immediate, continue as we would in immediate mode
300 ;; Otherwise, we want to compile this word
306 ;; (Word found, immediate mode)
309 ;; Stack is (word length addr)
316 ;; - No word is found, assume it is an integer literal -
317 ;; Stack is (word length addr)
321 dq STATE, GET, ZBRANCH, 8 * 5 ; Check whether we are in compilation or immediate mode
323 ;; (Number, compilation mode)
328 ;; (Number, immediate mode)
331 ;; .U prints the value on the stack as an unsigned integer in hexadecimal.
334 mov [.printed_length], 1
335 pop rax ; RAX = value to print
336 push rsi ; Save value of RSI
338 ;; We start by constructing the buffer to print in reverse
343 div rbx ; Put remainer in RDX and quotient in RAX
345 ;; Place the appropriate character in the buffer
354 ;; .printed_length is the number of characters that we ulitmately want to
355 ;; print. If we have printed a non-zero character, then we should update
358 je .skip_updating_real_length
360 mov [.printed_length], rbx
361 .skip_updating_real_length:
366 ;; Flip buffer around, since it is currently reversed
367 mov rcx, [.printed_length]
375 add rdi, [.printed_length]
385 mov rdx, [.printed_length]
388 ;; Restore RSI and continue execution
392 ;; Takes a value and an address, and stores the value at the given address.
399 ;; Takes an address and returns the value at the given address.
406 forth_asm PUT_BYTE, 'C!'
412 forth_asm GET_BYTE, 'C@'
414 movzx rax, byte [rax]
418 ;; Add two integers on the stack.
426 ;; Calculate difference between two integers on the stack. The second number is
427 ;; subtracted from the first.
435 ;; Given two integers a and b on the stack, pushes the quotient and remainder of
436 ;; division of a by b.
437 forth_asm TIMESMOD, '/MOD'
446 ;; Get the location of the STATE variable. It can be set with '!' and read with
452 ;; Get the location of the LATEST variable. It can be set with '!' and read with
454 forth LATEST, 'LATEST'
458 ;; Get the location at which compiled words are expected to be added. This
459 ;; pointer is usually modified automatically when calling ',', but we can also
460 ;; read it manually with 'HERE'.
466 dq HERE, GET, PUT ; Set the memory at the address pointed to by HERE
467 dq HERE, GET, LIT, 8, PLUS ; Calculate new address for HERE to point to
468 dq HERE, PUT ; Update HERE to point to the new address
471 ;; Read user input until next " character is found. Push a string containing the
472 ;; input on the stack as (buffer length). Note that the buffer is only valid
473 ;; until the next call to S" and that no more than 255 character can be read.
474 forth_asm READ_STRING, 'S"'
482 mov rsi, .char_buffer
486 mov al, [.char_buffer]
504 ;; CREATE inserts a new header in the dictionary, and updates LATEST so that it
505 ;; points to the header. To compile a word, the user can then call ',' to
506 ;; continue to append data after the header.
508 ;; It takes the name of the word as a string (address length) on the stack.
509 forth_asm CREATE, 'CREATE'
510 pop rcx ; Word string length
511 pop rdx ; Word string pointer
513 mov rdi, [here] ; rdi = Address at which to insert this entry
514 mov rax, [latest_entry] ; rax = Address of the previous entry
515 mov [rdi], rax ; Insert link to previous entry
516 mov [latest_entry], rdi ; Update LATEST to point to this word
519 mov [rdi], byte 0 ; Insert immediate flag
522 mov [rdi], byte cl ; Insert length
524 ;; Insert word string
528 mov rsi, rdx ; rsi = Word string pointer
537 ;; Mark the last added word as immediate.
538 forth IMMEDIATE, 'IMMEDIATE', 1
545 ;; Given the address of a word, return 0 if the given word is not immediate.
546 forth IS_IMMEDIATE, 'IMMEDIATE?'
551 ;; Enter immediate mode, immediately
552 forth INTO_IMMEDIATE, '[', 1
553 dq LIT, 0, STATE, PUT_BYTE
556 ;; Enter compilation mode
557 forth OUTOF_IMMEDIATE, ']'
558 dq LIT, 1, STATE, PUT_BYTE
575 forth_asm PICK, 'PICK'
577 lea rax, [rsp + 8 * rax]
600 segment readable writable
602 ;; The LATEST variable holds a pointer to the word that was last added to the
603 ;; dictionary. This pointer is updated as new words are added, and its value is
604 ;; used by FIND to look up words.
605 latest_entry dq initial_latest_entry
607 ;; The STATE variable is 0 when the interpreter is executing, and non-zero when
616 READ_STRING.char_buffer db ?
617 READ_STRING.buffer rb $FF
618 READ_STRING.length dq ?
620 DOTU.chars db '0123456789ABCDEF'
621 DOTU.buffer rq 16 ; 64-bit number has no more than 16 digits in hex
624 DOTU.printed_length dq ?
626 ;; Reserve space for compiled words, accessed through HERE.