From 96179d996d9810f4e96be44d26f3036d8c9aaf65 Mon Sep 17 00:00:00 2001 From: starnakin Date: Thu, 19 Sep 2024 18:00:18 +0200 Subject: [PATCH] add: %u, %p, %x to kprintf --- headers/base.h | 6 ++++++ src/kprint/kvprintf.c | 14 ++++++++++++-- src/kprint/print_int_base.c | 28 ++++++++++++++++++++++++++++ src/terminal/put.c | 18 ------------------ 4 files changed, 46 insertions(+), 20 deletions(-) create mode 100644 headers/base.h create mode 100644 src/kprint/print_int_base.c diff --git a/headers/base.h b/headers/base.h new file mode 100644 index 0000000..eb7d151 --- /dev/null +++ b/headers/base.h @@ -0,0 +1,6 @@ +#pragma once + +#define BASE_BIN "01" +#define BASE_OCTA BASE_BIN "234567" +#define BASE_DECA BASE_OCTA "89" +#define BASE_HEXA BASE_DECA "abcdef" \ No newline at end of file diff --git a/src/kprint/kvprintf.c b/src/kprint/kvprintf.c index ee19cdf..02e4790 100644 --- a/src/kprint/kvprintf.c +++ b/src/kprint/kvprintf.c @@ -1,3 +1,4 @@ +#include "base.h" #include "ctype.h" #include "kprintf.h" #include "stdlib.h" @@ -5,6 +6,9 @@ #include "terminal.h" #include +#include + +int print_int_base(int32_t number, const char *prefix, const char *base); static int print_flag(char flag, va_list *ap) { @@ -12,13 +16,19 @@ static int print_flag(char flag, va_list *ap) case '%': return terminal_putchar('%'); case 'i': - return terminal_writelong(va_arg(*ap, int)); + return print_int_base(va_arg(*ap, int), NULL, BASE_DECA); case 'd': - return terminal_writelong(va_arg(*ap, int)); + return print_int_base(va_arg(*ap, int), NULL, BASE_DECA); case 'c': return terminal_putchar(va_arg(*ap, int)); case 's': return terminal_writestring(va_arg(*ap, char *)); + case 'p': + return print_int_base(va_arg(*ap, int), "0x", BASE_HEXA); + case 'x': + return print_int_base(va_arg(*ap, int), "0x", BASE_HEXA); + case 'u': + return print_int_base(va_arg(*ap, uint32_t), NULL, BASE_DECA); } return 0; } diff --git a/src/kprint/print_int_base.c b/src/kprint/print_int_base.c new file mode 100644 index 0000000..e285827 --- /dev/null +++ b/src/kprint/print_int_base.c @@ -0,0 +1,28 @@ +#include + +#include "string.h" +#include "terminal.h" + +int print_int_base(int32_t number, const char *prefix, const char *base) +{ + const int base_size = strlen(base); + uint64_t div = 1; + uint32_t tmp; + int rv = 0; + + if (prefix) + terminal_writestring(prefix); + if (number < 0) { + rv += terminal_putchar('-'); + tmp = -1 * number; + } else { + tmp = number; + } + while (div <= tmp / base_size) + div *= base_size; + while (div > 0) { + rv += terminal_putchar(base[tmp / div % base_size]); + div /= base_size; + } + return rv; +} \ No newline at end of file diff --git a/src/terminal/put.c b/src/terminal/put.c index 8e3bd73..906f5b9 100644 --- a/src/terminal/put.c +++ b/src/terminal/put.c @@ -118,24 +118,6 @@ int terminal_writestring(const char *data) return len; } -int terminal_writelong(long n) -{ - long div = 10; - int rv = 0; - if (n < 0) { - rv += terminal_putchar('-'); - n *= -1; - } - div = 1; - while (div <= n / 10) - div *= 10; - while (div > 0) { - rv += terminal_putchar('0' + n / div % 10); - div /= 10; - } - return rv; -} - void update_cursor(void) { uint16_t pos = screen->row * VGA_WIDTH + screen->column;