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:
popr rsi
next
+;; Prints a newline to standard output.
NEWLINE:
dq docol
dq LIT, $A
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
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
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
READ_WORD.length db ?
READ_WORD.char_buffer db ?
-
;; Return stack
rq $2000
return_stack_top: