]> git.sur5r.net Git - cc65/blob - libsrc/common/scanf.s
Removed (pretty inconsistently used) tab chars from source code base.
[cc65] / libsrc / common / scanf.s
1 ;
2 ; int scanf(const char* Format, ...);
3 ;
4 ; 2000-12-01, Ullrich von Bassewitz
5 ; 2004-12-31, Greg King
6 ;
7
8         .export         _scanf
9
10         .import         _stdin, pushax, addysp, _vfscanf
11         .import         sp:zp, ptr1:zp
12
13         .macpack        generic
14
15 ; ----------------------------------------------------------------------------
16 ; Code
17 ;
18 _scanf:
19         sty     ArgSize         ; Number of argument bytes passed in .Y
20
21 ; We are using a (hopefully) clever trick here to reduce code size.  On entry,
22 ; the stack pointer points to the last pushed argument of the variable
23 ; argument list.  Adding the number of argument bytes, would result in a
24 ; pointer that points _above_ the Format argument.
25 ; Because we have to push stdin anyway, we will do that here, so:
26 ;
27 ;   * we will save the subtraction of 2 (__fixargs__) later;
28 ;   * we will have the address of the Format argument which needs to
29 ;     be pushed next.
30
31         lda     _stdin
32         ldx     _stdin+1
33         jsr     pushax
34
35 ; Now, calculate the va_list pointer, which does point to Format.
36
37         lda     sp
38         ldx     sp+1
39         add     ArgSize
40         bcc     @L1
41         inx
42 @L1:    sta     ptr1
43         stx     ptr1+1
44
45 ; Push a copy of Format.
46
47         ldy     #1
48         lda     (ptr1),y
49         tax
50         dey
51         lda     (ptr1),y
52         jsr     pushax
53
54 ; Load va_list [last and __fastcall__ argument to vfscanf()].
55
56         lda     ptr1
57         ldx     ptr1+1
58
59 ; Call vfscanf().
60
61         jsr     _vfscanf
62
63 ; Clean up the stack.  We will return what we got from vfscanf().
64
65         ldy     ArgSize
66         jmp     addysp
67
68 ; ----------------------------------------------------------------------------
69 ; Data
70 ;
71         .bss
72 ArgSize:
73         .res    1               ; Number of argument bytes
74