;;
;; 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.
lea rcx, [rsp]
mov rdx, 1
- sys_print_string
+ call os_print_string
add rsp, 8
popr rax
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:
pop rdx ; Length
pop rcx ; Buffer
- sys_print_string
+ call os_print_string
popr rsi
popr rax
;; 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'
;; Print the buffer
mov rcx, .buffer
mov rdx, [.printed_length]
- sys_print_string
+ call os_print_string
;; Restore RSI and continue execution
pop rsi
mov [.length], 0
.read_char:
- mov rsi, .char_buffer
- sys_read_char
-
- mov al, [.char_buffer]
+ call os_read_char
cmp al, '"'
je .done
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
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