From: Duje Mihanović Date: Fri, 13 May 2022 18:19:23 +0000 (+0200) Subject: Add ability to print numbers in decimal X-Git-Url: https://git.dujemihanovic.xyz/?a=commitdiff_plain;h=49dc1d078bb1a5530e452aa800c8871c18808340;p=nameless-os.git Add ability to print numbers in decimal --- diff --git a/Makefile b/Makefile index aee8cc1..f5a33b6 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ QEMU = qemu-system-i386 -monitor stdio export GIT_REV = $(shell git describe --long HEAD) -CFLAGS = -std=gnu89 -g -Iinclude/arch/x86 -ffreestanding -DGIT_COMMIT=\"$(GIT_REV)\" +CFLAGS = -std=gnu99 -g -Iinclude/arch/x86 -ffreestanding -DGIT_COMMIT=\"$(GIT_REV)\" KERNEL_OBJ = kernel/entry.o kernel/arch/x86/tty/tty.o kernel/drivers/irq/i8259a.o kernel/arch/x86/irq/idt.o kernel/arch/x86/irq/sample_handler.o kernel/kernel.o BOOTLOADER_OBJ = boot/x86/mbr boot/x86/vbr-fat32 boot/x86/stage3/LOADER.BIN @@ -18,6 +18,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/fat32/*.s @@ -52,4 +55,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 2224737..42857ac 100644 --- a/include/arch/x86/tty.h +++ b/include/arch/x86/tty.h @@ -25,4 +25,5 @@ extern void kprint(const char *string, uint8_t color); extern void kprintb(const uint8_t byte); extern void kprintw(const uint16_t word); extern void kprintd(const uint32_t dword); +extern int kprintdec(uint32_t num); #endif diff --git a/kernel/arch/x86/tty/tty.c b/kernel/arch/x86/tty/tty.c index 07d734e..51686a3 100644 --- a/kernel/arch/x86/tty/tty.c +++ b/kernel/arch/x86/tty/tty.c @@ -137,3 +137,38 @@ void kprintd(uint32_t dword) temp = dword & 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 4c6efd8..24deeb9 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -27,5 +27,9 @@ void kmain(void) pic_unmask(1); asm volatile ("sti"); kprint("All done\n", 0); + kprintdec(12345); + kprintc('\n'); + kprintdec(6789); + kprintc('\n'); while(1); }