forked from cc65/cc65
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request cc65#2382 from colinleroy/asm-gets
Rewrite gets in asm
- Loading branch information
Showing
3 changed files
with
93 additions
and
63 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
; | ||
; Colin Leroy-Mira, 2024 | ||
; | ||
; char* __fastcall__ gets (char* s) | ||
; | ||
|
||
.export _gets | ||
.import _fgets, _stdin, popax, pushax | ||
.importzp ptr4 | ||
|
||
_gets: | ||
; Push buffer | ||
sta ptr4 | ||
stx ptr4+1 | ||
jsr pushax | ||
|
||
; Push size (there's no limit!) | ||
lda #$FF | ||
tax | ||
jsr pushax | ||
|
||
lda _stdin | ||
ldx _stdin+1 | ||
|
||
jsr _fgets | ||
|
||
; Check return value | ||
bne :+ | ||
cpx #$00 | ||
bne :+ | ||
rts | ||
|
||
: ; At least one byte written. | ||
jsr pushax ; Store returned pointer | ||
|
||
; Remove \n if there is one. | ||
lda ptr4 ; _fgets returns with ptr4 at | ||
bne :+ ; end of buffer | ||
dec ptr4+1 | ||
: dec ptr4 | ||
lda (ptr4),y ; _fgets returns with Y=0 | ||
cmp #$0A | ||
bne :+ | ||
tya | ||
sta (ptr4),y ; Set terminator over \n | ||
|
||
: jmp popax |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
!!DESCRIPTION!! gets test | ||
!!LICENCE!! Public domain | ||
*/ | ||
|
||
#include "common.h" | ||
|
||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <fcntl.h> | ||
#include <ctype.h> | ||
|
||
char buf[512]; | ||
|
||
#define INFILE "cf.in" | ||
|
||
#ifndef __CC65__ | ||
/* Force declaration on host compiler, as gets() is deprecated for | ||
* being dangerous as hell */ | ||
char *gets (char *__s); | ||
#endif | ||
|
||
#ifdef NO_OLD_FUNC_DECL | ||
int main(int argc,char **argv) | ||
#else | ||
main(argc, argv) | ||
int argc; | ||
char *argv[]; | ||
#endif | ||
{ | ||
/* Fake stdin with the reference file */ | ||
fclose(stdin); | ||
stdin = fopen(INFILE, "r"); | ||
if (stdin == NULL) { | ||
return EXIT_FAILURE; | ||
} | ||
|
||
while (gets(buf) != NULL) | ||
{ | ||
printf("%s",buf); | ||
} | ||
|
||
fclose(stdin); | ||
return 0; | ||
} |