1
0
mirror of https://github.com/cc65/cc65.git synced 2024-06-07 23:29:39 +00:00

Fixed more bugs, shortened the code where possible.

git-svn-id: svn://svn.cc65.org/cc65/trunk@4257 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
uz 2009-09-28 18:39:55 +00:00
parent 6c8fab8e51
commit e4d9fd3bd1
2 changed files with 112 additions and 101 deletions

View File

@ -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
iny
lda (sp),y
sta ptr1+1
ora ptr1
beq L9
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
iny
lda (sp),y
sta ptr1+1
ora ptr1
beq L9
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

View File

@ -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