source: xtideuniversalbios/trunk/Assembly_Library/Src/Display/DisplayFormat.asm@ 114

Last change on this file since 114 was 101, checked in by Tomi Tilli, 14 years ago

Changes to Assembly Library:

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