From 57fc34cb2d8ba78b78a4ed387fbb77b2f5439111 Mon Sep 17 00:00:00 2001 From: Kamila Szewczyk Date: Sun, 8 Oct 2023 14:58:45 +0200 Subject: [PATCH] memcmp --- Makefile | 2 +- lib83/memcmp.c | 13 +++++++++++++ lib83/string.h | 6 +++--- 3 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 lib83/memcmp.c diff --git a/Makefile b/Makefile index fba1954..0de944f 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ CC=sdcc CFLAGS=-Ilib83 -c -mz80 --std-sdcc2x --no-std-crt0 --reserve-regs-iy --opt-code-size OBJS=_crt0.rel clrscr.rel putchar.rel puts.rel exit.rel gotoxy.rel __assert_fail.rel \ - getchar.rel put_int.rel ctype.rel memcpy.rel memset.rel memmove.rel \ + getchar.rel put_int.rel ctype.rel memcpy.rel memset.rel memmove.rel memcmp.rel \ main.rel .PHONY: all clean try diff --git a/lib83/memcmp.c b/lib83/memcmp.c new file mode 100644 index 0000000..2064e9c --- /dev/null +++ b/lib83/memcmp.c @@ -0,0 +1,13 @@ + +#include + +int8_t memcmp(void * buf1, void * buf2, size_t count) { + if (!count) return 0; + + while (--count && *(unsigned char*)buf1 == *(unsigned char*)buf2) { + buf1 = 1 + (unsigned char *)buf1; buf2 = 1 + (unsigned char *)buf2; + } + + return *(char*)buf1 - *(char*)buf2; +} + diff --git a/lib83/string.h b/lib83/string.h index 0268d4c..a039288 100644 --- a/lib83/string.h +++ b/lib83/string.h @@ -7,13 +7,13 @@ extern void *memcpy(void *dest, const void *src, size_t n); extern void *memset(void *s, unsigned char c, size_t n); extern void *memmove(void *dest, const void *src, size_t n); -extern int memcmp(const void *s1, const void *s2, size_t n); +extern int8_t memcmp(const void *s1, const void *s2, size_t n); extern char *strcpy(char *dest, const char *src); extern char *strncpy(char *dest, const char *src, size_t n); extern size_t strlen(const char *s); -extern int strcmp(const char *s1, const char *s2); -extern int strncmp(const char *s1, const char *s2, size_t n); +extern int8_t strcmp(const char *s1, const char *s2); +extern int8_t strncmp(const char *s1, const char *s2, size_t n); #endif