diff --git a/libsrc/common/vsnprintf.s b/libsrc/common/vsnprintf.s index 10dc7fa1d..21c358780 100644 --- a/libsrc/common/vsnprintf.s +++ b/libsrc/common/vsnprintf.s @@ -4,7 +4,7 @@ ; Ullrich von Bassewitz, 2009-09-26 ; - .export _vsnprintf + .export _vsnprintf, vsnprintf .import ldaxysp, popax, incsp2, incsp6 .import _memcpy, __printf .importzp sp, ptr1 @@ -26,6 +26,107 @@ bufsize:.word 0 ; Buffer size .code +; ---------------------------------------------------------------------------- +; vsprintf - formatted output into a buffer +; +; int __fastcall__ vsnprintf (char* buf, size_t size, const char* format, va_list ap); +; + +_vsnprintf: + pha ; Save ap + txa + pha + +; Setup the outdesc structure. This is also an additional entry point for +; vsprintf with ap on stack + +vsnprintf: + lda #0 + sta ccount+0 + sta ccount+1 ; Clear ccount + +; Get the size parameter and replace it by a pointer to outdesc. This is to +; build a stack frame for the call to _printf. +; If size is zero, there's nothing to do. + + ldy #2 + lda (sp),y + sta ptr1 + + lda #outdesc + sta (sp),y + +; Write size-1 to outdesc.uns + + ldy ptr1+1 + ldx ptr1 + bne L1 + dey +L1: dex + stx bufsize+0 + sty bufsize+1 + +; Copy buf to the outdesc.ptr + + ldy #5 + jsr ldaxysp + sta bufptr+0 + stx bufptr+1 + +; Restore ap and call _printf + + pla + tax + pla + jsr __printf + +; Terminate the string. The last char is either at bufptr+ccount or +; bufptr+bufsize, whichever is smaller. + + lda ccount+0 + ldx ccount+1 + cpx bufsize+1 + bne L2 + cmp bufsize+0 +L2: bcc L3 + lda bufsize+0 + ldx bufsize+1 + clc +L3: adc bufptr+0 + sta ptr1 + txa + adc bufptr+1 + sta ptr1+1 + + lda #0 + tay + sta (ptr1),y + +; Return the number of bytes written and drop buf + + lda ccount+0 + ldx ccount+1 + jmp incsp2 + +; Bail out if size is zero. + +L9: pla + pla ; Discard ap + lda #0 + tax + jmp incsp6 ; Drop parameters + + ; ---------------------------------------------------------------------------- ; Callback routine used for the actual output. ; @@ -40,17 +141,17 @@ out: ; Calculate the space left in the buffer. If no space is left, don't copy ; any characters - lda bufsize ; Low byte of buffer size + lda bufsize+0 ; Low byte of buffer size sec sbc ccount+0 ; Low byte of bytes already written sta ptr1 lda bufsize+1 sbc ccount+1 sta ptr1+1 - bcs @L0 ; Space left - lda #0 + bcs @L0 ; Branch if space left + lda #$00 sta ptr1 - sta ptr1+1 + sta ptr1+1 ; No space left ; Replace the pointer to d by a pointer to the write position in the buffer ; for the call to memcpy that follows. @@ -95,92 +196,4 @@ out: @L2: jmp _memcpy -; ---------------------------------------------------------------------------- -; vsprintf - formatted output into a buffer -; -; int __fastcall__ vsnprintf (char* buf, size_t size, const char* format, va_list ap); -; - -_vsnprintf: - pha ; Save low byte of ap - -; Setup the outdesc structure - - lda #0 - sta ccount+0 - sta ccount+1 ; Clear ccount - -; Get the size parameter and replace it by a pointer to outdesc. This is to -; build a stack frame for the call to _printf. -; If size is zero, there's nothing to do. - - ldy #2 - lda (sp),y - sta ptr1 - lda #outdesc - sta (sp),y - -; Write size-1 to outdesc.uns - - ldx ptr1 - ldy ptr1+1 - dex - bne L1 - dey -L1: stx bufsize+0 - sty bufsize+1 - -; Copy buf to the outdesc.ptr - - ldy #5 - jsr ldaxysp - sta bufptr+0 - stx bufptr+1 - -; Restore low byte of ap and call _printf - - pla - jsr __printf - -; Terminate the string - - lda ccount+0 - ldx ccount+1 - cpx bufsize+1 - bne L2 - cmp bufsize+0 -L2: bcc L3 - lda bufsize+0 - ldx bufsize+1 - clc -L3: adc bufptr+0 - sta ptr1 - txa - adc bufptr+1 - sta ptr1+1 - - lda #0 - tay - sta (ptr1),y - -; Return the number of bytes written and drop buf - - lda ccount+0 - ldx ccount+1 - jmp incsp2 - -; Bail out if size is zero - -L9: lda #0 - tax - jmp incsp6 ; Drop parameters - diff --git a/libsrc/common/vsprintf.s b/libsrc/common/vsprintf.s index 4c265ec22..f1a2fe5d8 100644 --- a/libsrc/common/vsprintf.s +++ b/libsrc/common/vsprintf.s @@ -4,9 +4,9 @@ ; Ullrich von Bassewitz, 2009-09-26 ; - .export _vsprintf - .import pushw0sp, staxysp - .import _vsnprintf + .export _vsprintf + .import pushw0sp, staxysp + .import vsnprintf ; ---------------------------------------------------------------------------- @@ -30,10 +30,8 @@ _vsprintf: ldy #2 jsr staxysp -; Retrieve ap and contine by jumping to _vsnprintf, which will cleanup the stack +; Contine by jumping to vsnprintf, which expects ap on the CPU stack and will +; cleanup the C stack - pla - tax - pla - jmp _vsnprintf + jmp vsnprintf