diff --git a/src/kernel.c b/src/kernel.c index 31ef846..ea048b4 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -8,12 +8,10 @@ void kmain() { vga_clear(); - pic_remap(); enable_interrupts(); - kprintf("\nWelcome to nub OS\n"); - + kprintf("Welcome to nub OS\n"); khalt(); } @@ -28,12 +26,18 @@ void kprintf(const char* fmt, ...) va_list args; va_start(args, fmt); + bool should_format = false; + for (size_t i = 0; fmt[i] != '\0'; i++) { - if (fmt[i] == '%') + if (should_format) { - i++; - if (fmt[i] == 's') + should_format = false; + if (fmt[i] == '%') + { + vga_put_char('%', 0x0F); + } + else if (fmt[i] == 's') { const char* str = va_arg(args, const char*); for (size_t j = 0; str[j] != '\0'; j++) @@ -43,20 +47,29 @@ void kprintf(const char* fmt, ...) } else if (fmt[i] == 'd') { - int val = va_arg(args, int); - char buf[12]; - itoa(val, buf); + int64_t val = va_arg(args, int64_t); + char buf[21]; + itoa64(val, buf); for (size_t j = 0; buf[j] != '\0'; j++) { vga_put_char(buf[j], 0x0F); } } - else + else if (fmt[i] == 'u') { - vga_put_char('%', 0x0F); - vga_put_char(fmt[i], 0x0F); + uint64_t val = va_arg(args, uint64_t); + char buf[21]; + uitoa64(val, buf); + for (size_t j = 0; buf[j] != '\0'; j++) + { + vga_put_char(buf[j], 0x0F); + } } } + else if (fmt[i] == '%') + { + should_format = true; + } else { vga_put_char(fmt[i], 0x0F); diff --git a/src/string.c b/src/string.c index 01ab9b5..9d2492d 100644 --- a/src/string.c +++ b/src/string.c @@ -25,64 +25,77 @@ void reverse(char* str, size_t length) } } -int uitoa(uint64_t value, char* buffer) +void itoa64(int64_t value, char* buffer) { - int i = 0; - if (value == 0) - { - buffer[i++] = '0'; - buffer[i] = '\0'; - return i; - } - - while (value > 0) - { - buffer[i++] = (value % 10) + '0'; - value /= 10; - } - - buffer[i] = '\0'; - reverse(buffer, i); - return i; -} - -int itoa(int64_t value, char* buffer) -{ - int i = 0; - - if (value == 0) - { - buffer[i++] = '0'; - buffer[i] = '\0'; - return i; - } - - bool negative = false; - uint64_t v; + char temp[21]; + int i = 0, j = 0; + int negative = 0; if (value < 0) { - negative = true; - v = (uint64_t)(-value); - } - else - { - v = (uint64_t)value; + negative = 1; + if (value == INT64_MIN) + { + const char* min_str = "9223372036854775808"; + for (i = 0; min_str[i] != '\0'; i++) + temp[i] = min_str[i]; + i = 19; + } + else + { + value = -value; + } } - while (v > 0) + if (value == 0 && !negative) { - buffer[i++] = (v % 10) + '0'; - v /= 10; + buffer[0] = '0'; + buffer[1] = '\0'; + return; + } + + if (!(negative && value == INT64_MIN)) + { + while (value > 0) + { + temp[i++] = '0' + (value % 10); + value /= 10; + } } if (negative) { - buffer[i++] = '-'; + temp[i++] = '-'; } - buffer[i] = '\0'; - reverse(buffer, i); + while (i > 0) + { + buffer[j++] = temp[--i]; + } + buffer[j] = '\0'; +} - return i; +void uitoa64(uint64_t value, char* buffer) +{ + char temp[21]; + int i = 0, j = 0; + + if (value == 0) + { + buffer[0] = '0'; + buffer[1] = '\0'; + return; + } + + while (value > 0) + { + temp[i++] = '0' + (value % 10); + value /= 10; + } + + while (i > 0) + { + buffer[j++] = temp[--i]; + } + buffer[j] = '\0'; } \ No newline at end of file diff --git a/src/string.h b/src/string.h index 49ed2aa..1abefcf 100644 --- a/src/string.h +++ b/src/string.h @@ -5,5 +5,5 @@ int strcmp(const char* a, const char* b); void reverse(char* str, size_t length); -int uitoa(uint64_t value, char* buffer); -int itoa(int64_t value, char* buffer); \ No newline at end of file +void uitoa64(uint64_t value, char* buffer); +void itoa64(int64_t value, char* buffer); \ No newline at end of file