From: Duje Mihanović Date: Fri, 13 May 2022 18:19:23 +0000 (+0200) Subject: Add ability to print numbers in decimal X-Git-Tag: 0.1.1~4 X-Git-Url: https://git.dujemihanovic.xyz/%7B%7B%20%24style.RelPermalink%20%7D%7D?a=commitdiff_plain;h=e8c050c11b7f05ec562888d6a8129ab841d163fb;p=nameless-os.git Add ability to print numbers in decimal --- diff --git a/Makefile b/Makefile index 2eedce2..0e56c23 100644 --- a/Makefile +++ b/Makefile @@ -19,6 +19,9 @@ bootloader: $(BOOTLOADER_OBJ) run: all $(QEMU) boot/x86/disk.img +debug: all + $(QEMU) -s -S boot/x86/disk.img + boot/x86/mbr: boot/x86/mbr.s boot/x86/vbr-fat32: boot/x86/vbr-fat32.s boot/x86/fat32/*.s boot/x86/stage3/LOADER.BIN: boot/x86/stage3/*.s boot/x86/stage3/*.c boot/x86/fat32/*.s @@ -54,4 +57,4 @@ clean: -rm kernel/kernel.{bin,dbg,elf} ${KERNEL_OBJ} cd boot/x86 && $(MAKE) clean -.PHONY: default all clean run bootloader +.PHONY: default all clean run debug bootloader diff --git a/include/arch/x86/tty.h b/include/arch/x86/tty.h index 3ddd6b5..e141069 100644 --- a/include/arch/x86/tty.h +++ b/include/arch/x86/tty.h @@ -27,4 +27,5 @@ extern void kprintb(const uint8_t byte); extern void kprintw(const uint16_t word); extern void kprintd(const uint32_t dword); extern void kprintq(const uint64_t qword); +extern int kprintdec(uint32_t num); #endif diff --git a/kernel/arch/x86/tty/tty.c b/kernel/arch/x86/tty/tty.c index 2110a7d..861610d 100644 --- a/kernel/arch/x86/tty/tty.c +++ b/kernel/arch/x86/tty/tty.c @@ -175,3 +175,38 @@ void kprintq(uint64_t qword) temp = qword & 0xF; kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY); } + +int kprintdec(uint32_t num) +{ + char buffer[11]; + int digits = 10; + /* TODO: make an actual memset function to use instead of this */ + for (int i=0; i<11; i++) { + buffer[i] = 0; + } + + /* put the numbers in the buffer */ + for (int i=9; i>0 && num>0; i--) { + uint8_t currdigit = num%10; + buffer[i] = currdigit+'0'; + num /= 10; + } + + /* shift the array as much as needed */ + while (*buffer == '\0') { + digits--; + for (int i=0; i<9; i++) { + buffer[i] = buffer[i+1]; + } + } + + /* zero out any leftovers */ + if (digits < 10) { + for (int i=digits; i<10; i++) { + buffer[i] = '\0'; + } + } + + kprint(buffer, 0); + return digits; +} diff --git a/kernel/kernel.c b/kernel/kernel.c index 9257a0e..16b2b40 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -59,5 +59,9 @@ void kmain(struct e820_map *mmap, int mmap_size) pic_unmask(1); asm volatile ("sti"); kprint("All done\n", 0); + kprintdec(12345); + kprintc('\n'); + kprintdec(6789); + kprintc('\n'); while(1); }