summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile1
-rw-r--r--stage3/memory.asm59
-rw-r--r--stage3/memory.c42
-rw-r--r--stage3/memory.h9
-rw-r--r--stage3/memory2.c22
5 files changed, 47 insertions, 86 deletions
diff --git a/Makefile b/Makefile
index fc3adf6..0768865 100644
--- a/Makefile
+++ b/Makefile
@@ -21,7 +21,6 @@ STAGE3 = \
stage3/isr.o \
stage3/framebuffer.o \
stage3/memory.o \
- stage3/memory2.o \
stage3/paging.o \
stage3/heap.o \
stage3/font.o \
diff --git a/stage3/memory.asm b/stage3/memory.asm
deleted file mode 100644
index b4410a3..0000000
--- a/stage3/memory.asm
+++ /dev/null
@@ -1,59 +0,0 @@
-global memcpy, memmove
-
-section .text
-
-memcpy:
-.bulk_copy:
- cmp rdx, 8
- jl .bytewise_copy
- mov rax, qword[rsi]
- mov qword[rdi], rax
- sub rdx, 8
- add rdi, 8
- add rsi, 8
-.bytewise_copy:
- cmp rdx, 0
- je .return
- mov al, byte[rsi]
- mov byte[rdi], al
- dec rdx
- inc rdi
- inc rsi
- jmp .bytewise_copy
-.return:
- ret
-
-memmove:
- mov rcx, rdx
-.bulk_read:
- cmp rdx, 8
- jl .bytewise_read
- push qword[rsi]
- add rsi, 8
- sub rdx, 8
- jmp .bulk_read
-.bytewise_read:
- cmp rdx, 0
- je .bulk_write
- dec rsp
- mov al, byte[rsi]
- mov byte[rsp], al
- inc rsi
- dec rdx
- jmp .bytewise_read
-.bulk_write:
- cmp rcx, 8
- jl .bytewise_write
- pop qword[rdi]
- add rdi, 8
- sub rcx, 8
-.bytewise_write:
- cmp rcx, 0
- je .return
- mov al, byte[rsp]
- mov byte[rdi], al
- inc rsp
- inc rdi
- dec rcx
-.return:
- ret
diff --git a/stage3/memory.c b/stage3/memory.c
new file mode 100644
index 0000000..fcf5a28
--- /dev/null
+++ b/stage3/memory.c
@@ -0,0 +1,42 @@
+#include "heap.h"
+#include "memory.h"
+
+void *memcpy(void *dst, const void *src, usize bytes)
+{
+ for (usize i = 0; i < bytes; i++)
+ ((unsigned char *) dst)[i] = ((const unsigned char *) src)[i];
+}
+
+int memcpy_r(void *dst, const void *src, usize bytes)
+{
+ for (usize i = bytes; i > 0; i--)
+ ((unsigned char *) dst)[i-1] = ((const unsigned char *) src)[i-1];
+}
+
+int memcmp(const void *s1, const void *s2, usize n)
+{
+ for (usize i = 0; i < n; i++) {
+ unsigned char c1 = ((const unsigned char *) s1)[i];
+ unsigned char c2 = ((const unsigned char *) s2)[i];
+
+ if (c1 != c2)
+ return (int) c1 - (int) c2;
+ }
+
+ return 0;
+}
+
+u8 memsum(const void *ptr, usize size)
+{
+ u8 sum = 0;
+ for (usize i = 0; i < size; i++)
+ sum += ((const u8 *) ptr)[i];
+ return sum;
+}
+
+void *memset(void *ptr, int value, usize size)
+{
+ for (usize i = 0; i < size; i++)
+ ((unsigned char *) ptr)[i] = value;
+ return ptr;
+}
diff --git a/stage3/memory.h b/stage3/memory.h
index 774d596..56de49d 100644
--- a/stage3/memory.h
+++ b/stage3/memory.h
@@ -3,9 +3,10 @@
#include "def.h"
-void memcpy(void *dst, const void *src, usize bytes); // memory.asm
-void memmove(void *dst, const void *src, usize bytes); // memory.asm
-int memcmp(const void *s1, const void *s2, usize n); // memory2.c
-u8 memsum(const void *ptr, usize size); // memory2.c
+void *memcpy(void *dst, const void *src, usize bytes);
+int memcpy_r(void *dst, const void *src, usize bytes);
+int memcmp(const void *s1, const void *s2, usize n);
+u8 memsum(const void *ptr, usize size);
+void *memset(void *s, int c, usize n);
#endif
diff --git a/stage3/memory2.c b/stage3/memory2.c
deleted file mode 100644
index cca3cb1..0000000
--- a/stage3/memory2.c
+++ /dev/null
@@ -1,22 +0,0 @@
-#include "memory.h"
-
-int memcmp(const void *s1, const void *s2, usize n)
-{
- for (usize i = 0; i < n; i++) {
- unsigned char c1 = ((const unsigned char *) s1)[i];
- unsigned char c2 = ((const unsigned char *) s2)[i];
-
- if (c1 != c2)
- return (int) c1 - (int) c2;
- }
-
- return 0;
-}
-
-u8 memsum(const void *ptr, usize size)
-{
- u8 sum = 0;
- for (usize i = 0; i < size; i++)
- sum += ((const u8 *) ptr)[i];
- return sum;
-}