+forth_asm TERMINATE, 'TERMINATE'
+ sys_terminate 0
+
+;; 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
+
+forth_asm NOT_, 'NOT'
+ pop rax
+ cmp rax, 0
+ jz .false
+.true:
+ push 0
+ next
+.false:
+ push 1
+ next
+
+;; .U prints the value on the stack as an unsigned integer in hexadecimal.
+forth_asm DOTU, '.U'
+ mov [.length], 0
+ mov [.printed_length], 1
+ pop rax ; RAX = value to print
+ push rsi ; Save value of RSI
+
+ ;; We start by constructing the buffer to print in reverse
+
+.loop:
+ mov rdx, 0
+ mov rbx, $10
+ div rbx ; Put remainer in RDX and quotient in RAX
+
+ ;; Place the appropriate character in the buffer
+ mov rsi, .chars
+ add rsi, rdx
+ mov bl, [rsi]
+ mov rdi, .rbuffer
+ add rdi, [.length]
+ mov [rdi], bl
+ inc [.length]
+
+ ;; .printed_length is the number of characters that we ulitmately want to
+ ;; print. If we have printed a non-zero character, then we should update
+ ;; .printed_length.
+ cmp bl, '0'
+ je .skip_updating_real_length
+ mov rbx, [.length]
+ mov [.printed_length], rbx
+.skip_updating_real_length:
+
+ cmp [.length], 16
+ jle .loop
+
+ ;; Flip buffer around, since it is currently reversed
+ mov rcx, [.printed_length]
+.flip:
+ mov rsi, .rbuffer
+ add rsi, rcx
+ dec rsi
+ mov al, [rsi]
+
+ mov rdi, .buffer
+ add rdi, [.printed_length]
+ sub rdi, rcx
+ mov [rdi], al
+
+ loop .flip
+
+ ;; Print the buffer
+ mov rcx, .buffer
+ mov rdx, [.printed_length]
+ sys_print_string
+
+ ;; Restore RSI and continue execution
+ 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
+
+forth_asm PUT_BYTE, 'C!'
+ pop rbx
+ pop rax ; Value
+ mov [rbx], al
+ next
+
+forth_asm GET_BYTE, 'C@'
+ pop rax
+ movzx rax, byte [rax]
+ 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
+
+;; Given two integers a and b on the stack, pushes the quotient and remainder of
+;; division of a by b.
+forth_asm TIMESMOD, '/MOD'
+ pop rbx ; b
+ pop rax ; a
+ mov rdx, 0
+ div rbx
+ push rax ; a / b
+ push rdx ; a % b
+ next
+
+;; Read input until next " character is found. Push a string containing the
+;; input on the stack as (buffer length). Note that the buffer is only valid
+;; until the next call to S" and that no more than 255 characters can be read.
+forth_asm READ_STRING, 'S"'
+ ;; If the input buffer is set, we should read from there instead.
+ cmp [input_buffer], 0
+ jne read_string_buffer
+
+ push rsi
+
+ mov [.length], 0
+
+.read_char:
+ mov rsi, .char_buffer
+ sys_read_char
+
+ mov al, [.char_buffer]
+ cmp al, '"'
+ je .done
+
+ mov rdx, .buffer
+ add rdx, [.length]
+ mov [rdx], al
+ inc [.length]
+ jmp .read_char
+
+.done:
+ pop rsi
+
+ push .buffer
+ push [.length]
+
+ next
+
+read_string_buffer:
+ push rsi
+
+ ;; We borrow READ_STRING's buffer. They won't mind.
+ mov [READ_STRING.length], 0
+
+.read_char:
+ mov rbx, [input_buffer]
+ mov al, [rbx]
+ cmp al, '"'
+ je .done
+
+ mov rdx, READ_STRING.buffer
+ add rdx, [READ_STRING.length]
+ mov [rdx], al
+ inc [READ_STRING.length]
+
+ inc [input_buffer]
+ dec [input_buffer_length]
+
+ jmp .read_char
+
+.done:
+ pop rsi
+
+ ;; Skip closing "
+ inc [input_buffer]
+ dec [input_buffer_length]
+
+ push READ_STRING.buffer
+ push [READ_STRING.length]
+
+ next
+
+;; CREATE inserts a new header in the dictionary, and updates LATEST so that it
+;; points to the header. To compile a word, the user can then call ',' to
+;; continue to append data after the header.
+;;
+;; It takes the name of the word as a string (address length) on the stack.
+forth_asm CREATE, 'CREATE'
+ pop rcx ; Word string length
+ pop rdx ; Word string pointer
+
+ mov rdi, [here] ; rdi = Address at which to insert this entry
+ mov rax, [latest_entry] ; rax = Address of the previous entry
+ mov [rdi], rax ; Insert link to previous entry
+ mov [latest_entry], rdi ; Update LATEST to point to this word
+
+ add rdi, 8
+ mov [rdi], byte 0 ; Insert immediate flag
+
+ add rdi, 1
+ mov [rdi], byte cl ; Insert length
+
+ ;; Insert word string
+ add rdi, 1
+
+ push rsi
+ mov rsi, rdx ; rsi = Word string pointer
+ rep movsb
+ pop rsi
+
+ ;; Update HERE
+ mov [here], rdi
+
+ next
+
+forth_asm TICK, "'"
+ lodsq
+ 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 SYSCODE
+ dq INTERPRET_STRING
+ dq INTERPRET
+ dq BRANCH, -8 * 2
+ dq TERMINATE
+
+;; Built-in variables:
+
+forth STATE, 'STATE'
+ dq LIT, var_STATE