Clean up OS interface
[rrq/jonasforth.git] / main.asm
index 5985b997af8095926394802fd7e6c59b636a5510..407d3a30a9baad7bbd6b05b9818d50d29cf889b6 100644 (file)
--- a/main.asm
+++ b/main.asm
 ;;
 ;; os_read_char
 ;;   Wait for the user to type a key, and then put the corresponding ASCII byte
-;;   into the buffer pointed to by RCX.
+;;   into RAX.
 ;;
 ;; os_terminate
-;;   Shut down the system.
+;;   Shut down the system, returning the error code given in RAX.
 include '%OS_INCLUDE%'
 
-;; Print a string of a given length.
-;;
-;; Input:
-;; - RCX = Pointer to buffer
-;; - RDX = Buffer length
-;;
-;; Clobbers: RAX, RCX, R11, RDI, RSI
-macro sys_print_string {
-  push r8
-  push r9
-  push r10
-
-  call os_print_string
-
-  pop r10
-  pop r9
-  pop r8
-}
-
-;; 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 {
-  push rbx
-  push r8
-  push r9
-  push r10
-  push r15
-
-  mov rcx, rsi
-  call os_read_char
-
-  pop r15
-  pop r10
-  pop r9
-  pop r8
-  pop rbx
-}
-
-macro sys_terminate code {
-  mov rax, code
-  call os_terminate
-}
-
 ;; The code in this macro is placed at the end of each Forth word. When we are
 ;; executing a definition, this code is what causes execution to resume at the
 ;; next word in that definition.
@@ -240,7 +190,7 @@ forth_asm EMIT, 'EMIT'
 
   lea rcx, [rsp]
   mov rdx, 1
-  sys_print_string
+  call os_print_string
 
   add rsp, 8
   popr rax
@@ -265,12 +215,7 @@ forth_asm KEY, 'KEY'
   jne .from_buffer
 
   ;; Reading user input
-  push rsi
-  mov rsi, .buffer
-  sys_read_char
-  pop rsi
-
-  movzx rax, byte [.buffer]
+  call os_read_char
   ret
 
 .from_buffer:
@@ -341,7 +286,7 @@ forth_asm TELL, 'TELL'
 
   pop rdx ; Length
   pop rcx ; Buffer
-  sys_print_string
+  call os_print_string
 
   popr rsi
   popr rax
@@ -349,7 +294,8 @@ forth_asm TELL, 'TELL'
 
 ;; Exit the program cleanly.
 forth_asm TERMINATE, 'TERMINATE'
-  sys_terminate 0
+  mov rax, 0
+  call os_terminate
 
 ;; Duplicate a pair of elements.
 forth_asm PAIRDUP, '2DUP'
@@ -438,7 +384,7 @@ forth_asm DOTU, '.U'
   ;; Print the buffer
   mov rcx, .buffer
   mov rdx, [.printed_length]
-  sys_print_string
+  call os_print_string
 
   ;; Restore RSI and continue execution
   pop rsi
@@ -511,10 +457,7 @@ forth_asm READ_STRING, 'S"'
   mov [.length], 0
 
 .read_char:
-  mov rsi, .char_buffer
-  sys_read_char
-
-  mov al, [.char_buffer]
+  call os_read_char
   cmp al, '"'
   je .done