From 3e21454c3e1e006a38d2a05d856e4651d099b668 Mon Sep 17 00:00:00 2001 From: Jonas Hvid Date: Tue, 10 Dec 2019 16:58:14 +0100 Subject: [PATCH] Implement looping words and add fibonacci example --- main.asm | 28 ++++++++++++++++++++++++++++ sys.f | 26 ++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/main.asm b/main.asm index 086d1dc..cb2e0d5 100644 --- 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 cab2ab7..0996352 100644 --- 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 + -- 2.39.2