Rename TYPE to TELL
[rrq/jonasforth.git] / main.asm
index 049cb13e8b50df3ca0d56c7bdbc01fd5a19ba7df..4ddb26785e04a419d221f986de7c3ac223321f2a 100644 (file)
--- a/main.asm
+++ b/main.asm
@@ -59,6 +59,24 @@ LIT:
   push rax
   next
 
+;; 0BRANCH is the fundamental mechanism for branching. If the top of the stack
+;; is zero, we jump by the given offset. 0BRANCH is given the offset as an
+;; integer after the word.
+ZBRANCH:
+  dq .start
+.start:
+  ;; Compare top of stack to see if we should branch
+  pop rax
+  cmp rax, 0
+  jnz .dont_branch
+.do_branch:
+  add rsi, [rsi] ; [RSI], which is the next word, contains the offset; we add this to the instruction pointer.
+  next           ; Then, we can just continue execution as normal
+.dont_branch:
+  add rsi, 8     ; We need to skip over the next word, which contains the offset.
+  next
+
+;; Expects a character on the stack and prints it to standard output.
 EMIT:
   dq .start
 .start:
@@ -74,6 +92,7 @@ EMIT:
   popr rsi
   next
 
+;; Prints a newline to standard output.
 NEWLINE:
   dq docol
   dq LIT, $A
@@ -134,7 +153,9 @@ READ_WORD:  ; 400170
 
   next
 
-TYPE:
+;; Takes a string (in the form of a pointer and a length on the stack) and
+;; prints it to standard output.
+TELL:
   dq .start
 .start:
   mov rbx, rsi
@@ -150,6 +171,14 @@ TYPE:
   mov rsi, rbx
   next
 
+;; Exit the program cleanly.
+TERMINATE:
+  dq .start
+.start:
+  mov rax, $3C
+  mov rdi, 0
+  syscall
+
 PUSH_HELLO_CHARS:
   dq docol
   dq LIT, $A
@@ -178,21 +207,14 @@ HELLO:
   dq NEWLINE
   dq EXIT
 
-TERMINATE:
-  dq .start
-.start:
-  mov rax, $3C
-  mov rdi, 0
-  syscall
-
 MAIN:
   dq docol
   dq HELLO
   dq READ_WORD
   dq LIT, you_typed_string
   dq LIT, you_typed_string.length
-  dq TYPE
-  dq TYPE
+  dq TELL
+  dq TELL
   dq NEWLINE
   dq HELLO
   dq TERMINATE
@@ -209,7 +231,6 @@ READ_WORD.buffer rb READ_WORD.max_size
 READ_WORD.length db ?
 READ_WORD.char_buffer db ?
 
-
 ;; Return stack
 rq $2000
 return_stack_top: