]> git.sur5r.net Git - cc65/blob - libsrc/common/sprintf.s
Removed (pretty inconsistently used) tab chars from source code base.
[cc65] / libsrc / common / sprintf.s
1 ;
2 ; int sprintf (char* buf, const char* Format, ...);
3 ;
4 ; Ullrich von Bassewitz, 1.12.2000
5 ;
6
7         .export         _sprintf
8         .import         pushax, addysp, decsp4, _vsprintf
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 _sprintf:
27         sty     ParamSize               ; Number of param bytes passed in Y
28
29 ; We have to push buf 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, buf 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 vsprintf)
57
58         lda     ptr1
59         ldx     ptr1+1
60
61 ; Call vsprintf
62
63         jsr     _vsprintf
64
65 ; Cleanup the stack. We will return what we got from vsprintf
66
67         ldy     ParamSize
68         jmp     addysp
69