File: printf.h

package info (click to toggle)
sbm 3.7.1-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,060 kB
  • ctags: 1,590
  • sloc: asm: 11,940; ansic: 2,870; makefile: 531
file content (414 lines) | stat: -rw-r--r-- 11,333 bytes parent folder | download | duplicates (4)
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
%ifndef _PRINTF_H
%define _PRINTF_H
%include "helptool.h"

%assign PRINT_EOL 1

%macro push_string 1+
	section .data
%%str	db	%1,0
	section .text
	push word %%str
%endmacro

%macro printf 1-*

	pusha
	%assign rargc 0
	%if %0 >1
	%rotate -2
	%assign argc %0-1
	%rep argc
		%ifidn %1,byte
	     		push byte %2
			%rotate -1
			%assign argc argc-1
			%assign rargc rargc+1
	    	%elifidn %1,word
	    		push word %2
			%rotate -1
			%assign argc argc-1
			%assign rargc rargc+1
	    	%elifidn %1,dword
	        	push dword %2
			%rotate -1
			%assign argc argc-1
			%assign rargc rargc+2
		%elifstr %2
			push_string %2
			%assign rargc rargc+1
	    	%else
	        	push word %2
			%assign rargc rargc+1
    	    	%endif
		%rotate -1
		%assign argc argc-1
		%if argc <=0
			%exitrep
		%endif
	%endrep
	%rotate 1
	%endif
	%ifstr %1
		push_string %1
	%else
		push word %1
	%endif
	%assign rargc rargc+1
	call __printf
	add sp, (rargc) *2
	popa
%endmacro

	section .text
	
%define n           bp+6
%define strp        bp+4

;Proc        itoa
itoa:

            push bp                 ;Set up stack frame
            mov bp,sp
            pusha                   ;Save all registers

            mov ax,[n]              ;AX = n
            mov di,[strp]           ;DI = string pointer

            test ax,ax              ;Negative?
            jge .p1_noneg
            mov byte[ di],'-'       ;Store minus sign
            inc di
            neg ax                  ;Make it positive

.p1_noneg:   xor cx,cx               ;Zero CX
            test ax,ax              ;Check for zero
            jnz p1_nozero

            push byte '0'                ;Push a zero
            inc cx                  ;One digit
            jmp p1_ploop

p1_nozero:  mov si,10               ;SI = 10

.p1_dloop:   xor dx,dx               ;Divide by 10
            div si
            mov bl,dl               ;Remainder in BL
            add bl,30h              ;Convert to digit
            push bx                 ;Push digit
            inc cx
            test ax,ax              ;Loop back
            jnz .p1_dloop

p1_ploop:   pop ax                  ;Pop digit
            mov [di],al             ;Store digit
            inc di
            loop p1_ploop           ;Loop back

            mov byte[ di],0         ;Add the null byte

            popa                    ;Restore registers
            pop bp                  ;Delete stack frame
            ret 4                   ;Return

;EndP        itoa

;****************** ltoa() -- Convert long to string
;void ltoa(long n, char *strp);

;Proc        ltoa
ltoa:

            push bp                 ;Set up stack frame
            mov bp,sp
            pusha                   ;Save all registers

            mov di,[strp]           ;DI = string pointer
            mov dx,[n+2]            ;DX:AX = n
            mov ax,[n]
            test dx,dx              ;DX = 0, use itoa
            jnz p1_chkn
            cmp ax,8000h
            jnb p1_chkn

p1_itoa:    push ax
	    push di              ;Convert with itoa
            call itoa
            jmp p1_done_l             ;Return

p1_chkn:    cmp dx,-1               ;DX = -1 and AX <> 0, use itoa
            jne p1_strp
            cmp ax,8000h
            jae p1_itoa

p1_strp:    test dx,dx              ;Negative?
            jge .p1_noneg
            mov byte[ di],'-'       ;Store minus sign
            inc di
            neg dx                  ;Make it positive
            neg ax
            sbb dx,0

.p1_noneg:   mov bx,50000            ;Divide by 100000
            div bx
            xor bx,bx               ;Zero BX
            shr ax,1
            adc bx,0                ;BX = rem flag
            push bx
	    push dx              ;Save BX, DX

            test ax,ax              ;Check for zero
            jz p1_cont

            xor cx,cx               ;Zero CX
            mov si,10               ;SI = 10

.p1_dloop:   xor dx,dx               ;Divide by 10
            div si
            mov bl,dl               ;Remainder in BL
            add bl,30h              ;Convert to digit
            push bx                 ;Push digit
            inc cx
            test ax,ax              ;Loop back
            jnz .p1_dloop

.p1_ploop:   pop ax                  ;Pop digit
            mov [di],al             ;Store digit
            inc di
            loop .p1_ploop           ;Loop back

p1_cont:    pop ax
	    pop bx               ;Restore low data
            xor dx,dx               ;Zero DX
            test bx,bx              ;Check for high part
            jz p1_nohigh

            add ax,50000            ;Add in 50000
            adc dx,0

p1_nohigh:  mov si,10               ;SI = 10
            mov cx,5                ;5 digits
            jmp .p1_skip1

.p1_dloopb:  xor dx,dx               ;Zero DX
.p1_skip1:   div si                  ;Divide by 10
            mov bl,dl               ;Remainder in BL
            add bl,30h              ;Convert to digit
            push bx                 ;Push digit
            loop .p1_dloopb          ;Loop back

            mov cx,5                ;5 digits

p1_ploopb:  pop ax                  ;Pop digit
            mov [di],al             ;Store digit
            inc di
            loop p1_ploopb          ;Loop back

            mov byte[ di],0         ;Add the null byte

p1_done_l:    popa                    ;Restore registers
            pop bp                  ;Delete stack frame
            ret 6                   ;Return

;EndP        ltoa

;Proc        PUT_CHAR
PUT_CHAR:

            pusha                   ;Save registers
%ifdef use_dos
	    xchg dx,ax              ;STDOUT output, DL = char
            mov ah,2
            int 21h                 ;DOS call
%else
	    mov ah, 0eh
	    sub bx,bx
	    int 10h
%endif
            popa                    ;Restore registers
            ret                     ;Return

;EndP        PUT_CHAR




;****************** printf() -- Print formatted string
;void printf(char *fmt, void *args);

%define fmt         bp+4
%define args        bp+6

	section .bss
PRT_BUF	resb	20
STAT_BUF resb	40
	section .text
__printf:

            push bp                 ;Set up stack frame
            mov bp,sp
            pusha                   ;Save all registers

            mov si,[fmt]            ;SI = string pointer
            lea bx,[args]           ;BX = arg pointer

p1_loop:    lodsb                   ;Get char
            test al,al              ;Check for null
            jz p1_done
            cmp al,'%'              ;Check for '%'
            je p1_proc
	    cmp al,'\'
	    je p1_esc
p1_putc:    call PUT_CHAR           ;Output char
            jmp p1_loop             ;Loop back

p1_esc:	    lodsb
	    test al,al
	    jz p1_done
	    cmp al,"\"
	    je p1_putc
	    cmp al,"n"
	    je p1_eol
	    jmp p1_loop
p1_eol:
	    mov al,13
	    call PUT_CHAR
	    mov al,10
	    call PUT_CHAR
	    jmp p1_loop
	    
p1_proc:    lodsb                   ;Get char
            test al,al              ;Check for null
            jz p1_done
            cmp al,'%'              ; %% = percent
            je p1_putc
            cmp al,'d'              ; %d = integer
            je p1_int
            cmp al,'l'              ; %l = long int
            je p1_long
            cmp al,'x'              ; %x = hex
            je p1_hex
	    cmp al,'h'		    ; %h = byte hex
	    je p1_bhex
            cmp al,'c'              ; %c = char
            je NEAR p1_char
            cmp al,'s'              ; %s = string
            je NEAR p1_str
	    cmp al,'b'		    ; %b = binary bits 8bit
	    je NEAR p1_bin8
	    cmp al,'B'		    ; %B = binary bits 16bit
	    je NEAR p1_bin16
            jmp p1_loop             ;Invalid, ignore

p1_done:    popa                    ;Restore registers
            pop bp                  ;Delete stack frame
            ret                    ;Return

p1_long:    lodsb                   ;Get char
            test al,al              ;Check for null
            jz p1_done
            cmp al,'d'              ; %ld = long integer
            je p1_lint
            cmp al,'x'              ; %lx = long hex
            je p1_lhex
            jmp p1_loop             ;Invalid, ignore

p1_int:     push word[ss:bx]          ;itoa(*bx, PRT_BUF);
            push word PRT_BUF
            call itoa
            inc bx                  ;Advance pointer
            inc bx
            mov di,PRT_BUF   ;Print alpha string
            jmp p1_alpha

p1_lint:    push word[ss:bx+2]
            push word[ss:bx]          ;ltoa(*bx, PRT_BUF);
            push word PRT_BUF
            call ltoa
            add bx,4                ;Advance pointer
            mov di,PRT_BUF   ;Print alpha string
            jmp p1_alpha

p1_hex:     mov ax,[ss:bx]             ;AX = arg
            call p1_chex            ;Convert to hex
            inc bx                  ;Advance pointer
            inc bx
            jmp p1_loop             ;Loop back
p1_bhex:
	    mov ax,[ss:bx]
	    mov cx,2
	    ror al,byte 4
	    call p1_hloop
	    inc bx
	    inc bx
	    jmp p1_loop

p1_lhex:    mov ax,[ss:bx+2]           ;AX = high word
            call p1_chex            ;Convert to hex
            mov ax,[ss:bx]             ;AX = low word
            call p1_chex            ;Convert to hex
            add bx,4                ;Advance pointer
            jmp p1_loop             ;Loop back
p1_char:    mov al,[ss:bx]             ;Output char
            call PUT_CHAR
            inc bx                  ;Advance pointer
            jmp p1_loop             ;Loop back

p1_str:
	    mov cx,[ss:bx]
	    add bx,2
.again:
	    xchg bx,cx
	    mov al,[ss:bx]             ;Get char
            inc bx                  ;Advance pointer
	    xchg bx,cx
            test al,al              ;Check for null
            jz p1_sdone
            call PUT_CHAR           ;Output char
            jmp .again              ;Loop back

p1_sdone:   jmp p1_loop             ;Return to main loop

p1_bin8:
	    mov cx,8
	    mov dh,[ss:bx]
	    jmp p1_bin
p1_bin16:	    
	    mov cx,16
	    mov dx,[ss:bx]           ;AX = high word
p1_bin:	    
p1_bin_loop:
	    shl dx,1
	    setc al
	    add al,'0'
	    call PUT_CHAR
	    loop p1_bin_loop
            add bx,2                ;Advance pointer
            jmp p1_loop             ;Loop back


p1_alpha:   mov al,[di]             ;Get char
            test al,al              ;Check for null
            jz p1_sdone
            call PUT_CHAR           ;Output char
            inc di                  ;Advance pointer
            jmp p1_alpha            ;Loop back


p1_chex:    mov cx,4                ;4 hex digits
            xchg al,ah              ;Reverse the order
            ror ah,cl               ;of the hex digits
            ror al,cl               ;in AX

p1_hloop:   push ax                 ;Save AX
            and al,0Fh              ;Keep 4 bits
            cmp al,0Ah              ;Compute the hex digit,
            sbb al,69h              ;using Improved Allison's Algorithm
            das
            call PUT_CHAR           ;Output char
            pop ax                  ;Restore AX
            shr ax,4                ;Shift it over
            loop p1_hloop           ;Loop back
            ret                     ;Return

%endif