1 | ; File name : DisplayFormat.asm |
---|
2 | ; Project name : Assembly Library |
---|
3 | ; Created date : 29.6.2010 |
---|
4 | ; Last update : 8.10.2010 |
---|
5 | ; Author : Tomi Tilli |
---|
6 | ; Description : Functions for displaying formatted strings. |
---|
7 | |
---|
8 | ; Section containing code |
---|
9 | SECTION .text |
---|
10 | |
---|
11 | ;-------------------------------------------------------------------- |
---|
12 | ; DisplayFormat_ParseCharacters |
---|
13 | ; Parameters: |
---|
14 | ; DS: BDA segment (zero) |
---|
15 | ; SS:BP: Pointer to first format parameter (-=2 updates to next parameter) |
---|
16 | ; CS:SI: Pointer to string to format |
---|
17 | ; ES:DI: Ptr to cursor location in video RAM |
---|
18 | ; Returns: |
---|
19 | ; CS:SI: Ptr to end of format string (ptr to one past NULL) |
---|
20 | ; DI: Updated offset to video RAM |
---|
21 | ; Corrupts registers: |
---|
22 | ; AX, BX, CX, DX, BP |
---|
23 | ;-------------------------------------------------------------------- |
---|
24 | ALIGN JUMP_ALIGN |
---|
25 | DisplayFormat_ParseCharacters: |
---|
26 | call ReadCharacterAndTestForNull |
---|
27 | jz SHORT .QuitCharacterParsing |
---|
28 | |
---|
29 | ePUSH_T cx, DisplayFormat_ParseCharacters ; Return address |
---|
30 | xor cx, cx ; Initial placeholder size |
---|
31 | cmp al, '%' ; Format specifier? |
---|
32 | je SHORT ParseFormatSpecifier |
---|
33 | jmp DisplayPrint_CharacterFromAL |
---|
34 | |
---|
35 | ALIGN JUMP_ALIGN |
---|
36 | .QuitCharacterParsing: |
---|
37 | ret |
---|
38 | |
---|
39 | |
---|
40 | ;-------------------------------------------------------------------- |
---|
41 | ; ParseFormatSpecifier |
---|
42 | ; Parameters: |
---|
43 | ; CX: Placeholder size |
---|
44 | ; DS: BDA segment (zero) |
---|
45 | ; SS:BP: Pointer to first format parameter (-=2 for next parameter) |
---|
46 | ; CS:SI: Pointer to string to format |
---|
47 | ; ES:DI: Ptr to cursor location in video RAM |
---|
48 | ; Returns: |
---|
49 | ; SI: Updated to first unparsed character |
---|
50 | ; DI: Updated offset to video RAM |
---|
51 | ; BP: Updated to next format parameter |
---|
52 | ; Corrupts registers: |
---|
53 | ; AX, BX, CX, DX |
---|
54 | ;-------------------------------------------------------------------- |
---|
55 | ALIGN JUMP_ALIGN |
---|
56 | ParseFormatSpecifier: |
---|
57 | call ReadCharacterAndTestForNull |
---|
58 | call Char_IsDecimalDigitInAL |
---|
59 | jc SHORT .ParsePlaceholderSizeDigitFromALtoCX |
---|
60 | call GetFormatSpecifierParserToAX |
---|
61 | call ax ; Parser function |
---|
62 | dec bp |
---|
63 | dec bp ; SS:BP now points to next parameter |
---|
64 | test cx, cx |
---|
65 | jnz SHORT PrependOrAppendSpaces |
---|
66 | ret |
---|
67 | |
---|
68 | ;-------------------------------------------------------------------- |
---|
69 | ; .ParsePlaceholderSizeDigitFromALtoCX |
---|
70 | ; Parameters: |
---|
71 | ; AL: Digit character from format string |
---|
72 | ; CX: Current placeholder size |
---|
73 | ; DS: BDA segment (zero) |
---|
74 | ; Returns: |
---|
75 | ; CX: Current placeholder size |
---|
76 | ; Jumps back to ParseFormatSpecifier |
---|
77 | ; Corrupts registers: |
---|
78 | ; AX |
---|
79 | ;-------------------------------------------------------------------- |
---|
80 | ALIGN JUMP_ALIGN |
---|
81 | .ParsePlaceholderSizeDigitFromALtoCX: |
---|
82 | mov [VIDEO_BDA.displayContext+DISPLAY_CONTEXT.fpCursorPosition], di |
---|
83 | sub al, '0' ; Digit '0'...'9' to integer 0...9 |
---|
84 | mov ah, cl ; Previous number parameter to AH |
---|
85 | aad ; AL += (AH * 10) |
---|
86 | mov cl, al ; Updated number parameter now in CX |
---|
87 | jmp SHORT ParseFormatSpecifier |
---|
88 | |
---|
89 | |
---|
90 | ;-------------------------------------------------------------------- |
---|
91 | ; ReadCharacterAndTestForNull |
---|
92 | ; Parameters: |
---|
93 | ; CS:SI: Pointer next character from string |
---|
94 | ; Returns: |
---|
95 | ; AL: Character from string |
---|
96 | ; SI: Incremented to next character |
---|
97 | ; ZF: Set if NULL, cleared if valid character |
---|
98 | ; Corrupts registers: |
---|
99 | ; Nothing |
---|
100 | ;-------------------------------------------------------------------- |
---|
101 | ALIGN JUMP_ALIGN |
---|
102 | ReadCharacterAndTestForNull: |
---|
103 | eSEG cs |
---|
104 | lodsb ; Load from CS:SI to AL |
---|
105 | test al, al ; NULL to end string? |
---|
106 | ret |
---|
107 | |
---|
108 | |
---|
109 | ;-------------------------------------------------------------------- |
---|
110 | ; GetFormatSpecifierParserToAX |
---|
111 | ; Parameters: |
---|
112 | ; AL: Format specifier character |
---|
113 | ; Returns: |
---|
114 | ; AX: Offset to parser function |
---|
115 | ; Corrupts registers: |
---|
116 | ; AX, BX |
---|
117 | ;-------------------------------------------------------------------- |
---|
118 | ALIGN JUMP_ALIGN |
---|
119 | GetFormatSpecifierParserToAX: |
---|
120 | mov bx, .rgcFormatCharToLookupIndex |
---|
121 | ALIGN JUMP_ALIGN |
---|
122 | .CheckForNextSpecifierParser: |
---|
123 | cmp al, [cs:bx] |
---|
124 | je SHORT .ConvertIndexToFunctionOffset |
---|
125 | inc bx |
---|
126 | cmp bx, .rgcFormatCharToLookupIndexEnd |
---|
127 | jb SHORT .CheckForNextSpecifierParser |
---|
128 | mov ax, c_FormatCharacter |
---|
129 | ret |
---|
130 | ALIGN JUMP_ALIGN |
---|
131 | .ConvertIndexToFunctionOffset: |
---|
132 | sub bx, .rgcFormatCharToLookupIndex |
---|
133 | shl bx, 1 ; Shift for WORD lookup |
---|
134 | mov ax, [cs:bx+.rgfnFormatSpecifierParser] |
---|
135 | ret |
---|
136 | |
---|
137 | .rgcFormatCharToLookupIndex: |
---|
138 | db "aAduxsSct-+%" |
---|
139 | .rgcFormatCharToLookupIndexEnd: |
---|
140 | ALIGN WORD_ALIGN |
---|
141 | .rgfnFormatSpecifierParser: |
---|
142 | dw a_FormatAttributeForNextCharacter |
---|
143 | dw A_FormatAttributeForRemainingString |
---|
144 | dw d_FormatSignedDecimalWord |
---|
145 | dw u_FormatUnsignedDecimalWord |
---|
146 | dw x_FormatHexadecimalWord |
---|
147 | dw s_FormatStringFromSegmentCS |
---|
148 | dw S_FormatStringFromFarPointer |
---|
149 | dw c_FormatCharacter |
---|
150 | dw t_FormatRepeatCharacter |
---|
151 | dw PrepareToPrependParameterWithSpaces |
---|
152 | dw PrepareToAppendSpacesAfterParameter |
---|
153 | dw percent_FormatPercent |
---|
154 | |
---|
155 | |
---|
156 | ;-------------------------------------------------------------------- |
---|
157 | ; PrependOrAppendSpaces |
---|
158 | ; Parameters: |
---|
159 | ; CX: Minimum length for format specifier in characters |
---|
160 | ; DS: BDA segment (zero) |
---|
161 | ; ES:DI: Ptr to cursor location in video RAM |
---|
162 | ; Returns: |
---|
163 | ; Nothing |
---|
164 | ; Corrupts registers: |
---|
165 | ; AX, BX, CX, DX |
---|
166 | ;-------------------------------------------------------------------- |
---|
167 | ALIGN JUMP_ALIGN |
---|
168 | PrependOrAppendSpaces: |
---|
169 | mov ax, di |
---|
170 | sub ax, [VIDEO_BDA.displayContext+DISPLAY_CONTEXT.fpCursorPosition] |
---|
171 | test cx, cx |
---|
172 | js SHORT .PrependWithSpaces |
---|
173 | ; Fall to .AppendSpaces |
---|
174 | |
---|
175 | ;-------------------------------------------------------------------- |
---|
176 | ; .AppendSpaces |
---|
177 | ; Parameters: |
---|
178 | ; AX: Number of format parameter BYTEs printed |
---|
179 | ; CX: Minimum length for format specifier in characters |
---|
180 | ; DS: BDA segment (zero) |
---|
181 | ; ES:DI: Ptr to cursor location in video RAM |
---|
182 | ; Returns: |
---|
183 | ; Nothing |
---|
184 | ; Corrupts registers: |
---|
185 | ; AX, CX, DX |
---|
186 | ;-------------------------------------------------------------------- |
---|
187 | .AppendSpaces: |
---|
188 | call DisplayContext_GetCharacterOffsetToAXfromByteOffsetInAX |
---|
189 | sub cx, ax |
---|
190 | jle SHORT .NothingToAppendOrPrepend |
---|
191 | mov al, ' ' |
---|
192 | jmp DisplayPrint_RepeatCharacterFromALwithCountInCX |
---|
193 | |
---|
194 | ;-------------------------------------------------------------------- |
---|
195 | ; .PrependWithSpaces |
---|
196 | ; Parameters: |
---|
197 | ; AX: Number of format parameter BYTEs printed |
---|
198 | ; CX: Negative minimum length for format specifier in characters |
---|
199 | ; DS: BDA segment (zero) |
---|
200 | ; ES:DI: Ptr to cursor location in video RAM |
---|
201 | ; Returns: |
---|
202 | ; Nothing |
---|
203 | ; Corrupts registers: |
---|
204 | ; AX, BX, CX, DX |
---|
205 | ;-------------------------------------------------------------------- |
---|
206 | ALIGN JUMP_ALIGN |
---|
207 | .PrependWithSpaces: |
---|
208 | xchg ax, cx |
---|
209 | neg ax |
---|
210 | call DisplayContext_GetByteOffsetToAXfromCharacterOffsetInAX |
---|
211 | sub ax, cx ; AX = BYTEs to prepend, CX = BYTEs to move |
---|
212 | jle SHORT .NothingToAppendOrPrepend |
---|
213 | |
---|
214 | std |
---|
215 | push si |
---|
216 | |
---|
217 | lea si, [di-1] ; SI = Offset to last byte formatted |
---|
218 | add di, ax ; DI = Cursor location after preceeding completed |
---|
219 | push di |
---|
220 | dec di ; DI = Offset where to move last byte formatted |
---|
221 | xchg bx, ax ; BX = BYTEs to prepend |
---|
222 | call .ReverseCopyCXbytesFromESSItoESDI |
---|
223 | xchg ax, bx |
---|
224 | call .ReversePrintAXspacesStartingFromESDI |
---|
225 | |
---|
226 | pop di |
---|
227 | pop si |
---|
228 | cld ; Restore DF |
---|
229 | .NothingToAppendOrPrepend: |
---|
230 | ret |
---|
231 | |
---|
232 | ;-------------------------------------------------------------------- |
---|
233 | ; .ReverseCopyCXbytesFromESSItoESDI |
---|
234 | ; Parameters: |
---|
235 | ; CX: Number of bytes to copy |
---|
236 | ; DS: BDA segment (zero) |
---|
237 | ; ES:SI: Ptr to old location |
---|
238 | ; ES:DI: Ptr to new location |
---|
239 | ; Returns: |
---|
240 | ; DI: Updated to before last character copied |
---|
241 | ; Corrupts registers: |
---|
242 | ; AX, CX, DX, SI |
---|
243 | ;-------------------------------------------------------------------- |
---|
244 | ALIGN JUMP_ALIGN |
---|
245 | .ReverseCopyCXbytesFromESSItoESDI: |
---|
246 | test BYTE [VIDEO_BDA.displayContext+DISPLAY_CONTEXT.bFlags], FLG_CONTEXT_ATTRIBUTES |
---|
247 | jz SHORT .CopyWithoutDisplayProcessing |
---|
248 | |
---|
249 | WAIT_RETRACE_IF_NECESSARY_THEN rep movsb |
---|
250 | dec di ; Point to preceeding character instead of attribute |
---|
251 | ret |
---|
252 | ALIGN JUMP_ALIGN |
---|
253 | .CopyWithoutDisplayProcessing: |
---|
254 | eSEG_STR rep, es, movsb |
---|
255 | ret |
---|
256 | |
---|
257 | ;-------------------------------------------------------------------- |
---|
258 | ; .ReversePrintAXspacesStartingFromESDI |
---|
259 | ; Parameters: |
---|
260 | ; AX: Number of spaces to print |
---|
261 | ; DS: BDA segment (zero) |
---|
262 | ; ES:DI: Ptr to destination in video RAM |
---|
263 | ; Returns: |
---|
264 | ; DI: Updated |
---|
265 | ; Corrupts registers: |
---|
266 | ; AX, CX, DX |
---|
267 | ALIGN JUMP_ALIGN |
---|
268 | .ReversePrintAXspacesStartingFromESDI: |
---|
269 | call DisplayContext_GetCharacterOffsetToAXfromByteOffsetInAX |
---|
270 | xchg cx, ax ; CX = Spaces to prepend |
---|
271 | mov al, ' ' |
---|
272 | jmp DisplayPrint_RepeatCharacterFromALwithCountInCX |
---|
273 | |
---|
274 | |
---|
275 | |
---|
276 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
---|
277 | ; Formatting functions |
---|
278 | ; Parameters: |
---|
279 | ; DS: BDA segment (zero) |
---|
280 | ; SS:BP: Pointer to next format parameter (-=2 updates to next parameter) |
---|
281 | ; ES:DI: Ptr to cursor location in video RAM |
---|
282 | ; Returns: |
---|
283 | ; SS:BP: Points to last WORD parameter used |
---|
284 | ; Corrupts registers: |
---|
285 | ; AX, BX, DX |
---|
286 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
---|
287 | ALIGN JUMP_ALIGN |
---|
288 | a_FormatAttributeForNextCharacter: |
---|
289 | mov bl, [bp] |
---|
290 | xchg bl, [VIDEO_BDA.displayContext+DISPLAY_CONTEXT.bAttribute] |
---|
291 | push bx |
---|
292 | push cx |
---|
293 | push di |
---|
294 | call DisplayFormat_ParseCharacters ; Recursive call |
---|
295 | pop WORD [VIDEO_BDA.displayContext+DISPLAY_CONTEXT.fpCursorPosition] |
---|
296 | pop cx |
---|
297 | pop bx |
---|
298 | mov [VIDEO_BDA.displayContext+DISPLAY_CONTEXT.bAttribute], bl |
---|
299 | ret |
---|
300 | |
---|
301 | ALIGN JUMP_ALIGN |
---|
302 | A_FormatAttributeForRemainingString: |
---|
303 | mov al, [bp] |
---|
304 | mov [VIDEO_BDA.displayContext+DISPLAY_CONTEXT.bAttribute], al |
---|
305 | ret |
---|
306 | |
---|
307 | ALIGN JUMP_ALIGN |
---|
308 | d_FormatSignedDecimalWord: |
---|
309 | mov ax, [bp] |
---|
310 | mov bx, 10 |
---|
311 | jmp DisplayPrint_SignedWordFromAXWithBaseInBX |
---|
312 | |
---|
313 | ALIGN JUMP_ALIGN |
---|
314 | u_FormatUnsignedDecimalWord: |
---|
315 | mov ax, [bp] |
---|
316 | mov bx, 10 |
---|
317 | jmp DisplayPrint_WordFromAXWithBaseInBX |
---|
318 | |
---|
319 | ALIGN JUMP_ALIGN |
---|
320 | x_FormatHexadecimalWord: |
---|
321 | mov ax, [bp] |
---|
322 | mov bx, 16 |
---|
323 | call DisplayPrint_WordFromAXWithBaseInBX |
---|
324 | mov al, 'h' |
---|
325 | jmp DisplayPrint_CharacterFromAL |
---|
326 | |
---|
327 | ALIGN JUMP_ALIGN |
---|
328 | s_FormatStringFromSegmentCS: |
---|
329 | xchg si, [bp] |
---|
330 | call DisplayPrint_NullTerminatedStringFromCSSI |
---|
331 | mov si, [bp] |
---|
332 | ret |
---|
333 | |
---|
334 | ALIGN JUMP_ALIGN |
---|
335 | S_FormatStringFromFarPointer: |
---|
336 | mov bx, [bp-2] |
---|
337 | xchg si, [bp] |
---|
338 | call DisplayPrint_NullTerminatedStringFromBXSI |
---|
339 | mov si, [bp] |
---|
340 | dec bp |
---|
341 | dec bp |
---|
342 | ret |
---|
343 | |
---|
344 | ALIGN JUMP_ALIGN |
---|
345 | c_FormatCharacter: |
---|
346 | mov al, [bp] |
---|
347 | jmp DisplayPrint_CharacterFromAL |
---|
348 | |
---|
349 | ALIGN JUMP_ALIGN |
---|
350 | t_FormatRepeatCharacter: |
---|
351 | push cx |
---|
352 | mov cx, [bp-2] |
---|
353 | mov al, [bp] |
---|
354 | call DisplayPrint_RepeatCharacterFromALwithCountInCX |
---|
355 | pop cx |
---|
356 | dec bp |
---|
357 | dec bp |
---|
358 | ret |
---|
359 | |
---|
360 | ALIGN JUMP_ALIGN |
---|
361 | percent_FormatPercent: |
---|
362 | mov al, '%' |
---|
363 | jmp DisplayPrint_CharacterFromAL |
---|
364 | |
---|
365 | ALIGN JUMP_ALIGN |
---|
366 | PrepareToPrependParameterWithSpaces: |
---|
367 | neg cx |
---|
368 | ; Fall to PrepareToAppendSpacesAfterParameter |
---|
369 | |
---|
370 | ALIGN JUMP_ALIGN |
---|
371 | PrepareToAppendSpacesAfterParameter: |
---|
372 | add sp, BYTE 2 ; Remove return offset |
---|
373 | jmp ParseFormatSpecifier |
---|