2 ; int vsnprintf (char* Buf, size_t size, const char* Format, va_list ap);
4 ; Ullrich von Bassewitz, 2009-09-26
7 .export _vsnprintf, vsnprintf
8 .import ldaxysp, popax, incsp2, incsp6
9 .import _memcpy, __printf
16 ; ----------------------------------------------------------------------------
18 ; Static data for the _vsnprintf routine
21 outdesc: ; Static outdesc structure
22 ccount: .word 0 ; ccount
23 func: .word out ; Output function pointer
25 bufsize:.word 0 ; Buffer size
29 ; ----------------------------------------------------------------------------
30 ; vsprintf - formatted output into a buffer
32 ; int __fastcall__ vsnprintf (char* buf, size_t size, const char* format, va_list ap);
40 ; Setup the outdesc structure. This is also an additional entry point for
41 ; vsprintf with ap on stack
46 sta ccount+1 ; Clear ccount
48 ; Get the size parameter and replace it by a pointer to outdesc. This is to
49 ; build a stack frame for the call to _printf.
50 ; If size is zero, there's nothing to do.
69 ; Write size-1 to outdesc.uns
79 ; Copy buf to the outdesc.ptr
86 ; Restore ap and call _printf
93 ; Terminate the string. The last char is either at bufptr+ccount or
94 ; bufptr+bufsize, whichever is smaller.
115 ; Return the number of bytes written and drop buf
121 ; Bail out if size is zero.
127 jmp incsp6 ; Drop parameters
130 ; ----------------------------------------------------------------------------
131 ; Callback routine used for the actual output.
133 ; static void out (struct outdesc* d, const char* buf, unsigned count)
134 ; /* Routine used for writing */
136 ; Since we know, we're called with a pointer to our static outdesc structure,
137 ; we don't need the pointer passed on the stack.
141 ; Calculate the space left in the buffer. If no space is left, don't copy
144 lda bufsize+0 ; Low byte of buffer size
146 sbc ccount+0 ; Low byte of bytes already written
151 bcs @L0 ; Branch if space left
154 sta ptr1+1 ; No space left
156 ; Replace the pointer to d by a pointer to the write position in the buffer
157 ; for the call to memcpy that follows.
170 ; Get Count from stack
174 ; outdesc.ccount += Count;
185 ; if (Count > Left) Count = Left;
194 ; Jump to memcpy, which will cleanup the stack and return to the caller