From d8a61fe84d4b71c21c4ce18a9b7bc78000db725a Mon Sep 17 00:00:00 2001 From: Jonas Hvid Date: Sun, 4 Oct 2020 02:02:42 +0200 Subject: [PATCH] Clean up OS interface We got rid of those weird macros that would try to preserve a bunch of registers for no reason. The OS interface was changes slightly to allow for simpler implementation and usage. --- impl.asm | 5 ++-- main.asm | 75 +++++++---------------------------------------------- os/uefi.asm | 11 ++++---- 3 files changed, 18 insertions(+), 73 deletions(-) diff --git a/impl.asm b/impl.asm index e8cd883..dd7fc9d 100644 --- a/impl.asm +++ b/impl.asm @@ -8,7 +8,7 @@ macro printlen msg, len { mov rcx, msg mov rdx, len - sys_print_string + call os_print_string sub rsp, 8 pop rsi @@ -183,7 +183,8 @@ parse_number: pop rdi printlen rdi, [.length] newline - sys_terminate 100 + mov rax, 100 + call os_terminate section '.data' readable writable diff --git a/main.asm b/main.asm index 5985b99..407d3a3 100644 --- a/main.asm +++ b/main.asm @@ -15,62 +15,12 @@ ;; ;; 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 diff --git a/os/uefi.asm b/os/uefi.asm index 4f8ce56..16fc85d 100644 --- a/os/uefi.asm +++ b/os/uefi.asm @@ -129,7 +129,6 @@ os_print_string: ret os_read_char: - mov r15, rcx .read_key: mov rcx, [system_table] ; EFI_SYSTEM_TABLE* rcx mov rcx, [rcx + EFI_SYSTEM_TABLE.ConIn] ; EFI_SIMPLE_TEXT_INPUT_PROTOCOL* rcx @@ -143,19 +142,21 @@ os_read_char: cmp rax, r8 je .read_key - mov ax, [input_key.UnicodeChar] - mov [r15], al + movzx rax, word [input_key.UnicodeChar] ;; Special handling of enter (UEFI gives us '\r', but we want '\n'.) cmp ax, $D jne .no_enter - mov byte [r15], $A + mov al, $A .no_enter: + push rax ;; Print the character - mov rcx, r15 + mov [char_buffer], al + mov rcx, char_buffer mov rdx, 1 call os_print_string + pop rax ret -- 2.39.2