]> git.sur5r.net Git - cc65/blob - libsrc/common/snprintf.s
Just removed some trailing spaces.
[cc65] / libsrc / common / snprintf.s
1 ;
2 ; int snprintf (char* buf, size_t size, const char* Format, ...);
3 ;
4 ; Ullrich von Bassewitz, 2009-09-26
5 ;
6
7         .export         _snprintf
8         .import         pushax, addysp, decsp6, _vsnprintf
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 _snprintf:
27         sty     ParamSize               ; Number of param bytes passed in Y
28
29 ; We have to push buf/size/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     decsp6
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 buf/size/format
49
50         ldy     #6-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 vsprintf)
57
58         lda     ptr1
59         ldx     ptr1+1
60
61 ; Call vsnprintf
62
63         jsr     _vsnprintf
64
65 ; Cleanup the stack. We will return what we got from vsprintf
66
67         ldy     ParamSize
68         jmp     addysp
69