Implement looping words and add fibonacci example
authorJonas Hvid <mail@johv.dk>
Tue, 10 Dec 2019 15:58:14 +0000 (16:58 +0100)
committerJonas Hvid <mail@johv.dk>
Tue, 10 Dec 2019 15:58:14 +0000 (16:58 +0100)
main.asm
sys.f

index 086d1dc243dcfa4d8641d2eba627eedd804c1138..cb2e0d5deb2b08c33e1d7ea4a2bfdc100ae39d12 100644 (file)
--- a/main.asm
+++ b/main.asm
@@ -563,6 +563,34 @@ forth_asm TICK, "'"
   push rax
   next
 
+forth_asm ROT, 'ROT'
+  pop rax
+  pop rbx
+  pop rdx
+  push rax
+  push rdx
+  push rbx
+  next
+
+forth_asm PICK, 'PICK'
+  pop rax
+  lea rax, [rsp + 8 * rax]
+  mov rax, [rax]
+  push rax
+  next
+
+forth_asm EQL, '='
+  pop rax
+  pop rbx
+  cmp rax, rbx
+  je .eq
+.noteq:
+  push 0
+  next
+.eq:
+  push 1
+  next
+
 forth MAIN, 'MAIN'
   dq HELLO
   dq INTERPRET
diff --git a/sys.f b/sys.f
index cab2ab7173df3f16b69381af1c9659e0cd19629a..09963525c9e3a9f343efdafd87bbf476ca05a252 100644 (file)
--- a/sys.f
+++ b/sys.f
@@ -33,3 +33,29 @@ EXIT [
   SWAP DUP HERE @ SWAP - SWAP !
 ;
 
+: BEGIN IMMEDIATE
+  HERE @
+;
+
+: UNTIL IMMEDIATE
+  ' 0BRANCH ,
+  HERE @ - ,
+;
+
+: FIB
+  0 1
+  0
+  BEGIN
+    ROT
+    DUP ROT +
+    ROT ROT
+
+    1 +
+  DUP 4 PICK = UNTIL
+  DROP SWAP DROP SWAP DROP
+;
+
+S" 10 FIB = " TELL
+10 FIB .U
+S"  (Expected: 59)" TELL NEWLINE
+