baseline
[rrq/rrqforth.git] / machine.fasm
1 ;;; This file define/describes the "machine"
2 ;;;
3 ;;; Abstract Machine:
4 ;;; https://en.wikibooks.org/wiki/X86_Assembly/X86_Architecture
5 ;;; 
6 ;;; General Purpose Registers ( * marks those used in syscalls )
7 ;;;  *rax = ( -, eax = ( -, ax = ( ah, al) )) "accumulator"
8 ;;;   rbx = ( -, ebx = ( -, bx = ( bh, bl) )) "base"
9 ;;;  *rcx = ( -, ecx = ( -, cx = ( ch, cl) )) "counter"
10 ;;;  *rdx = ( -, edx = ( -, dx = ( dh, dl) )) "data"
11 ;;;   rsp = ( -, esp = ( -, sp = ( -, spl) )) "stack pointer"
12 ;;;   rbp = ( -, ebp = ( -, bp = ( -, bpl) )) "stack base pointer"
13 ;;;  *rsi = ( -, esi = ( -, si = ( -, sil) )) "source"
14 ;;;  *rdi = ( -, edi = ( -, di = ( -, dil) )) "destination"
15 ;;;  *r8
16 ;;;  *r9
17 ;;;   r10
18 ;;;  *r11
19 ;;;   r12
20 ;;;   r13
21 ;;;   r14
22 ;;;   r15
23 ;;; clobbers rdi rsi rdx rcx r8 r9 r11
24 ;;; rax = syscall id
25 ;;;
26 ;;; Segment Registers
27 ;;;   SS "Stack Segment"
28 ;;;   CS "Code Segment"
29 ;;;   DS "Data Segment"
30 ;;;   ES "Extra Segment"
31 ;;;   FS "more Extra Segment"
32 ;;;   GS "more more Extra Segment"
33 ;;;
34 ;;; EFLAGS Register
35 ;;;   0,0,0,0,0,0,0,0,0,0,ID,VIP,VIF,AC,VM,RF,
36 ;;;   0,NT,[IOPL,IOPL],OF,DF,IF,TF,SF,ZF,0,AF,0,PF,1,CF
37 ;;; 
38 ;;; Instruction pointer
39 ;;;   EIP
40 ;;; 
41 ;;; Syscall allocations
42 ;;; clobbers rdi rsi rdx rcx r8 r9 r11
43 ;;; rax = syscall id
44 ;;;
45 ;;; function calling
46
47 ;;; FORTH model
48 ;;; rsp = data stack pointer
49 ;;; rbp = frame pointer
50 ;;; rdi = frame stack
51 ;;; rsi = instruction pointer
52
53 ;;; ========================================
54 ;;; The next macro "moves" execution to the next FORTH instruction,
55 ;;; using rsi as instruction pointer.
56
57         macro next {
58         lodsq                   ; mov rax, qword [rsi]
59                                 ; add rsi,8
60         jmp qword [rax]         ; goto code of that FORTH word
61         }
62
63 ;;; ========================================
64 ;;; The pushr macro pushes x onto the return stack
65 ;;; The popr macro pops x from the return stack
66         macro pushr x {
67         sub rbp, 8
68         mov qword [rbp], x
69         }
70
71         macro popr x {
72         mov x, [rbp]
73         add rbp, 8
74         }
75
76 ;;; ========================================
77 ;;; 
78
79         previous_word = 0
80
81         ;; Macro WORD starts a FORTH word definition in this code
82         macro WORD label, name, doer, flags {
83 label#_tfa:
84         ;; TFA
85         dq previous_word
86         ;; PFA
87 label#_word:
88         previous_word = label#_word
89         db flags + 0
90         db label - $ - 1
91         db name
92         ;; CFA = pointer to "interpreter"
93 label:
94         dq doer
95         }
96
97         ;; Macro WORD_assembler begins an assembler implementation
98         macro WORD_assembler label, name, flags {
99         WORD label, name, label#_code, flags
100 label#_code:
101         }