mirror of
https://github.com/cc65/cc65.git
synced 2024-12-27 00:29:31 +00:00
Rewrite fputc in assembly
-36 bytes, -12% cycles
This commit is contained in:
parent
ba36071022
commit
1a5a7d67a7
@ -1,41 +0,0 @@
|
||||
/*
|
||||
** fputc.c
|
||||
**
|
||||
** Ullrich von Bassewitz, 02.06.1998
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include "_file.h"
|
||||
|
||||
|
||||
|
||||
/*****************************************************************************/
|
||||
/* Code */
|
||||
/*****************************************************************************/
|
||||
|
||||
|
||||
|
||||
int __fastcall__ fputc (int c, register FILE* f)
|
||||
{
|
||||
/* Check if the file is open or if there is an error condition */
|
||||
if ((f->f_flags & _FOPEN) == 0 || (f->f_flags & (_FERROR | _FEOF)) != 0) {
|
||||
goto ReturnEOF;
|
||||
}
|
||||
|
||||
/* Write the byte */
|
||||
if (write (f->f_fd, &c, 1) != 1) {
|
||||
/* Error */
|
||||
f->f_flags |= _FERROR;
|
||||
ReturnEOF:
|
||||
return EOF;
|
||||
}
|
||||
|
||||
/* Return the byte written */
|
||||
return c & 0xFF;
|
||||
}
|
||||
|
||||
|
||||
|
72
libsrc/common/fputc.s
Normal file
72
libsrc/common/fputc.s
Normal file
@ -0,0 +1,72 @@
|
||||
;
|
||||
; Colin Leroy-Mira, 2024
|
||||
;
|
||||
; int __fastcall__ fputc (int c, FILE* f);
|
||||
;
|
||||
|
||||
.export _fputc
|
||||
.importzp ptr1
|
||||
.import _write
|
||||
.import pushax, pusha0, popax, incsp2
|
||||
.import pushptr1, popptr1, returnFFFF
|
||||
|
||||
.include "stdio.inc"
|
||||
.include "_file.inc"
|
||||
|
||||
_fputc:
|
||||
sta ptr1
|
||||
stx ptr1+1
|
||||
|
||||
jsr popax ; Get char, as we'll have
|
||||
sta c ; to return it anyway
|
||||
stx c+1
|
||||
|
||||
ldy #_FILE::f_flags
|
||||
lda (ptr1),y
|
||||
tax
|
||||
and #_FOPEN ; Check for file open
|
||||
beq ret_eof
|
||||
txa
|
||||
and #(_FERROR|_FEOF); Check for error/eof
|
||||
bne ret_eof
|
||||
|
||||
jsr pushptr1 ; Backup fp pointer
|
||||
|
||||
; Push _write parameters
|
||||
ldy #_FILE::f_fd
|
||||
lda (ptr1),y
|
||||
jsr pusha0
|
||||
|
||||
lda #<c
|
||||
ldx #>c
|
||||
jsr pushax
|
||||
|
||||
lda #$01
|
||||
ldx #$00
|
||||
|
||||
; Write
|
||||
jsr _write
|
||||
|
||||
; Check for errors
|
||||
cmp #$01
|
||||
bne set_ferror
|
||||
|
||||
; Return char
|
||||
lda c
|
||||
ldx #$00
|
||||
jmp incsp2 ; Drop fp pointer copy
|
||||
|
||||
ret_eof:
|
||||
jmp returnFFFF
|
||||
|
||||
set_ferror:
|
||||
jsr popptr1
|
||||
lda #_FERROR
|
||||
ldy #_FILE::f_flags
|
||||
ora (ptr1),y
|
||||
sta (ptr1),y
|
||||
jmp returnFFFF
|
||||
|
||||
.bss
|
||||
|
||||
c: .res 2
|
@ -32,9 +32,9 @@ _fputs:
|
||||
jsr swapstk ; Push fd, get s
|
||||
|
||||
jsr pushax ; Push s
|
||||
|
||||
|
||||
jsr _strlen ; Get length
|
||||
|
||||
|
||||
; Write
|
||||
jmp _write
|
||||
|
||||
|
39
test/ref/test_fputc.c
Normal file
39
test/ref/test_fputc.c
Normal file
@ -0,0 +1,39 @@
|
||||
/*
|
||||
!!DESCRIPTION!! fgets test
|
||||
!!LICENCE!! Public domain
|
||||
*/
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
FILE *in, *out;
|
||||
int c, err;
|
||||
|
||||
#define INFILE "cf.in"
|
||||
|
||||
int main(int argc,char **argv)
|
||||
{
|
||||
in = fopen(INFILE, "rb");
|
||||
if (in == NULL) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (fputc(c, in) != EOF) {
|
||||
printf("Error: can fputc to a file opened for reading\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
clearerr(in);
|
||||
|
||||
while ((c = fgetc(in)) != EOF) {
|
||||
fputc(c, stdout);
|
||||
}
|
||||
|
||||
fclose(in);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user