add: %u, %p, %x to kprintf

This commit is contained in:
starnakin 2024-09-19 18:00:18 +02:00
parent 4cc1dba5f9
commit 96179d996d
4 changed files with 46 additions and 20 deletions

6
headers/base.h Normal file
View File

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

View File

@ -1,3 +1,4 @@
#include "base.h"
#include "ctype.h" #include "ctype.h"
#include "kprintf.h" #include "kprintf.h"
#include "stdlib.h" #include "stdlib.h"
@ -5,6 +6,9 @@
#include "terminal.h" #include "terminal.h"
#include <stdarg.h> #include <stdarg.h>
#include <stdint.h>
int print_int_base(int32_t number, const char *prefix, const char *base);
static int print_flag(char flag, va_list *ap) static int print_flag(char flag, va_list *ap)
{ {
@ -12,13 +16,19 @@ static int print_flag(char flag, va_list *ap)
case '%': case '%':
return terminal_putchar('%'); return terminal_putchar('%');
case 'i': case 'i':
return terminal_writelong(va_arg(*ap, int)); return print_int_base(va_arg(*ap, int), NULL, BASE_DECA);
case 'd': case 'd':
return terminal_writelong(va_arg(*ap, int)); return print_int_base(va_arg(*ap, int), NULL, BASE_DECA);
case 'c': case 'c':
return terminal_putchar(va_arg(*ap, int)); return terminal_putchar(va_arg(*ap, int));
case 's': case 's':
return terminal_writestring(va_arg(*ap, char *)); 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; return 0;
} }

View File

@ -0,0 +1,28 @@
#include <stdint.h>
#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;
}

View File

@ -118,24 +118,6 @@ int terminal_writestring(const char *data)
return len; 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) void update_cursor(void)
{ {
uint16_t pos = screen->row * VGA_WIDTH + screen->column; uint16_t pos = screen->row * VGA_WIDTH + screen->column;