1 | ; Project name : Assembly Library |
---|
2 | ; Description : Functions for managing keyboard. |
---|
3 | |
---|
4 | BUFFER_SIZE_FOR_WORD_INPUT EQU 6 ; 5 chars + NULL |
---|
5 | |
---|
6 | ; Section containing code |
---|
7 | SECTION .text |
---|
8 | |
---|
9 | ;-------------------------------------------------------------------- |
---|
10 | ; Reads user inputted word. |
---|
11 | ; Function returns when ENTER or ESC will be pressed. |
---|
12 | ; |
---|
13 | ; Keyboard_ReadUserInputtedWordWhilePrinting |
---|
14 | ; Parameters |
---|
15 | ; BX: Numeric base (10 or 16) |
---|
16 | ; Returns: |
---|
17 | ; AX: User inputted word |
---|
18 | ; ZF: Set if user cancellation |
---|
19 | ; Corrupts registers: |
---|
20 | ; DX |
---|
21 | ;-------------------------------------------------------------------- |
---|
22 | ALIGN JUMP_ALIGN |
---|
23 | Keyboard_ReadUserInputtedWordWhilePrinting: |
---|
24 | push ds |
---|
25 | push si |
---|
26 | push cx |
---|
27 | |
---|
28 | mov cx, BUFFER_SIZE_FOR_WORD_INPUT |
---|
29 | call Memory_ReserveCXbytesFromStackToDSSI |
---|
30 | |
---|
31 | call Char_GetFilterFunctionToDXforNumericBaseInBX |
---|
32 | call Registers_ExchangeDSSIwithESDI |
---|
33 | call Keyboard_ReadUserInputtedStringToESDIWhilePrinting |
---|
34 | call Registers_ExchangeDSSIwithESDI ; Does not modify FLAGS |
---|
35 | jz SHORT .CancelledByUser |
---|
36 | |
---|
37 | call String_ConvertWordToAXfromStringInDSSIwithBaseInBX |
---|
38 | .CancelledByUser: |
---|
39 | add sp, BYTE BUFFER_SIZE_FOR_WORD_INPUT |
---|
40 | test cx, cx ; Set ZF if string length is zero |
---|
41 | pop cx |
---|
42 | pop si |
---|
43 | pop ds |
---|
44 | ret |
---|
45 | |
---|
46 | |
---|
47 | ;-------------------------------------------------------------------- |
---|
48 | ; Reads user inputted string to buffer. Character filter is |
---|
49 | ; supported to ignore unwanted characters. |
---|
50 | ; Function returns when ENTER or ESC will be pressed. |
---|
51 | ; |
---|
52 | ; Keyboard_ReadUserInputtedStringToESDIWhilePrinting |
---|
53 | ; Parameters: |
---|
54 | ; CX: Buffer size (with NULL) |
---|
55 | ; ES:DI: Ptr to destination buffer |
---|
56 | ; CS:DX: Ptr to character filter function: |
---|
57 | ; Parameters: |
---|
58 | ; AL: Character inputted by user |
---|
59 | ; Returns: |
---|
60 | ; CF: Set if character is accepted |
---|
61 | ; Cleared if character is rejected |
---|
62 | ; Corrupts registers: |
---|
63 | ; Nothing |
---|
64 | ; Returns: |
---|
65 | ; CX: String length in characters (without NULL) |
---|
66 | ; ZF: Set if user cancellation |
---|
67 | ; Corrupts registers: |
---|
68 | ; AX |
---|
69 | ;-------------------------------------------------------------------- |
---|
70 | ALIGN JUMP_ALIGN |
---|
71 | Keyboard_ReadUserInputtedStringToESDIWhilePrinting: |
---|
72 | push di |
---|
73 | push si |
---|
74 | push bx |
---|
75 | call .PrepareDisplayContextForKeyboardInput |
---|
76 | jcxz .ReturnAfterUpdatingZF |
---|
77 | |
---|
78 | xor bx, bx ; Zero character counter |
---|
79 | dec cx ; Decrement buffer size for NULL |
---|
80 | cld |
---|
81 | ALIGN JUMP_ALIGN |
---|
82 | .GetCharacterFromUser: |
---|
83 | call Keyboard_GetKeystrokeToAXandWaitIfNecessary ; Get ASCII to AL |
---|
84 | call .ProcessControlCharacter |
---|
85 | jz SHORT .TerminateStringWithNULL |
---|
86 | jc SHORT .PlayBellForRejectedCharacter |
---|
87 | call dx ; Filter character |
---|
88 | jnc SHORT .PlayBellForRejectedCharacter |
---|
89 | inc bx ; Increment number of characters stored |
---|
90 | stosb ; Store from AL to ES:DI |
---|
91 | call Keyboard_PrintInputtedCharacter |
---|
92 | loop .GetCharacterFromUser |
---|
93 | .PlayBellForRejectedCharacter: |
---|
94 | cmp al, BS ; No bell for backspace |
---|
95 | je SHORT .GetCharacterFromUser |
---|
96 | call Keyboard_PlayBellForUnwantedKeystroke |
---|
97 | jmp SHORT .GetCharacterFromUser |
---|
98 | |
---|
99 | .TerminateStringWithNULL: |
---|
100 | stosb ; Terminate string with NULL |
---|
101 | mov cx, bx ; String length now in CX |
---|
102 | |
---|
103 | .ReturnAfterUpdatingZF: |
---|
104 | CALL_DISPLAY_LIBRARY PopDisplayContext |
---|
105 | test cx, cx ; Clear or set ZF |
---|
106 | pop bx |
---|
107 | pop si |
---|
108 | pop di |
---|
109 | ret |
---|
110 | |
---|
111 | ;-------------------------------------------------------------------- |
---|
112 | ; .PrepareDisplayContextForKeyboardInput |
---|
113 | ; Parameters: |
---|
114 | ; Nothing |
---|
115 | ; Returns: |
---|
116 | ; Nothing (Display context pushed to stack) |
---|
117 | ; Corrupts registers: |
---|
118 | ; AX, BX, SI |
---|
119 | ;-------------------------------------------------------------------- |
---|
120 | ALIGN JUMP_ALIGN |
---|
121 | .PrepareDisplayContextForKeyboardInput: |
---|
122 | pop bx ; Pop return address to BX |
---|
123 | mov si, di |
---|
124 | |
---|
125 | CALL_DISPLAY_LIBRARY PushDisplayContext |
---|
126 | mov ax, CURSOR_NORMAL |
---|
127 | CALL_DISPLAY_LIBRARY SetCursorShapeFromAX |
---|
128 | CALL_DISPLAY_LIBRARY SynchronizeDisplayContextToHardware |
---|
129 | |
---|
130 | mov di, si |
---|
131 | jmp bx |
---|
132 | |
---|
133 | |
---|
134 | ;-------------------------------------------------------------------- |
---|
135 | ; .ProcessControlCharacter |
---|
136 | ; Parameters: |
---|
137 | ; AL: Character inputted by user |
---|
138 | ; CX: Number of bytes left in buffer |
---|
139 | ; BX: Total number of characters inputted |
---|
140 | ; ES:DI: Ptr where to store next character |
---|
141 | ; Returns: |
---|
142 | ; AL: Character inputted by user or NULL if end of input |
---|
143 | ; BX: Cleared if user cancellation |
---|
144 | ; ZF: Set if user has ended or cancelled key input |
---|
145 | ; CF: Set if character is rejected |
---|
146 | ; Corrupts registers: |
---|
147 | ; AH |
---|
148 | ;-------------------------------------------------------------------- |
---|
149 | ALIGN JUMP_ALIGN |
---|
150 | .ProcessControlCharacter: |
---|
151 | cmp al, CR ; ENTER to terminate string? |
---|
152 | je SHORT .EndCharacterInput |
---|
153 | cmp al, ESC ; Cancel input? |
---|
154 | je SHORT .CancelCharacterInput |
---|
155 | cmp al, BS ; Backspace? |
---|
156 | je SHORT .Backspace |
---|
157 | jcxz .RejectCharacter |
---|
158 | test al, al ; Clear ZF and CF |
---|
159 | ret |
---|
160 | |
---|
161 | .Backspace: |
---|
162 | test bx, bx ; At the beginning? |
---|
163 | jz SHORT .RejectCharacter |
---|
164 | inc cx ; Increment bytes left |
---|
165 | dec bx ; Decrement characters inputted |
---|
166 | dec di |
---|
167 | call Keyboard_PrintBackspace |
---|
168 | mov al, BS ; Restore character |
---|
169 | .RejectCharacter: |
---|
170 | test al, al ; Clear ZF... |
---|
171 | stc ; ...and set CF |
---|
172 | ret |
---|
173 | |
---|
174 | .CancelCharacterInput: |
---|
175 | xor bx, bx |
---|
176 | .EndCharacterInput: |
---|
177 | xor al, al ; Set ZF and clear CF |
---|
178 | ret |
---|
179 | |
---|
180 | |
---|
181 | ;-------------------------------------------------------------------- |
---|
182 | ; Keyboard_PrintBackspace |
---|
183 | ; Parameters: |
---|
184 | ; Nothing |
---|
185 | ; Returns: |
---|
186 | ; Nothing |
---|
187 | ; Corrupts registers: |
---|
188 | ; AX |
---|
189 | ;-------------------------------------------------------------------- |
---|
190 | ALIGN JUMP_ALIGN |
---|
191 | Keyboard_PrintBackspace: |
---|
192 | mov al, BS |
---|
193 | call Keyboard_PrintInputtedCharacter |
---|
194 | mov al, ' ' |
---|
195 | call Keyboard_PrintInputtedCharacter |
---|
196 | mov al, BS |
---|
197 | jmp SHORT Keyboard_PrintInputtedCharacter |
---|
198 | |
---|
199 | |
---|
200 | ;-------------------------------------------------------------------- |
---|
201 | ; Keyboard_PlayBellForUnwantedKeystroke |
---|
202 | ; Parameters: |
---|
203 | ; Nothing |
---|
204 | ; Returns: |
---|
205 | ; Nothing |
---|
206 | ; Corrupts registers: |
---|
207 | ; AX |
---|
208 | ;-------------------------------------------------------------------- |
---|
209 | ALIGN JUMP_ALIGN |
---|
210 | Keyboard_PlayBellForUnwantedKeystroke: |
---|
211 | mov al, BELL |
---|
212 | ; Fall to Keyboard_PrintInputtedCharacter |
---|
213 | |
---|
214 | ;-------------------------------------------------------------------- |
---|
215 | ; Keyboard_PrintInputtedCharacter |
---|
216 | ; Parameters: |
---|
217 | ; AL: Character inputted by user |
---|
218 | ; Returns: |
---|
219 | ; Nothing |
---|
220 | ; Corrupts registers: |
---|
221 | ; AX |
---|
222 | ;-------------------------------------------------------------------- |
---|
223 | ALIGN JUMP_ALIGN |
---|
224 | Keyboard_PrintInputtedCharacter: |
---|
225 | push di |
---|
226 | CALL_DISPLAY_LIBRARY PrintCharacterFromAL |
---|
227 | CALL_DISPLAY_LIBRARY SynchronizeDisplayContextToHardware ; Hardware cursor |
---|
228 | pop di |
---|
229 | ret |
---|
230 | |
---|
231 | |
---|
232 | ;-------------------------------------------------------------------- |
---|
233 | ; Keyboard_RemoveAllKeystrokesFromBuffer |
---|
234 | ; Parameters: |
---|
235 | ; Nothing |
---|
236 | ; Returns: |
---|
237 | ; Nothing |
---|
238 | ; Corrupts registers: |
---|
239 | ; AX |
---|
240 | ;-------------------------------------------------------------------- |
---|
241 | ALIGN JUMP_ALIGN |
---|
242 | Keyboard_RemoveAllKeystrokesFromBuffer: |
---|
243 | call Keyboard_GetKeystrokeToAX |
---|
244 | jnz SHORT Keyboard_RemoveAllKeystrokesFromBuffer |
---|
245 | ret |
---|
246 | |
---|
247 | |
---|
248 | ;-------------------------------------------------------------------- |
---|
249 | ; Keyboard_GetKeystrokeToAX |
---|
250 | ; Keyboard_GetKeystrokeToAXandLeaveItToBuffer |
---|
251 | ; Keyboard_GetKeystrokeToAXandWaitIfNecessary |
---|
252 | ; Parameters: |
---|
253 | ; Nothing |
---|
254 | ; Returns: |
---|
255 | ; AL: ASCII character (if keystroke available) |
---|
256 | ; AH: BIOS scan code (if keystroke available) |
---|
257 | ; ZF: Set if no keystroke available |
---|
258 | ; Cleared if keystroke available |
---|
259 | ; Corrupts registers: |
---|
260 | ; Nothing |
---|
261 | ;-------------------------------------------------------------------- |
---|
262 | ALIGN JUMP_ALIGN |
---|
263 | Keyboard_GetKeystrokeToAX: |
---|
264 | call Keyboard_GetKeystrokeToAXandLeaveItToBuffer |
---|
265 | jnz SHORT Keyboard_GetKeystrokeToAXandWaitIfNecessary |
---|
266 | ret |
---|
267 | ALIGN JUMP_ALIGN |
---|
268 | Keyboard_GetKeystrokeToAXandLeaveItToBuffer: |
---|
269 | mov ah, CHECK_FOR_KEYSTROKE |
---|
270 | int BIOS_KEYBOARD_INTERRUPT_16h |
---|
271 | ret |
---|
272 | ALIGN JUMP_ALIGN |
---|
273 | Keyboard_GetKeystrokeToAXandWaitIfNecessary: |
---|
274 | xor ah, ah ; GET_KEYSTROKE |
---|
275 | int BIOS_KEYBOARD_INTERRUPT_16h |
---|
276 | test ax, ax ; Clear ZF |
---|
277 | ret |
---|