Skip to content

Commit

Permalink
Rewrite fputs in assembly
Browse files Browse the repository at this point in the history
-28 bytes, -1% cycles
  • Loading branch information
colinleroy committed Jan 30, 2024
1 parent 51b946b commit b7d61ad
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 28 deletions.
28 changes: 0 additions & 28 deletions libsrc/common/fputs.c

This file was deleted.

42 changes: 42 additions & 0 deletions libsrc/common/fputs.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
;
; Colin Leroy-Mira, 2024
;
; int __fastcall__ fputs (const char* s, register FILE* f)
;

.export _fputs
.importzp ptr1, ptr2
.import _write, _strlen
.import swapstk, pushax, returnFFFF

.include "stdio.inc"
.include "_file.inc"

_fputs:
sta ptr1
stx ptr1+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

; Push _write parameters
ldy #_FILE::f_fd
lda (ptr1),y
ldx #$00
jsr swapstk ; Push fd, get s

jsr pushax ; Push s

jsr _strlen ; Get length

; Write
jmp _write

ret_eof:
jmp returnFFFF
40 changes: 40 additions & 0 deletions test/ref/test_fputs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
!!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;
char buf[512], err;

#define INFILE "cf.in"

int main(int argc,char **argv)
{
in = fopen(INFILE, "rb");
if (in == NULL) {
return EXIT_FAILURE;
}

strcpy(buf, "test");
if (fputs(buf, in) != EOF) {
printf("Error: can fputs to a file opened for reading\n");
return EXIT_FAILURE;
}
clearerr(in);

while (fgets(buf, 512, in) != NULL) {
fputs(buf, stdout);
}

fclose(in);
return 0;
}

0 comments on commit b7d61ad

Please sign in to comment.