baseline
[rrq/rrqforth.git] / main.fasm
1 ; This is a forth interpreter
2         format elf64 executable
3         entry main_code
4
5 ;;; ############################################################
6 ;;; The FORTH words
7         segment readable writable executable
8
9 include 'machine.fasm'
10         
11         ;; PROGRAM_VERSION is the program version string
12         WORD program_version, 'PROGRAM_VERSION', marker
13         db length
14 program_version_string: 
15         include 'version'
16         length = $ - program_version_string
17         
18         ;; MAIN is the program entry point
19         ;; ( -- )
20         WORD_assembler main, "MAIN"
21         mov rsi,program_version_string ; address of string
22         mov edx,length          ; length of string (cheating)
23         mov edi,1               ; stdout
24         mov eax,1               ; sys_write
25         syscall
26         jmp terminate0_code
27
28         ;; TERMINATE0 terminates the program with code 0
29         ;; ( -- )
30         WORD_assembler terminate0, 'TERMINATE0'
31         xor edi,edi
32         mov eax,60
33         syscall
34
35         ;; EXIT ends a forth code defintion, returning to caller
36         ;; ( -- )
37         WORD_assembler exit, 'EXIT'
38         popr rsi
39         next
40         
41         ;; MARKER is a word that pushes the address after itself, then exits
42         ;; ( -- p )
43         WORD_assembler marker, 'MARKER'
44         push qword rsi
45         jmp exit_code
46
47         ;; MARKER@ is a word that pushes a the value after itself, then exits
48         ;; ( -- v )
49         WORD_assembler marker_get, 'MARKER@'
50         push qword [rsi]
51         jmp exit_code
52
53         ;; DOFORTH begins a FORTH defintion
54         WORD_assembler doforth, 'DOFORTH'
55         pushr rsi
56         lea rsi, [rax + 8]
57         next
58
59         ;; LIT is a word that pushes a the value after itself, then continues
60         ;; ( -- v )
61         WORD_assembler lit, 'LIT'
62         push qword [rsi]
63         add rsi, 8
64         next
65
66         ;; HERE is a variable pointing to the free heap
67         WORD here, 'HERE', marker
68         dq heap_start           ; initialise to first "free" data
69
70         ;; WORDS is the list of words
71         WORD words, 'WORDS', marker
72         dq forth_tfa            ; initialise to last forth word
73
74         ;; FORTH is the last word of the VOCABULARY 
75         WORD forth, 'FORTH', marker_get
76         dq forth_tfa
77         
78 heap_start:
79