]> git.sur5r.net Git - cc65/blob - libsrc/common/fscanf.s
Merge pull request #740 from laubzega/master
[cc65] / libsrc / common / fscanf.s
1 ;
2 ; int fscanf (FILE* f, const char* format, ...);
3 ;
4 ; Ullrich von Bassewitz, 2004-11-28
5 ;
6
7         .export         _fscanf
8         .import         addysp, decsp4, _vfscanf
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 ; int fscanf (FILE* f, const char* format, ...)
22 ; /* Standard C function */
23 ; {
24 ;     va_list ap;
25 ;
26 ;     /* Setup for variable arguments */
27 ;     va_start (ap, format);
28 ;
29 ;     /* Call vfscanf(). Since we know that va_end won't do anything, we will
30 ;     ** save the call and return the value directly.
31 ;     */
32 ;     return vfscanf (f, format, ap);
33 ; }
34 ;
35
36 .code
37
38 _fscanf:
39         sty     ParamSize               ; Number of param bytes passed in Y
40
41 ; We have to push f and format, both in the order they already have on stack.
42 ; To make this somewhat more efficient, we will create space on the stack and
43 ; then do a copy of the complete block instead of pushing each parameter
44 ; separately. Since the size of the arguments passed is the same as the size
45 ; of the fixed arguments, this will allow us to calculate the pointer to the
46 ; fixed size arguments easier (they're just ParamSize bytes away).
47
48         jsr     decsp4
49
50 ; Calculate a pointer to the Format argument
51
52         lda     ParamSize
53         add     sp
54         sta     ptr1
55         ldx     sp+1
56         bcc     @L1
57         inx
58 @L1:    stx     ptr1+1
59
60 ; Now copy both, f and format
61
62         ldy     #4-1
63 @L2:    lda     (ptr1),y
64         sta     (sp),y
65         dey
66         bpl     @L2
67
68 ; Load va_list (last and __fastcall__ parameter to vfscanf)
69
70         lda     ptr1
71         ldx     ptr1+1
72
73 ; Call vfscanf
74
75         jsr     _vfscanf
76
77 ; Cleanup the stack. We will return what we got from vfscanf
78
79         ldy     ParamSize
80         jmp     addysp
81