Implement ','
[rrq/jonasforth.git] / main.asm
index 183be74435f3b7091edd50c08ce92d3076434640..8af0955347ffb2f088ff8b65cc8bb531f4065649 100644 (file)
--- a/main.asm
+++ b/main.asm
@@ -366,12 +366,48 @@ forth_asm GET, '@'
   push rax
   next
 
+;; Add two integers on the stack.
+forth_asm PLUS, '+'
+  pop rax
+  pop rbx
+  add rax, rbx
+  push rax
+  next
+
+;; Calculate difference between two integers on the stack. The second number is
+;; subtracted from the first.
+forth_asm MINUS, '-'
+  pop rax
+  pop rbx
+  sub rbx, rax
+  push rbx
+  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
 
+;; Get the location of the LATEST variable. It can be set with '!' and read with
+;; '@'.
+forth LATEST, 'LATEST'
+  dq LIT, latest_entry
+  dq EXIT
+
+;; Get the location at which compiled words are expected to be added. This
+;; pointer is usually modified automatically when calling ',', but we can also
+;; read it manually with 'HERE'.
+forth HERE, 'HERE'
+  dq LIT, here
+  dq EXIT
+
+forth COMMA, ','
+  dq HERE, GET, PUT             ; Set the memory at the address pointed to by HERE
+  dq HERE, GET, LIT, 8, PLUS    ; Calculate new address for HERE to point to
+  dq HERE, PUT                  ; Update HERE to point to the new address
+  dq EXIT
+
 forth MAIN, 'MAIN'
   dq HELLO
   dq INTERPRET
@@ -380,6 +416,9 @@ forth MAIN, 'MAIN'
 
 segment readable writable
 
+;; The LATEST variable holds a pointer to the word that was last added to the
+;; dictionary. This pointer is updated as new words are added, and its value is
+;; used by FIND to look up words.
 latest_entry dq initial_latest_entry
 
 ;; The STATE variable is 0 when the interpreter is executing, and non-zero when
@@ -397,6 +436,10 @@ DOTU.rbuffer rq 16
 DOTU.length dq ?
 DOTU.printed_length dq ?
 
+;; Reserve space for compiled words, accessed through HERE.
+here dq here_top
+here_top rq $2000
+
 ;; Return stack
 rq $2000
 return_stack_top: