-; This is a program
+; This is a forth interpreter
format elf64 executable
- entry main
+ entry main_code
- segment readable executable
- msg db 'hello world.'
- db 10
- length = $ - msg
+;;; ############################################################
+;;; The FORTH words
+ segment readable writable executable
-main:
- lea rsi,[msg] ; address of message
- mov edx,length ; length od trdting
+include 'machine.fasm'
+
+ ;; PROGRAM_VERSION is the program version string
+ WORD program_version, 'PROGRAM_VERSION', marker
+ db length
+program_version_string:
+ include 'version'
+ length = $ - program_version_string
+
+ ;; MAIN is the program entry point
+ ;; ( -- )
+ WORD_assembler main, "MAIN"
+ mov rsi,program_version_string ; address of string
+ mov edx,length ; length of string (cheating)
mov edi,1 ; stdout
mov eax,1 ; sys_write
syscall
+ jmp terminate0_code
-fini:
+ ;; TERMINATE0 terminates the program with code 0
+ ;; ( -- )
+ WORD_assembler terminate0, 'TERMINATE0'
xor edi,edi
mov eax,60
syscall
+
+ ;; EXIT ends a forth code defintion, returning to caller
+ ;; ( -- )
+ WORD_assembler exit, 'EXIT'
+ popr rsi
+ next
+ ;; MARKER is a word that pushes the address after itself, then exits
+ ;; ( -- p )
+ WORD_assembler marker, 'MARKER'
+ push qword rsi
+ jmp exit_code
+
+ ;; MARKER@ is a word that pushes a the value after itself, then exits
+ ;; ( -- v )
+ WORD_assembler marker_get, 'MARKER@'
+ push qword [rsi]
+ jmp exit_code
+
+ ;; DOFORTH begins a FORTH defintion
+ WORD_assembler doforth, 'DOFORTH'
+ pushr rsi
+ lea rsi, [rax + 8]
+ next
+
+ ;; LIT is a word that pushes a the value after itself, then continues
+ ;; ( -- v )
+ WORD_assembler lit, 'LIT'
+ push qword [rsi]
+ add rsi, 8
+ next
+
+ ;; HERE is a variable pointing to the free heap
+ WORD here, 'HERE', marker
+ dq heap_start ; initialise to first "free" data
+
+ ;; WORDS is the list of words
+ WORD words, 'WORDS', marker
+ dq forth_tfa ; initialise to last forth word
+
+ ;; FORTH is the last word of the VOCABULARY
+ WORD forth, 'FORTH', marker_get
+ dq forth_tfa
+
+heap_start:
+