aboutsummaryrefslogtreecommitdiffstats
path: root/src/kernel/forth.asm
diff options
context:
space:
mode:
Diffstat (limited to 'src/kernel/forth.asm')
-rw-r--r--src/kernel/forth.asm159
1 files changed, 113 insertions, 46 deletions
diff --git a/src/kernel/forth.asm b/src/kernel/forth.asm
index 05f254f..5ac0496 100644
--- a/src/kernel/forth.asm
+++ b/src/kernel/forth.asm
@@ -5,96 +5,158 @@
%macro defword 2-3 0
word_%2:
- dw link
+ dd link
%define link word%2
%strlen %%len %1
db %3+%%len
db %1
- %2
+ align 4
+ dd %2
%endmacro
+;; Collection of useful macros to built on later
%macro NEXT 0
lodsl
jmp [eax]
%endmacro
-%macro STACK_PUSH 1
+%macro DATA_PUSH 1
lea ebp, [ebp - 4]
mov ebp, %1
%endmacro
-%macro STACK_POP 1
+%macro DATA_POP 1
mov %1, [ebp]
lea ebp, [ebp + 4]
%endmacro
+%macro RET_PUSH 1
+ mov %1, [edi]
+ lea edi, [edi + 4]
+%endmacro
+
+%macro RET_POP 1
+ lea edi, [edi - 4]
+ mov [edi], %1
+%endmacro
+
+%macro DOCOL 0
+ RET_PUSH esi
+ lea esi, [ebx + 4]
+ NEXT
+%endmacro
+
+;; Words related to the stack
+
defword "DROP", DROP
- pop eax
+ DATA_POP eax
NEXT
defword "SWAP", SWAP
- pop eax
- pop ebx
- push eax
- push ebx
+ DATA_POP eax
+ DATA_POP ebx
+ DATA_PUSH eax
+ DATA_PUSH ebx
NEXT
defword "DUP", DUP
- mov eax, [esp]
- push eax
+ DATA_POP eax
+ DATA_PUSH eax
+ DATA_PUSH eax
NEXT
defword "OVER", OVER
- mov eax, [esp + 4]
- push eax
+ DATA_POP eax
+ DATA_POP ebx
+ DATA_PUSH ebx
+ DATA_PUSH eax
+ DATA_PUSH ebx
NEXT
defword "ROT", ROT
- pop eax
- pop ebx
- pop ecx
- push ebx
- push eax
- push ecx
+ DATA_POP eax
+ DATA_POP ebx
+ DATA_POP ecx
+ DATA_PUSH ebx
+ DATA_PUSH eax
+ DATA_PUSH ecx
NEXT
defword "-ROT", NROT
- pop eax
- pop ebx
- pop ecx
- push eax
- push ecx
- push ebx
+ DATA_POP eax
+ DATA_POP ebx
+ DATA_POP ecx
+ DATA_PUSH eax
+ DATA_PUSH ecx
+ DATA_PUSH ebx
NEXT
defword "2DROP", TWODROP
- pop eax
- pop eax
+ DATA_POP eax
+ DATA_POP eax
NEXT
defword "2DUP", TWODUP
- mov eax, [esp]
- mov ebx, [esp + 4]
- push ebx
- push eax
+ DATA_POP eax
+ DATA_POP ebx
+ DATA_PUSH ebx
+ DATA_PUSH eax
+ DATA_PUSH ebx
+ DATA_PUSH eax
NEXT
+ defword "?DUP", QDUP
+ DATA_POP eax
+ test eax, eax
+ jz .done
+ DATA_PUSH eax
+ DATA_PUSH eax
+.done:
+ NEXT
+
+;; Words related to memory
+
defword "@", FETCH
- pop ebx
- push [ebx]
+ DATA_POP ebx
+ mov eax, [ebx]
+ DATA_PUSH eax
NEXT
+ defword "!", STORE
+ DATA_POP ebx
+ DATA_POP eax
+ mov [ebx], eax
+ NEXT
+
+ defword "C@", CFETCH
+ DATA_POP ebx
+ movzx eax, byte [ebx]
+ DATA_PUSH eax
+ NEXT
+
+ defword "C!", CSTORE
+ DATA_POP ebx
+ DATA_POP eax
+ mov [ebx], al
+
+;; Words related to the interpreter
+
defword "EXIT", EXIT_WORD
- Pop Esi
- Next
+ RET_POP esi
+ NEXT
- Defword "LIT", LITERAL
+ defword "LIT", LITERAL
lodsl
- add esi, eax
+ DATA_PUSH eax
NEXT
+ defword "BRANCH", branch
+ lodsl
+ add esi, eax
+ NEXT
+
defword "0BRANCH", ZERO_BRANCH
- pop eax
+ DATA_POP eax
test eax, eax
jnz .skip
lodsl
@@ -104,14 +166,19 @@
add esi, 4
NEXT
- defword "?DUP", QDUP
- mov eax, esp
- test eax, eax
- jz .1
- push eax
- .1:
+ defword "HERE", HERE
+ DATA_PUSH dword [here]
NEXT
-
- defword
+
+ defword "ALLOT", ALLOT
+ DATA_POP eax
+ add [here], eax
+ NEXT
+
+ defword ",", COMMA
+ DATA_POP eax
+ mov ebx, [here]
+ mov [ebx], eax
+ add dword [here], 4
%endif