Get rid of remaining "loose" syscalls
authorJonas Hvid <mail@johv.dk>
Mon, 9 Mar 2020 13:50:45 +0000 (14:50 +0100)
committerJonas Hvid <mail@johv.dk>
Mon, 9 Mar 2020 13:50:45 +0000 (14:50 +0100)
impl.asm
main.asm

index f6a25cf5b54d420cc016582e6f3d41e0661636a2..659d98ed7258b90b82e7a74061b6364948c15a8a 100644 (file)
--- a/impl.asm
+++ b/impl.asm
@@ -6,11 +6,9 @@ macro printlen msg, len {
   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
@@ -25,12 +23,6 @@ macro print msg {
   printlen msg, msg#.len
 }
 
-macro exit code {
-  mov rax, $3C
-  mov rdi, code
-  syscall
-}
-
 struc string bytes {
   . db bytes
   .len = $ - .
@@ -88,11 +80,8 @@ find:
 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], ' '
@@ -112,11 +101,8 @@ read_word:
   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
@@ -243,7 +229,7 @@ parse_number:
   pop rdi
   printlen rdi, [.length]
   newline
-  exit 100
+  sys_terminate 100
 
 segment readable writable
 
index a670b343e99c1a1f0154f0eab083ef6eb1e6cc1a..691e9f363d352fc95f351beabb2a30f05b14615a 100644 (file)
--- a/main.asm
+++ b/main.asm
@@ -28,6 +28,28 @@ macro sys_print_string {
   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
@@ -258,9 +280,7 @@ forth_asm TELL, 'TELL'
 
 ;; 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'
@@ -422,11 +442,8 @@ forth_asm READ_STRING, 'S"'
   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, '"'