This commit is contained in:
nub31
2025-06-26 13:30:22 +02:00
parent 7d44366ce5
commit d88f5566c6
4 changed files with 47 additions and 24 deletions

View File

@@ -1 +1 @@
runtime.o *.o

View File

@@ -1,5 +1,5 @@
runtime: x86_64:
cc -c runtime.c gcc -nostdlib -ffreestanding -c platform/x86_64.s runtime.c
clean: clean:
rm runtime.o rm runtime.o

View File

@@ -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

View File

@@ -1,16 +1,17 @@
#include <stdint.h> #include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/syscall.h>
#include <syscall.h>
#include <unistd.h>
#define NUB_PANIC_ERROR_CODE 101 #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() uint64_t nub_string_length()
@@ -18,32 +19,37 @@ uint64_t nub_string_length()
return 0; 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() void nub_panic_array_oob()
{ {
puts("Array out of bounds"); nub_write_string("Array out of bounds\n");
exit(NUB_PANIC_ERROR_CODE); nub_exit(NUB_PANIC_ERROR_CODE);
} }
void nub_panic_oom() void nub_panic_oom()
{ {
puts("Out of memory"); nub_write_string("Out of memory\n");
exit(NUB_PANIC_ERROR_CODE); nub_exit(NUB_PANIC_ERROR_CODE);
} }
extern int64_t main(); extern int main();
void _start() __attribute__((noreturn)) void _start()
{ {
int64_t exit_code = main(); int exit_code = main();
exit(exit_code); nub_exit(exit_code);
__builtin_unreachable();
} }