* remove "\r" nonsense
[mascara-docs.git] / amd64 / bareMetalOS-0.5.3 / os / syscalls / input.asm
blob7fe0c19142575827b1b2f75e3ee00f7db60cf43c
1 ; =============================================================================
2 ; BareMetal -- a 64-bit OS written in Assembly for x86-64 systems
3 ; Copyright (C) 2008-2012 Return Infinity -- see LICENSE.TXT
5 ; Input Functions
6 ; =============================================================================
8 align 16
9 db 'DEBUG: INPUT '
10 align 16
13 ; -----------------------------------------------------------------------------
14 ; os_input_key_check -- Scans keyboard for input, but doesn't wait
15 ; IN: Nothing
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
19 os_input_key_check:
20 mov al, [key]
21 cmp al, 0
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
25 ret
27 os_input_key_check_no_key:
28 xor al, al ; mov al, 0x00
29 clc ; clear the carry flag
30 ret
31 ; -----------------------------------------------------------------------------
34 ; -----------------------------------------------------------------------------
35 ; os_input_key_wait -- Waits for keypress and returns key
36 ; IN: Nothing
37 ; OUT: AL = key pressed
38 ; All other registers preserved
39 os_input_key_wait:
40 mov al, [key]
41 cmp al, 0
42 je os_input_key_wait
43 mov byte [key], 0x00 ; clear the variable as the keystroke is in AL now
44 ret
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
54 os_input_string:
55 push rdi
56 push rdx ; Counter to keep track of max accepted characters
57 push rax
59 mov rdx, rcx
60 xor rcx, rcx
61 os_input_string_more:
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
70 cmp al, 126
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
91 os_input_string_halt:
92 hlt ; Halt until another keystroke is received
93 jmp os_input_string_more
95 os_input_string_done:
96 mov al, 0x00
97 stosb ; We NULL terminate the string
99 pop rax
100 pop rdx
101 pop rdi
103 ; -----------------------------------------------------------------------------
106 ; =============================================================================
107 ; EOF