]> git.sur5r.net Git - cc65/blob - libsrc/common/fprintf.s
Changes due to code review.
[cc65] / libsrc / common / fprintf.s
1 ;
2 ; int fprintf (FILE* f, const char* Format, ...);
3 ;
4 ; Ullrich von Bassewitz, 1.12.2000
5 ;
6
7         .export         _fprintf
8         .import         addysp, decsp4, _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 _fprintf:
27         sty     ParamSize               ; Number of param bytes passed in Y
28
29 ; We have to push f and format, both in the order they already have on stack.
30 ; To make this somewhat more efficient, we will create space on the stack and
31 ; then do a copy of the complete block instead of pushing each parameter
32 ; separately. Since the size of the arguments passed is the same as the size
33 ; of the fixed arguments, this will allow us to calculate the pointer to the
34 ; fixed size arguments easier (they're just ParamSize bytes away).
35
36         jsr     decsp4
37
38 ; Calculate a pointer to the Format argument
39
40         lda     ParamSize
41         add     sp
42         sta     ptr1
43         ldx     sp+1
44         bcc     @L1
45         inx
46 @L1:    stx     ptr1+1
47
48 ; Now copy both, f and format
49
50         ldy     #4-1
51 @L2:    lda     (ptr1),y
52         sta     (sp),y
53         dey
54         bpl     @L2
55
56 ; Load va_list (last and __fastcall__ parameter to vfprintf)
57
58         lda     ptr1
59         ldx     ptr1+1
60
61 ; Call vfprintf
62
63         jsr     _vfprintf
64
65 ; Cleanup the stack. We will return what we got from vfprintf
66
67         ldy     ParamSize
68         jmp     addysp
69