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