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

View File

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

View File

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

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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/syscall.h>
#include <syscall.h>
#include <unistd.h>
#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);
int exit_code = main();
nub_exit(exit_code);
__builtin_unreachable();
}