-
Notifications
You must be signed in to change notification settings - Fork 1
/
inout.inc
313 lines (203 loc) · 4.9 KB
/
inout.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
; This file contains MACROs fo preparing parameters and CALL the real Procedures in inout.asm
; These MACROs should be called instead of the Procedures in inout.asm
; THese procedures are external
; MUST be declared at another assembly file with same name,type
; so that the linker join them
EXTRN setCursorPosition:NEAR
EXTRN getCursorPosition:NEAR
EXTRN getPressedKey:NEAR
EXTRN isKeyPressed:NEAR
EXTRN clearKeyboardBuffer:NEAR
EXTRN isLetter:NEAR
EXTRN readString:NEAR
EXTRN printString:NEAR
EXTRN printChar:NEAR
EXTRN printNumber:NEAR
EXTRN clearCharacters:NEAR
EXTRN switchToTextMode:NEAR
EXTRN switchToGraphicsMode:NEAR
EXTRN openFile:NEAR
EXTRN loadImageData:NEAR
EXTRN closeFile:NEAR
EXTRN getSystemTime:NEAR
EXTRN delayInSeconds:NEAR
EXTRN time_seconds:BYTE
callSetCursorPosition MACRO x , y
PUSHA
MOV DL , x
MOV DH , y
CALL setCursorPosition
POPA
ENDM callSetCursorPosition
; @Returns x -> DL
; @Returns y -> DH
callGetCursorPosition MACRO
PUSH BX
PUSH AX
CALL getCursorPosition
POP AX
POP BX
ENDM callGetCursorPosition
callWaitForAnyKey MACRO
PUSH AX
CALL getPressedKey
POP AX
ENDM callWaitForAnyKey
; return AH -> scan code , AL -> ASCII code
callGetPressedKey MACRO
CALL getPressedKey
ENDM callGetPressedKey
; @Return answer in Zero flag
; Zero Flag = 0 -> true , Zero Flag = 1 -> false
; JNZ -> true , JZ -> false
; return AH -> scan code , AL -> ASCII code
callIsKeyPressed MACRO
CALL isKeyPressed
ENDM callIsKeyPressed
callClearKeyboardBuffer MACRO
PUSH AX
CALL clearKeyboardBuffer
POP AX
ENDM callClearKeyboardBuffer
; @Return result in carry flag
; Carry = 1 -> true , Carry = 0 -> false
; JC -> true , JNC -> false
callIsLetter MACRO letter
PUSH AX
MOV AL , letter
CALL isLetter
POP AX
ENDM callIsLetter
; inputBuffer should have 2 bytes before it determining the size of buffer
; inputBuffer offsets:
; inputBuffer-2 -> max bytes to read including Enter
; inputBuffer-1 -> @return the actual number of characters read
; inputBuffer -> the input string
; returned string is saved at memory location of inputBuffer
callReadString MACRO inputBuffer
PUSHA
LEA DX , inputBuffer
SUB DX , 2
CALL readString
POPA
ENDM callReadString
; string must be a memory location terminated with '$'
; string is printed at the current cursor position
callPrintString MACRO string
PUSH AX
PUSH DX
LEA DX , string
CALL printString
POP DX
POP AX
ENDM callPrintString
; character is printed at the current cursor position
callPrintChar MACRO char
PUSH AX
PUSH DX
MOV DL , char
CALL printChar
POP DX
POP AX
ENDM callPrintChar
; number is assumed as byte/word variable/register
; number is printed at the current cursor position
callPrintNumber MACRO number
PUSHA
MOVZX EAX , number
CALL printNumber
POPA
ENDM callPrintNumber
; Clear number of characters to reprint over them without overlapping
; i.e. printing spaces to clear the screen
; Note: it does not change the cursor position
callClearCharacters MACRO numberOfChars
PUSHA
MOV CH , 0
MOV CL , numberOfChars
CALL clearCharacters
POPA
ENDM callClearCharacters
callSwitchToTextMode MACRO
PUSH AX
CALL switchToTextMode
POP AX
ENDM callSwitchToTextMode
callSwitchToGraphicsMode MACRO
PUSH AX
CALL switchToGraphicsMode
POP AX
ENDM callSwitchToGraphicsMode
; open file from the device with filename
; @ returns the output pointer to this file in fileHandle
callOpenFile MACRO filename , fileHandle
PUSHA
LEA DX , Filename
LEA SI , fileHandle
CALL openFile
POPA
ENDM callOpenFile
; Loads data from image with its fileHandle and save them in imageData
; dimensions of the image is 16px * 16px
; imageData is assumed an array with available space to store the data (16 * 16 bytes)
callLoadImageData MACRO fileHandle , imageData
PUSHA
MOV BX , [fileHandle]
LEA DX , imageData
CALL loadImageData
POPA
ENDM callLoadImageData
; Closes the opened file with its corresponding fileHandle
callCloseFile MACRO fileHandle
PUSHA
LEA SI , fileHandle
CALL closeFile
POPA
ENDM callCloseFile
callGetSystemTime MACRO
PUSHA
CALL getSystemTime
POPA
ENDM callGetSystemTime
callDelayInSeconds MACRO seconds
PUSHA
MOV CH , 0
MOV CL , seconds
CALL delayInSeconds
POPA
ENDM callDelayInSeconds
; Memory Macros
; Same interface as Assembly instructions but for memory to memory operations
MOV_MEMORY_WORD MACRO destination , source
PUSH source
POP destination
ENDM MOV_MEMORY_WORD
MOV_MEMORY_BYTE MACRO destination , source
PUSH AX
MOV AL , source
MOV destination , AL
POP AX
ENDM MOV_MEMORY_BYTE
XCHG_MEMORY_WORD MACRO var_1 , var_2
PUSH var_1
PUSH var_2
POP var_1
POP var_2
ENDM XCHG_MEMORY_WORD
XCHG_MEMORY_BYTE MACRO var_1 , var_2
PUSH AX
MOV AL , var_1
MOV AH , var_2
MOV var_1 , AH
MOV var_2 , AL
POP AX
ENDM XCHG_MEMORY_BYTE
CMP_MEMORY MACRO var_1 , var_2
PUSH EAX
PUSH EBX
MOVZX EAX , var_1
MOVZX EBX , var_2
SUB EAX , EBX
POP EBX
POP EAX
ENDM CMP_MEMORY