]> git.sur5r.net Git - cc65/blob - libsrc/common/printf.s
Changed most "backticks" (grave accents) into apostrophes.
[cc65] / libsrc / common / printf.s
1 ;
2 ; int printf (const char* Format, ...);
3 ;
4 ; Ullrich von Bassewitz, 1.12.2000
5 ;
6
7         .export         _printf
8         .import         _stdout, pushax, addysp, _vfprintf
9         .importzp       sp, ptr1
10
11         .macpack        generic
12
13 ; ----------------------------------------------------------------------------
14 ; Data
15
16 .bss
17
18 ParamSize:      .res    1               ; Number of parameter bytes
19
20 ; ----------------------------------------------------------------------------
21 ; Code
22
23 .code
24
25
26 _printf:
27         sty     ParamSize               ; Number of param bytes passed in Y
28
29 ; We are using a (hopefully) clever trick here to reduce code size. On entry,
30 ; the stack pointer points to the last pushed parameter of the variable
31 ; parameter list. Adding the number of parameter bytes, would result in a
32 ; pointer that points *after* the Format parameter.
33 ; Since we have to push stdout anyway, we will do that here, so
34 ;
35 ;   * we will save the subtraction of 2 (__fixargs__) later
36 ;   * we will have the address of the Format parameter which needs to
37 ;     be pushed next.
38 ;
39
40         lda     _stdout
41         ldx     _stdout+1
42         jsr     pushax
43
44 ; Now calculate the va_list pointer, which does points to Format
45
46         lda     sp
47         ldx     sp+1
48         add     ParamSize
49         bcc     @L1
50         inx
51 @L1:    sta     ptr1
52         stx     ptr1+1
53
54 ; Push Format
55
56         ldy     #1
57         lda     (ptr1),y
58         tax
59         dey
60         lda     (ptr1),y
61         jsr     pushax
62
63 ; Load va_list (last and __fastcall__ parameter to vfprintf)
64
65         lda     ptr1
66         ldx     ptr1+1
67
68 ; Call vfprintf
69
70         jsr     _vfprintf
71
72 ; Cleanup the stack. We will return what we got from vfprintf
73
74         ldy     ParamSize
75         jmp     addysp
76