X-Git-Url: https://git.rrq.au/?a=blobdiff_plain;f=main.asm;h=183be74435f3b7091edd50c08ce92d3076434640;hb=412aa1087846d432ddc5bb93c3dcb2acf292010c;hp=21a911cf164675d6dba7fc201559d3c77fdb43a3;hpb=2cd770b4f9a46cbc7ff07b26f4851bba1ea36a7d;p=rrq%2Fjonasforth.git diff --git a/main.asm b/main.asm index 21a911c..183be74 100644 --- a/main.asm +++ b/main.asm @@ -242,6 +242,55 @@ forth HELLO, 'HELLO' dq NEWLINE dq EXIT +;; Duplicate a pair of elements. +forth_asm PAIRDUP, '2DUP' + pop rbx + pop rax + push rax + push rbx + push rax + push rbx + next + +;; Swap the top two elements on the stack. +forth_asm SWAP, 'SWAP' + pop rax + pop rbx + push rax + push rbx + next + +;; Remove the top element from the stack. +forth_asm DROP, 'DROP' + add rsp, 8 + next + +;; The INTERPRET word reads and interprets user input. It's behavior depends on +;; the current STATE. It provides special handling for integers. (TODO) +forth INTERPRET, 'INTERPRET' + ;; Read word + dq READ_WORD + dq PAIRDUP + ;; Stack is (word length word length). + dq FIND ; Try to find word + dq DUP_ + dq ZBRANCH, 8 * 8 ; Check if word is found + + ;; Word is found, execute it + dq TCFA + ;; Stack is (word length addr) + dq SWAP, DROP + dq SWAP, DROP + ;; Stack is (addr) + dq EXEC + dq EXIT + + ;; No word is found, assume it is an integer literal + ;; Stack is (word length addr) + dq DROP + dq PARSE_NUMBER + dq EXIT + ;; .U prints the value on the stack as an unsigned integer in hexadecimal. forth_asm DOTU, '.U' mov [.length], 0 @@ -303,16 +352,40 @@ forth_asm DOTU, '.U' pop rsi next +;; Takes a value and an address, and stores the value at the given address. +forth_asm PUT, '!' + pop rbx ; Address + pop rax ; Value + mov [rbx], rax + next + +;; Takes an address and returns the value at the given address. +forth_asm GET, '@' + pop rax + mov rax, [rax] + push rax + next + +;; Get the location of the STATE variable. It can be set with '!' and read with +;; '@'. +forth STATE, 'STATE' + dq LIT, var_STATE + dq EXIT + forth MAIN, 'MAIN' dq HELLO - dq READ_WORD, FIND, TCFA, EXEC - dq BRANCH, -8 * 5 + dq INTERPRET + dq BRANCH, -8 * 2 dq TERMINATE segment readable writable latest_entry dq initial_latest_entry +;; The STATE variable is 0 when the interpreter is executing, and non-zero when +;; it is compiling. +var_STATE dq 0 + FIND.rsi dq ? READ_WORD.rsi dq ?