push rsi
add rsp, 8
- mov rsi, msg
+ mov rcx, msg
mov rdx, len
- mov rax, 1
- mov rdi, 1
- syscall
+ sys_print_string
sub rsp, 8
pop rsi
printlen msg, msg#.len
}
-macro exit code {
- mov rax, $3C
- mov rdi, code
- syscall
-}
-
struc string bytes {
. db bytes
.len = $ - .
read_word:
.skip_whitespace:
;; Read characters into .char_buffer until one of them is not whitespace.
- mov rax, 0
- mov rdi, 0
mov rsi, .char_buffer
- mov rdx, 1
- syscall
+ sys_read_char
;; We consider newlines and spaces to be whitespace.
cmp [.char_buffer], ' '
mov [rsi], al
inc [.length]
- mov rax, 0
- mov rdi, 0
mov rsi, .char_buffer
- mov rdx, 1
- syscall
+ sys_read_char
cmp [.char_buffer], ' '
je .end
pop rdi
printlen rdi, [.length]
newline
- exit 100
+ sys_terminate 100
segment readable writable
syscall
}
+;; Read a character from the user into the given buffer.
+;;
+;; Input:
+;; - RSI = Character buffer
+;;
+;; Output:
+;; - BYTE [RSI] = Character
+;;
+;; Clobbers: RAX, RCX, R11, RDI, RSI, RDX
+macro sys_read_char {
+ mov rax, 0
+ mov rdi, 0
+ mov rdx, 1
+ syscall
+}
+
+macro sys_terminate code {
+ mov rax, $3C
+ mov rdi, code
+ syscall
+}
+
;; }}}
;; The code in this macro is placed at the end of each Forth word. When we are
;; Exit the program cleanly.
forth_asm TERMINATE, 'TERMINATE'
- mov rax, $3C
- mov rdi, 0
- syscall
+ sys_terminate 0
;; Duplicate a pair of elements.
forth_asm PAIRDUP, '2DUP'
mov [.length], 0
.read_char:
- mov rax, 0
- mov rdi, 0
mov rsi, .char_buffer
- mov rdx, 1
- syscall
+ sys_read_char
mov al, [.char_buffer]
cmp al, '"'