1
0
mirror of https://github.com/cc65/cc65.git synced 2024-07-05 21:29:03 +00:00

Fixed a bug (report and patch by Greg King)

git-svn-id: svn://svn.cc65.org/cc65/trunk@3384 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
cuz 2005-02-14 09:59:54 +00:00
parent 9afcff6020
commit 100fbfc0d1

View File

@ -1,29 +1,28 @@
; vfprintf.s
; ;
; int vfprintf (FILE* f, const char* Format, va_list ap); ; int fastcall vfprintf(FILE* f, const char* Format, va_list ap);
;
; Ullrich von Bassewitz, 1.12.2000
; ;
; 2005-02-08, Ullrich von Bassewitz
; 2005-02-11, Greg King
.export _vfprintf .export _vfprintf
.import pushax, popax, push1, pushwysp, ldaxysp, ldaxidx, incsp6 .import pushax, popax, push1, pushwysp, ldaxysp, ldaxidx, incsp6
.import _fwrite, __printf .import _fwrite, __printf
.importzp sp, ptr1, ptr2 .importzp sp, ptr1, ptr2
.macpack generic .macpack generic
.data .data
; ---------------------------------------------------------------------------- ; ----------------------------------------------------------------------------
;
; Static data for the _vfprintf routine ; Static data for the _vfprintf routine
; ;
outdesc: ; Static outdesc structure
outdesc: ; Static outdesc structure ccount: .res 2
.word 0 ; ccount .word out ; Output function pointer
.word out ; Output function pointer ptr: .res 2 ; Points to output file
.word 0 ; ptr .res 2 ; (Not used by this function)
.word 0 ; uns
.code .code
@ -33,24 +32,20 @@ outdesc: ; Static outdesc structure
; static void out (struct outdesc* d, const char* buf, unsigned count) ; static void out (struct outdesc* d, const char* buf, unsigned count)
; /* Routine used for writing */ ; /* Routine used for writing */
; { ; {
; register size_t cnt;
; /* Write to the file */ ; /* Write to the file */
; if (fwrite (buf, count, 1, (FILE*) d->ptr) == -1) { ; if ((cnt = fwrite(buf, 1, count, (FILE *)d->ptr)) == 0) {
; d->ccount = -1; ; d->ccount = -1;
; } else { ; } else {
; d->ccount += count; ; d->ccount += cnt;
; } ; }
; } ; }
out:
; About to call ; About to call
; ;
; fwrite (buf, 1, count, (FILE*) d->ptr); ; fwrite (buf, 1, count, (FILE*) d->ptr);
; ;
; Since Buf and Count are already in place, we will just push the last out: ldy #5
; two parameters. The fwrite function will remove Buf and Count on exit.
ldy #5
jsr pushwysp ; Push buf jsr pushwysp ; Push buf
jsr push1 ; Push #1 jsr push1 ; Push #1
ldy #7 ldy #7
@ -59,9 +54,9 @@ out:
jsr ldaxysp ; Load D jsr ldaxysp ; Load D
ldy #5 ; Offset of ptr1+1 in struct outdesc ldy #5 ; Offset of ptr1+1 in struct outdesc
jsr ldaxidx ; Load jsr ldaxidx ; Load
jsr _fwrite jsr _fwrite
sta ptr2 ; Save function result sta ptr2 ; Save function result
stx ptr2+1 stx ptr2+1
; Get D and store it in ptr1 ; Get D and store it in ptr1
@ -72,41 +67,49 @@ out:
; Load the offset of ccount in struct outdesc ; Load the offset of ccount in struct outdesc
ldy #$00 ldy #$00
; Check the return code. Checking the high byte against $FF is ok here. ; Check the return value.
lda ptr2+1 lda ptr2+1
cmp #$FF ora ptr2
bne @Ok bne @Ok
; We had an error. Store -1 into d->ccount ; We had an error. Store -1 into d->ccount
sta (ptr1),y .ifp02
lda #<-1
.else
dec a
.endif
sta (ptr1),y
iny iny
bne @Done ; Branch always bne @Done ; Branch always
; Result was ok, count bytes written ; Result was ok, count bytes written
@Ok: lda (ptr1),y @Ok: lda (ptr1),y
add ptr2 add ptr2
sta (ptr1),y sta (ptr1),y
iny iny
lda (ptr1),y lda (ptr1),y
adc ptr2+1 adc ptr2+1
@Done: sta (ptr1),y @Done: sta (ptr1),y
jmp incsp6 ; Drop stackframe jmp incsp6 ; Drop stackframe
; ---------------------------------------------------------------------------- ; ----------------------------------------------------------------------------
; vfprintf - formatted output ; vfprintf - formatted output
; ;
; int vfprintf (FILE* f, const char* format, va_list ap) ; int fastcall vfprintf(FILE* f, const char* format, va_list ap)
; { ; {
; struct outdesc d; ; static struct outdesc d = {
; 0,
; out
; };
; ;
; /* Setup descriptor */ ; /* Setup descriptor */
; d.fout = out; ; d.ccount = 0;
; d.ptr = f; ; d.ptr = f;
; ;
; /* Do formatting and output */ ; /* Do formatting and output */
@ -115,39 +118,39 @@ out:
; /* Return bytes written */ ; /* Return bytes written */
; return d.ccount; ; return d.ccount;
; } ; }
;
_vfprintf: _vfprintf:
pha ; Save low byte of ap pha ; Save low byte of ap
; Setup the outdesc structure ; Setup the outdesc structure
lda #0 lda #0
sta outdesc sta ccount
sta outdesc+1 ; Clear ccount sta ccount+1 ; Clear character-count
; Reorder the stack. Replace f on the stack by &d, so the stack frame is ; Reorder the stack. Replace f on the stack by &d, so the stack frame is
; exactly as _printf expects it. Parameters will get dropped by _printf. ; exactly as _printf expects it. Parameters will get dropped by _printf.
ldy #2 ; ldy #2
lda (sp),y ; Low byte of f lda (sp),y ; Low byte of f
sta outdesc+4 ; sta ptr
lda #<outdesc lda #<outdesc
sta (sp),y sta (sp),y
iny iny
lda (sp),y ; High byte of f lda (sp),y ; High byte of f
sta outdesc+5 sta ptr+1
lda #>outdesc lda #>outdesc
sta (sp),y sta (sp),y
; Restore low byte of ap and call _printf ; Restore low byte of ap and call _printf
pla pla
jsr __printf jsr __printf
; Return the number of bytes written ; Return the number of bytes written
lda outdesc lda ccount
ldx outdesc+1 ldx ccount+1
rts rts