This repository has been archived by the owner on Apr 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdio.inc
244 lines (221 loc) · 4.08 KB
/
stdio.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
;
; This file contains routines for text i/o
; (c) Dmytro Sirenko, 2010
; ____________________________________________________________________
%ifndef __STDIO_INC_
%define __STDIO_INC_
%include "misc.inc"
%include "msgs.inc"
[section .text]
; ___________________________________________________________________
press_any:
; show "Press any key" message and waits for it
mov si, m_press_any
call print_string
call wait_key
ret
; ___________________________________________________________________
print_char:
print_ascii_al:
mov ah, 0x0E
mov bh, 0x00
int 0x10
retn
; ___________________________________________________________________
print_endl:
mov ax, 0x0E0D
mov bh, 0x00
int 0x10
mov al, 0x0A
int 0x10
retn
; ___________________________________________________________________
print_string:
print_ds_si:
; displays string in ds:si
; args: si - offset of a string to display
pusha
mov ah, 0x0E
movzx bx, byte [text_attr]
.iter:
mov al, [si]
and al, al
jz .done
int 0x10
lodsb
jmp .iter
.done: popa
retn
print_ds_dx:
pusha
mov si, dx
mov ah, 0x0E
movzx bx, byte [text_attr]
.iter:
mov al, [si]
and al, al
jz .done
cmp al, '$'
je .done
int 0x10
lodsb
jmp .iter
.done: popa
retn
; ___________________________________________________________________
read_string_dos:
;; implements DOS 0Ah function behavior;
retn
; ___________________________________________________________________
read_string:
;; args:
;; ds:dx - address of a buffer up to 255 symbols, first symbol is
;; maximum count of symbols
pusha
mov cx, 0
mov di, dx
mov si, dx ; store it
.type_loop:
call read_char
;; is valid ascii
cmp al, 0x20
jl .char_skipped
test al, 0x80
jnz .char_skipped
stosb
; echo
mov ah, 0x0E
mov bh, 0x00
int 0x10
jmp .type_loop
.char_skipped:
.bksp: cmp al, 0x08
jne .enter
; remove last
mov word [di], 0
; is the first?
cmp di, si
jle .type_loop
dec di
; clear symbols
mov ax, 0x0E08
xor bh, bh
int 0x10
mov al, ' '
int 0x10
mov al, 0x08
int 0x10
jmp .type_loop
.escape:
;
jmp .type_loop
.enter:
cmp al, 0x0D
jne .type_loop
; push the end if line into si
mov al, 0
stosb
; print end of line
call print_endl
popa
retn
; ___________________________________________________________________
print_ascii_dl:
mov al, dl
mov ah, 0x0E
mov bh, 0x00
int 0x10
retn
; ___________________________________________________________________
wait_key:
read_char:
; waits for a key press, returns it
; args: none
; out: al - ASCII-code, 0, or scan-code prefix
;;; mov ah, 10h ; 101/102 keys
;;; int 16h ; - this stinking interrupt loads 95% CPU!
;int 0x4A
.loop:
mov ax, 20
call delay
mov ah, 11h
int 16h
jz .loop
;; clear keyboard buffer
push ds
push 0
pop ds
mov dx, word [041Ah]
mov word [041Ch], dx ; clear buffer
;; process some specific shortcuts
;call shortcuts
pop ds
retn
; ___________________________________________________________________
print_hex_digit_al:
and al, 0x0F
movzx si, al
add si, .digits
mov al, byte [si]
mov ah, 0x0E
mov bh, 0x00
int 0x10
retn
.digits db "0123456789ABCDEF"
; ___________________________________________________________________
print_hex_dl:
;; args:
;; dl - hexadecimal number to output
pusha
mov al, dl
shr al, 4
call print_hex_digit_al
mov al, dl
and al, 0x0F
call print_hex_digit_al
popa
retn
; ___________________________________________________________________
print_hex_dx:
;; args:
;; dx - hexadecimal number to output
pusha
xchg dl, dh
call print_hex_dl
mov dl, dh
call print_hex_dl
popa
retn
; ___________________________________________________________________
read_hex_dx:
;; args:
;; es:di - address of asciiz string in memory
;; rets:
;; dx - hexadecimal number in [es:di]
retn
; ___________________________________________________________________
ascii_to_hex_digit_dl:
cmp dl, '0'
jl .fail
cmp dl, '9'
jg .next
sub dl, '0'
retn
.next:
cmp dl, 'A'
jl .fail
cmp dl, 'F'
jg .next1
sub dl, 'A' - 10
retn
.next1: cmp dl, 'a'
jl .fail
cmp dl, 'f'
jg .fail
sub dl, 'a' - 10
retn
.fail: mov dl, 0xFF
retn
[section .data]
text_attr db 0x07 ;; default
%endif ;; __STDIO_INC_