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

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

Initial commit for Assembly Library.

File size: 9.1 KB
Line 
1; File name : DisplayFormat.asm
2; Project name : Assembly Library
3; Created date : 29.6.2010
4; Last update : 10.8.2010
5; Author : Tomi Tilli
6; Description : Functions for displaying formatted strings.
7
8; Section containing code
9SECTION .text
10
11;--------------------------------------------------------------------
12; DisplayFormat_ParseCharacters
13; Parameters:
14; DS: BDA segment (zero)
15; SS:BP: Pointer to first format parameter (-=2 for next parameter)
16; CS:SI: Pointer to string to format
17; ES:DI: Ptr to cursor location in video RAM
18; Returns:
19; DI: Updated offset to video RAM
20; Corrupts registers:
21; AX, BX, CX, DX, SI, BP
22;--------------------------------------------------------------------
23ALIGN JUMP_ALIGN
24DisplayFormat_ParseCharacters:
25 call .ReadCharacterAndTestForNull
26 jz SHORT .Return
27 xor cx, cx ; Zero CX for parsing number parameter
28 cmp al, '%'
29 je SHORT .FormatParameterEncountered
30 call DisplayPrint_CharacterFromAL ; Control or printable character
31 jmp SHORT DisplayFormat_ParseCharacters
32ALIGN JUMP_ALIGN
33.Return:
34 ret
35
36;--------------------------------------------------------------------
37; .ReadCharacterAndTestForNull
38; Parameters:
39; CS:SI: Pointer next character from string
40; Returns:
41; AL: Character from string
42; SI: Incremented to next character
43; ZF: Set if NULL, cleared if valid character
44; Corrupts registers:
45; Nothing
46;--------------------------------------------------------------------
47ALIGN JUMP_ALIGN
48.ReadCharacterAndTestForNull:
49 eSEG cs
50 lodsb ; Load from CS:SI to AL
51 test al, al ; NULL to end string?
52 ret
53
54
55;--------------------------------------------------------------------
56; .FormatParameterEncountered
57; Parameters:
58; CX: Zero or previous number parameter
59; DS: BDA segment (zero)
60; SS:BP: Pointer to next format parameter
61; CS:SI: Pointer to next format string character
62; ES:DI: Ptr to cursor location in video RAM
63; Returns:
64; SI: Incremented to next character
65; BP: Offset to next format parameter
66; Eventually jumps to DisplayFormat_ParseCharacters
67; Corrupts registers:
68; AX, BX, CX, DX
69;--------------------------------------------------------------------
70ALIGN JUMP_ALIGN
71.FormatParameterEncountered:
72 call .ReadCharacterAndTestForNull
73 jz SHORT .Return
74 call Char_IsDecimalDigitInAL
75 jnc SHORT .FormatWithCorrectFormatFunction
76 ; Fall to .ParseNumberParameterToCX
77
78;--------------------------------------------------------------------
79; .ParseNumberParameterToCX
80; Parameters:
81; AL: Number digit from format string
82; CX: Zero or previous number parameter
83; Returns:
84; CX: Updated number parameter
85; Jumps to .FormatParameterEncountered
86; Corrupts registers:
87; AX
88;--------------------------------------------------------------------
89.ParseNumberParameterToCX:
90 sub al, '0' ; Digit '0'...'9' to integer 0...9
91 mov ah, cl ; Previous number parameter to AH
92 aad ; AL += (AH * 10)
93 mov cl, al ; Updated number parameter now in CX
94 jmp SHORT .FormatParameterEncountered
95
96;--------------------------------------------------------------------
97; .FormatWithCorrectFormatFunction
98; Parameters:
99; AL: Format placeholder character (non digit character after '%')
100; CX: Number parameter (zero if no number parameter present)
101; DS: BDA segment (zero)
102; SS:BP: Pointer to format parameter
103; CS:SI: Pointer to next format string character
104; ES:DI: Ptr to cursor location in video RAM
105; Returns:
106; Eventually jumps to DisplayFormat_ParseCharacters
107; Corrupts registers:
108; AX, BX, CX, DX
109;--------------------------------------------------------------------
110ALIGN JUMP_ALIGN
111.FormatWithCorrectFormatFunction:
112 xor bx, bx ; Zero lookup index
113ALIGN JUMP_ALIGN
114.PlaceholderComparisonLoop:
115 cmp al, [cs:bx+.rgcFormatCharToLookupIndex]
116 je SHORT .JumpToFormatPlaceholder
117 inc bx
118 cmp bl, .EndOFrgcFormatCharToLookupIndex - .rgcFormatCharToLookupIndex
119 jb SHORT .PlaceholderComparisonLoop ; Loop
120 call DisplayPrint_CharacterFromAL ; Display unsupported format character
121 ; Fall to .UpdateSpacesToAppendAfterPrintingSingleCharacter
122
123;--------------------------------------------------------------------
124; .UpdateSpacesToAppendAfterPrintingSingleCharacter
125; .UpdateSpacesToAppend
126; Parameters:
127; BX: Number of characters printed (.UpdateSpacesToAppend only)
128; CX: Number parameter (zero if no number parameter present)
129; DS: BDA segment (zero)
130; SS:BP: Pointer to format parameter
131; CS:SI: Pointer to next format string character
132; ES:DI: Ptr to cursor location in video RAM
133; Returns:
134; CX: Number of spaces to append
135; Jumps to .PrepareToFormatNextParameter
136; Corrupts registers:
137; AX, BX, CX, DX
138;--------------------------------------------------------------------
139ALIGN JUMP_ALIGN
140.UpdateSpacesToAppendAfterPrintingSingleCharacter:
141 mov bx, 1
142ALIGN JUMP_ALIGN
143.UpdateSpacesToAppend:
144 jcxz .PrepareToFormatNextParameter
145 sub cx, bx ; Number of spaces to append
146 jle SHORT .PrepareToFormatNextParameter
147 mov al, ' '
148 call DisplayPrint_RepeatCharacterFromALwithCountInCX
149 ; Fall to .PrepareToFormatNextParameter
150
151;--------------------------------------------------------------------
152; .PrepareToFormatNextParameter
153; Parameters:
154; DS: BDA segment (zero)
155; SS:BP: Pointer to format parameter
156; CS:SI: Pointer to next format string character
157; ES:DI: Ptr to cursor location in video RAM
158; Returns:
159; BP: Adjusted to point next stack parameter
160; Jumps to DisplayFormat_ParseCharacters
161; Corrupts registers:
162; Nothing
163;--------------------------------------------------------------------
164ALIGN JUMP_ALIGN
165.PrepareToFormatNextParameter:
166 dec bp
167 dec bp
168 jmp SHORT DisplayFormat_ParseCharacters
169
170
171;--------------------------------------------------------------------
172; .JumpToFormatPlaceholder
173; Parameters:
174; BX: Lookup index for format function
175; CX: Number parameter (zero if no number parameter present)
176; DS: BDA segment (zero)
177; SS:BP: Pointer to next format parameter
178; CS:SI: Pointer to next format string character
179; ES:DI: Ptr to cursor location in video RAM
180; Returns:
181; Eventually jumps to DisplayFormat_ParseCharacters
182; Corrupts registers:
183; AX, BX, DX
184;--------------------------------------------------------------------
185ALIGN JUMP_ALIGN
186.JumpToFormatPlaceholder:
187 shl bx, 1 ; Shift for WORD lookup
188 jmp [cs:bx+.rgfnFormatParameter]
189
190ALIGN JUMP_ALIGN
191.a_FormatAttributeForNextCharacter:
192 mov bl, [bp]
193 xchg bl, [VIDEO_BDA.displayContext+DISPLAY_CONTEXT.bAttribute]
194 call .ReadCharacterAndTestForNull
195 jz SHORT .Return
196 call DisplayPrint_CharacterFromAL
197 mov [VIDEO_BDA.displayContext+DISPLAY_CONTEXT.bAttribute], bl
198 jmp SHORT .UpdateSpacesToAppendAfterPrintingSingleCharacter
199
200ALIGN JUMP_ALIGN
201.A_FormatAttributeForRemainingString:
202 mov al, [bp]
203 mov [VIDEO_BDA.displayContext+DISPLAY_CONTEXT.bAttribute], al
204 jmp SHORT .PrepareToFormatNextParameter
205
206ALIGN JUMP_ALIGN
207.d_FormatSignedDecimalWord:
208 mov ax, [bp]
209 call DisplayPrint_SignedDecimalIntegerFromAX
210 jmp SHORT .UpdateSpacesToAppend
211
212ALIGN JUMP_ALIGN
213.u_FormatUnsignedDecimalWord:
214 mov ax, [bp]
215 mov bx, 10
216 call DisplayPrint_WordFromAXWithBaseInBX
217 jmp SHORT .UpdateSpacesToAppend
218
219ALIGN JUMP_ALIGN
220.x_FormatHexadecimalWord:
221 mov ax, [bp]
222 mov bx, 16
223 call DisplayPrint_WordFromAXWithBaseInBX
224 inc bx ; Increment character count for 'h'
225 mov al, 'h'
226 call DisplayPrint_CharacterFromAL
227 jmp SHORT .UpdateSpacesToAppend
228
229ALIGN JUMP_ALIGN
230.s_FormatStringFromSegmentCS:
231 xchg si, [bp]
232 call DisplayPrint_NullTerminatedStringFromCSSI
233 mov si, [bp] ; Restore SI
234 jmp SHORT .UpdateSpacesToAppend
235
236ALIGN JUMP_ALIGN
237.S_FormatStringFromFarPointer:
238 xchg si, [bp]
239 mov bx, [bp-2]
240 call DisplayPrint_NullTerminatedStringFromBXSI
241 mov si, [bp] ; Restore SI
242 jmp SHORT .UpdateSpacesToAppendWithDWordParameter
243
244ALIGN JUMP_ALIGN
245.c_FormatCharacter:
246 mov al, [bp]
247 jmp SHORT .FormatSingleCharacterFromAL
248
249ALIGN JUMP_ALIGN
250.t_FormatRepeatCharacter:
251 mov cx, [bp-2]
252 mov bx, cx
253 mov al, [bp]
254 call DisplayPrint_RepeatCharacterFromALwithCountInCX
255 jmp SHORT .UpdateSpacesToAppendWithDWordParameter
256
257ALIGN JUMP_ALIGN
258.percent_FormatPercent:
259 mov al, '%'
260 inc bp ; Adjust here since...
261 inc bp ; ...no parameter on stack
262 ; Fall to .FormatSingleCharacterFromAL
263
264ALIGN JUMP_ALIGN
265.FormatSingleCharacterFromAL:
266 call DisplayPrint_CharacterFromAL
267 jmp .UpdateSpacesToAppendAfterPrintingSingleCharacter
268
269ALIGN JUMP_ALIGN
270.UpdateSpacesToAppendWithDWordParameter:
271 dec bp
272 dec bp
273 jmp .UpdateSpacesToAppend
274
275
276; Table for converting format character to jump table index
277.rgcFormatCharToLookupIndex:
278 db "aAduxsSct%"
279.EndOFrgcFormatCharToLookupIndex:
280
281; Jump table
282ALIGN WORD_ALIGN
283.rgfnFormatParameter:
284 dw .a_FormatAttributeForNextCharacter
285 dw .A_FormatAttributeForRemainingString
286 dw .d_FormatSignedDecimalWord
287 dw .u_FormatUnsignedDecimalWord
288 dw .x_FormatHexadecimalWord
289 dw .s_FormatStringFromSegmentCS
290 dw .S_FormatStringFromFarPointer
291 dw .c_FormatCharacter
292 dw .t_FormatRepeatCharacter
293 dw .percent_FormatPercent
Note: See TracBrowser for help on using the repository browser.