Copyright © 2007 Christopher Williams (abbrev@gmail.com).
This is an implementation for the Z80 of the string handling functions found in the ANSI C Standard Library. All of the functions, except for two, are implemented. The "POSIX" locale is assumed in functions where applicable. Here is a complete list of the functions in the Standard Library:
*These functions are not implemented (yet).
Note: Strings are zero-terminated (end with a 0 byte), as in C; this allows them to be any length.
For reference, here are the C function prototypes for all of the functions:
void *memchr(const void *s, int c, size_t n);
int memcmp(const void *s1, const void *s2, size_t n);
void *memcpy(void *s1, const void *s2, size_t n);
void *memmove(void *s1, const void *s2, size_t n);
void *memset(void *s, int c, size_t n);
char *strcat(char *s1, const char *s2);
char *strchr(const char *s, int c);
int strcmp(const char *s1, const char *s2);
int strcoll(const char *s1, const char *s2);
char *strcpy(char *s1, const char *s2);
size_t strcspn(const char *s1, const char *s2);
char *strerror(int errcode);
size_t strlen(const char *s);
char *strncat(char *s1, const char *s2, size_t n);
int strncmp(const char *s1, const char *s2, size_t n);
char *strncpy(char *s1, const char *s2, size_t n);
char *strpbrk(const char *s1, const char *s2);
char *strrchr(const char *s, int c);
size_t strspn(const char *s1, const char *s2);
char *strstr(const char *s1, const char *s2);
char *strtok(char *s1, const char *s2);
size_t strxfrm(char *s1, const char *s2, size_t n);
All of the functions follow a common convention for arguments and return values.
HL
contains its address.DE
contains the address of the first string, and HL
contains the address of the second string.BC
contains the length.E
contains the character.HL
contains the address of a string or character.A
contains the result of a comparison:
A
< 0 if s1
< s2
A
= 0 if s1
= s2
A
> 0 if s1
> s2
Here are the inputs and outputs of each of the functions in detail:
HL
= s
BC
= n
(number of bytes in s
)E
= c
(byte to find)HL
= address of matching byte, or NULL if byte is not foundDE
= s1
HL
= s2
BC
= n
A
< 0 if s1
< s2
A
= 0 if s1
= s2
A
> 0 if s1
> s2
DE
= dest
HL
= src
BC
= n
(number of bytes to copy)HL
= dest
DE
= dest
HL
= src
BC
= n
(number of bytes to copy)HL
= dest
HL
= s
BC
= n
(number of bytes to copy)E
= c
(byte to fill)HL
= s
DE
= dest
HL
= src
HL
= dest
HL
= s
E
= c
(character to find)HL
= address of matching byte, or NULL if character is not foundDE
= s1
HL
= s2
A
< 0 if s1
< s2
A
= 0 if s1
= s2
A
> 0 if s1
> s2
DE
= dest
HL
= src
HL
= dest
DE
= s
HL
= reject
HL
= number of characters in the initial segment of s
which are not in reject
HL
= s
HL
= number of characters in s
DE
= dest
HL
= src
BC
= n
(maximum number of bytes to copy from src
)HL
= dest
DE
= s1
HL
= s2
BC
= n
(maximum number of bytes to compare)A
< 0 if s1
< s2
A
= 0 if s1
= s2
A
> 0 if s1
> s2
DE
= dest
HL
= src
BC
= n
(maximum number of bytes to copy)HL
= dest
DE
= s
HL
= accept
HL
= address of character in s
that matches one of the characters in accept
, or NULL if no such character is foundHL
= s
E
= c
(character to find)HL
= address of last matching byte, or NULL if character is not foundDE
= s
HL
= accept
HL
= number of characters in the initial segment of s
which consist only of characters from accept
DE
= haystack
HL
= needle
HL
= address of needle
in haystack
DE
= dest
HL
= src
BC
= n
(maximum number of characters to copy)HL
= number of bytes required to store the transformed string in dest
excluding the terminating '\0' character.In this implementation, some functions depend on one or more other functions to work correctly. The following table lists these functions and their dependencies.
Function | Depends on |
---|---|
strcat | strcpy |
strcspn | strchr |
strpbrk | strchr |
strspn | strchr |
strstr | strlen and strncmp |
strxfrm | strchr and strlen |
This means that, for example, if you include "strcat.asm", you should also include "strcpy.asm".
There are no known bugs in this version. However, two of the functions in the Standard (strerror
and strtok
) are missing in this implementation. I did not implement strerror
because it's almost useless without more of the C Standard Library. I did not implement strtok
for a few reasons, one being that it is not reentrant (it stores static data), another being that it modifies the original string. I may write strtok
in a future version, but for now it's missing.
strcmp
function.