aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoraqua <aqua@iserlohn-fortress.net>2022-04-01 21:46:28 +0300
committeraqua <aqua@iserlohn-fortress.net>2022-08-12 10:13:59 +0300
commit9b777b088facde26c33df6c746b948caff725602 (patch)
tree3e88f6808e80a233f3e78640d1f2545489665376
parentlidt (diff)
downloadkernel-9b777b088facde26c33df6c746b948caff725602.tar.xz
printf: add %d, %u and %x
-rw-r--r--lib/stdio.h3
-rw-r--r--lib/stdio/printf.c12
-rw-r--r--lib/string.h3
-rw-r--r--lib/string/itoa.c29
-rw-r--r--src/isr.c5
-rw-r--r--src/kernel.c3
6 files changed, 47 insertions, 8 deletions
diff --git a/lib/stdio.h b/lib/stdio.h
index cf3a33f..201cce1 100644
--- a/lib/stdio.h
+++ b/lib/stdio.h
@@ -1,3 +1,6 @@
#pragma once
+/**
+ * Supports %s (string), %d (decimal), %u (unsigned), %x (hexadecimal)
+ */
int printf(const char *restrict format, ...);
diff --git a/lib/stdio/printf.c b/lib/stdio/printf.c
index f6b8ef6..269b9b6 100644
--- a/lib/stdio/printf.c
+++ b/lib/stdio/printf.c
@@ -1,8 +1,11 @@
#include <stdarg.h>
#include <stdio.h>
+#include <string.h>
#include <devices/uart_16550.h>
+static char buffer[3 * sizeof(int) + 2];
+
int
printf(const char *restrict format, ...)
{
@@ -22,6 +25,15 @@ printf(const char *restrict format, ...)
case 's':
written += uart_puts(COM1, va_arg(params, const char *), -1);
break;
+ case 'd':
+ written += uart_puts(COM1, itoa(buffer, va_arg(params, int), 10), -1);
+ break;
+ case 'u':
+ written += uart_puts(COM1, utoa(buffer, va_arg(params, unsigned int), 10), -1);
+ break;
+ case 'x':
+ written += uart_puts(COM1, utoa(buffer, va_arg(params, unsigned int), 16), -1);
+ break;
}
l = 0;
diff --git a/lib/string.h b/lib/string.h
index 361d308..9873362 100644
--- a/lib/string.h
+++ b/lib/string.h
@@ -1,3 +1,4 @@
#pragma once
-char *itoa(char *p, unsigned x);
+char *itoa(char *p, int x, int base);
+char *utoa(char *p, unsigned x, int base);
diff --git a/lib/string/itoa.c b/lib/string/itoa.c
index 5eefb37..3708be2 100644
--- a/lib/string/itoa.c
+++ b/lib/string/itoa.c
@@ -1,13 +1,30 @@
-#include "string.h"
+#include <stdbool.h>
+#include <string.h>
+
+static const char *numbers = "0123456789abcdef";
+
+char *
+itoa(char *p, int x, int base)
+{
+ const bool is_negative = (x < 0);
+ if (is_negative) x = -x;
+
+ p = utoa(p, x, base);
+
+ if (is_negative) *--p = '-';
+ return p;
+}
char *
-itoa(char *p, unsigned x)
+utoa(char *p, unsigned x, int base)
{
- p += 3 * sizeof(int);
- *--p = 0;
+ p += 3 * sizeof(unsigned);
+ *--p = '\0';
+
do {
- *--p = '0' + x % 10;
- x /= 10;
+ *--p = numbers[x % base];
+ x /= base;
} while (x);
+
return p;
}
diff --git a/src/isr.c b/src/isr.c
index c94d37e..b89ecd2 100644
--- a/src/isr.c
+++ b/src/isr.c
@@ -9,7 +9,10 @@
__attribute__((interrupt)) void
abort_handler(struct interrupt_frame *frame)
{
- printf("abort\n");
+ printf("system abort\n");
+ printf("ip: %x cs=%x\n", frame->ip, frame->cs);
+ printf("sp: %x ss=%x\n", frame->sp, frame->ss);
+ printf("flags: %x\n", frame->flags);
abort();
}
diff --git a/src/kernel.c b/src/kernel.c
index f7f9d6d..ffa8814 100644
--- a/src/kernel.c
+++ b/src/kernel.c
@@ -17,6 +17,9 @@ void kmain() {
if (uart_init(COM1) != 0) printf("UART self-test failed.\r\n");
printf("hello %s world\n", "kernel");
+ printf("we are number %d\n", 1);
+ printf("a negative %d as hex %x\n", -1, -1);
+ printf("hex 255=0x%x\n", 255);
vga_init(vmm_map(0xb8000, 0xc03ff000));