1 ; =============================================================================
2 ; BareMetal -- a 64-bit OS written in Assembly for x86-64 systems
3 ; Copyright (C) 2008-2012 Return Infinity -- see LICENSE.TXT
6 ; =============================================================================
13 ; -----------------------------------------------------------------------------
14 ; os_input_key_check -- Scans keyboard for input, but doesn't wait
16 ; OUT: AL = 0 if no key pressed, otherwise ASCII code, other regs preserved
17 ; Carry flag is set if there was a keystoke, clear if there was not
18 ; All other registers preserved
22 je os_input_key_check_no_key
23 mov byte [key
], 0x00 ; clear the variable as the keystroke is in AL now
24 stc ; set the carry flag
27 os_input_key_check_no_key:
28 xor al, al ; mov al, 0x00
29 clc ; clear the carry flag
31 ; -----------------------------------------------------------------------------
34 ; -----------------------------------------------------------------------------
35 ; os_input_key_wait -- Waits for keypress and returns key
37 ; OUT: AL = key pressed
38 ; All other registers preserved
43 mov byte [key
], 0x00 ; clear the variable as the keystroke is in AL now
45 ; -----------------------------------------------------------------------------
48 ; -----------------------------------------------------------------------------
49 ; os_input_string -- Take string from keyboard entry
50 ; IN: RDI = location where string will be stored
51 ; RCX = number of characters to accept
52 ; OUT: RCX = length of string that was inputed (NULL not counted)
53 ; All other registers preserved
56 push rdx
; Counter to keep track of max accepted characters
62 call os_input_key_check
63 jnc os_input_string_halt
; No key entered... halt until an interrupt is received
64 cmp al, 0x1C ; If Enter key pressed, finish
65 je os_input_string_done
66 cmp al, 0x0E ; Backspace
67 je os_input_string_backspace
68 cmp al, 32 ; In ASCII range (32 - 126)?
69 jl os_input_string_more
71 jg os_input_string_more
72 cmp rcx
, rdx
; Check if we have reached the max number of chars
73 je os_input_string_more
; Jump if we have (should beep as well)
74 stosb ; Store AL at RDI and increment RDI by 1
75 inc rcx
; Increment the couter
76 call os_print_char
; Display char
77 jmp os_input_string_more
79 os_input_string_backspace:
80 cmp rcx
, 0 ; backspace at the beginning? get a new char
81 je os_input_string_more
82 call os_dec_cursor
; Decrement the cursor
83 mov al, 0x20 ; 0x20 is the character for a space
84 call os_print_char
; Write over the last typed character with the space
85 call os_dec_cursor
; Decremnt the cursor again
86 dec rdi
; go back one in the string
87 mov byte [rdi
], 0x00 ; NULL out the char
88 dec rcx
; decrement the counter by one
89 jmp os_input_string_more
92 hlt ; Halt until another keystroke is received
93 jmp os_input_string_more
97 stosb ; We NULL terminate the string
103 ; -----------------------------------------------------------------------------
106 ; =============================================================================