From 97b83208bbd07c59c8576b690828f60d11b7ccd9 Mon Sep 17 00:00:00 2001 From: nub31 Date: Thu, 26 Jun 2025 13:30:22 +0200 Subject: [PATCH] ... --- src/runtime/.gitignore | 2 +- src/runtime/makefile | 4 +-- src/runtime/platform/x86_64.s | 17 +++++++++++++ src/runtime/runtime.c | 48 ++++++++++++++++++++--------------- 4 files changed, 47 insertions(+), 24 deletions(-) create mode 100644 src/runtime/platform/x86_64.s diff --git a/src/runtime/.gitignore b/src/runtime/.gitignore index 7a49c57..1530978 100644 --- a/src/runtime/.gitignore +++ b/src/runtime/.gitignore @@ -1 +1 @@ -runtime.o \ No newline at end of file +*.o \ No newline at end of file diff --git a/src/runtime/makefile b/src/runtime/makefile index 00fdd78..c0aed79 100644 --- a/src/runtime/makefile +++ b/src/runtime/makefile @@ -1,5 +1,5 @@ -runtime: - cc -c runtime.c +x86_64: + gcc -nostdlib -ffreestanding -c platform/x86_64.s runtime.c clean: rm runtime.o diff --git a/src/runtime/platform/x86_64.s b/src/runtime/platform/x86_64.s new file mode 100644 index 0000000..78ef7ad --- /dev/null +++ b/src/runtime/platform/x86_64.s @@ -0,0 +1,17 @@ +.intel_syntax noprefix + +.text +.globl nub_write_string +# void nub_write_string(const char* str) +nub_write_string: + mov rsi, rdi + mov rdi, 1 + mov rax, 1 + syscall + +.text +.globl nub_exit +# void nub_exit(int code) +nub_exit: + mov rax, 60 + syscall diff --git a/src/runtime/runtime.c b/src/runtime/runtime.c index 1173ff4..a9b8729 100644 --- a/src/runtime/runtime.c +++ b/src/runtime/runtime.c @@ -1,16 +1,17 @@ #include -#include -#include -#include -#include -#include -#include #define NUB_PANIC_ERROR_CODE 101 -uint64_t nub_cstring_length(char* string) +extern void nub_write_string(const char* str); +extern void nub_exit(int code); + +uint64_t nub_cstring_length(const char* string) { - return strlen(string); + uint64_t len = 0; + while (string[len] != '\0') { + len++; + } + return len; } uint64_t nub_string_length() @@ -18,32 +19,37 @@ uint64_t nub_string_length() return 0; } -void nub_memcpy(void* source, void* destination, uint64_t length) +void nub_memcpy(uint8_t* source, uint8_t* destination, uint64_t length) { - memcpy(destination, source, length); + for (uint64_t i = 0; i < length; i++) { + destination[i] = source[i]; + } } -void nub_memset(void* destination, int value, uint64_t count) +void nub_memset(uint8_t* destination, uint8_t value, uint64_t count) { - memset(destination, value, count); + for (uint64_t i = 0; i < count; i++) { + destination[i] = value; + } } void nub_panic_array_oob() { - puts("Array out of bounds"); - exit(NUB_PANIC_ERROR_CODE); + nub_write_string("Array out of bounds\n"); + nub_exit(NUB_PANIC_ERROR_CODE); } void nub_panic_oom() { - puts("Out of memory"); - exit(NUB_PANIC_ERROR_CODE); + nub_write_string("Out of memory\n"); + nub_exit(NUB_PANIC_ERROR_CODE); } -extern int64_t main(); +extern int main(); -void _start() +__attribute__((noreturn)) void _start() { - int64_t exit_code = main(); - exit(exit_code); -} \ No newline at end of file + int exit_code = main(); + nub_exit(exit_code); + __builtin_unreachable(); +}