Implement basic return stack
[rrq/jonasforth.git] / main.asm
1 format ELF64 executable
2
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.
6 macro next {
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
11     ;; to execute.
12     jmp qword [rax]         ; Jump to the codeword of the current word
13 }
14
15 ;; pushr and popr work on the return stack, whose location is stored in the
16 ;; register RBP.
17 macro pushr x {
18     sub rbp, 8
19     mov [rbp], x
20 }
21 macro popr x {
22     mov x, [rbp]
23     add rbp, 8
24 }
25
26 segment readable executable
27
28 start:
29     ;; Initialize return stack
30     mov rbp, return_stack_top
31
32     jmp $
33
34 segment readable
35
36 segment readable writable
37
38 ;; Return stack
39 rq $2000
40 return_stack_top: