printf itoa utoa

This commit is contained in:
MonkeyGG2 2024-02-18 11:03:37 -05:00
parent 5aa2889ca0
commit a5822482db
3 changed files with 84 additions and 4 deletions

View File

@ -16,4 +16,9 @@ typedef char s8;
typedef unsigned int size_t; typedef unsigned int size_t;
typedef int ssize_t; typedef int ssize_t;
typedef unsigned char *va_list;
#define va_start(list, param) (list = (((va_list)&param) + sizeof(param)))
#define va_arg(list, type) (*(type *)((list += sizeof(type)) - sizeof(type)))
#define va_end(list) (list = NULL)
#endif #endif

View File

@ -1,4 +1,5 @@
#include <types.h> #include <types.h>
static char strbuf[32];
u32 u32
strlen(const char *str) strlen(const char *str)
@ -10,10 +11,82 @@ strlen(const char *str)
return i; return i;
} }
char*
itoa(s32 num, char *str, s32 base)
{
s32 i = 0;
if (num < 0 && base == 10) {
str[i++] = '-';
num = -num;
}
while (num != 0) {
s32 remainder = num % base;
str[i++] = (remainder < 10) ? remainder + '0' : remainder + 'a' - 10;
num/=base;
}
str[i] = '\0';
return str;
}
char*
utoa(u32 num, char *str, s32 base)
{
s32 i = 0;
while (num != 0) {
s32 remainder = num % base;
str[i++] = (remainder < 10) ? remainder + '0' : remainder + 'a' - 10;
num/=base;
}
str[i] = '\0';
return str;
}
s32
printf(const char *format, ...) printf(const char *format, ...)
{ {
const u32 len = strlen(format); const u32 len = sizeof(format);
va_list list;
va_start(list, len);
vga_write(format, len, 0x0f); for (u32 i = 0; i < len; i++) {
return len; if (format[i] == '%') {
if (format[i + 1] == 'd' || format[i + 1] == 'i') {
//integer numbers
const s32 num = va_arg(list, s32);
vga_write(itoa(num, strbuf, 10), 1, 0x0f);
} else if (format[i + 1] == 'o') {
//octal numbers
const s32 num = va_arg(list, s32);
vga_write(itoa(num, strbuf, 8), 1, 0x0f);
} else if (format[i + 1] == 'u') {
//unsigned integers
const u32 num = va_arg(list, u32);
vga_write(utoa(num, strbuf, 10), 1, 0x0f);
} else if (format[i + 1] == 'x') {
//hexadecimal numbers
const u32 num = va_arg(list, u32);
vga_write(utoa(num, strbuf, 16), 1, 0x0f);
} else if (format[i + 1] == 's') {
//strings
const char *str = va_arg(list, char*);
vga_write(str, strlen(str), 0x0f);
} else if (format[i + 1] == 'c') {
//one character
vga_write(va_arg(list, char), 1, 0x0f);
} else if (format[i + 1] == '%') {
vga_write('%', 1, 0x0f);
}
} else if (format[i] == '\\' && format[i + 1] == '%') {
vga_write('%', 1, 0x0f);
} else {
vga_write(format[i], 1, 0x0f);
}
}
} }

View File

@ -11,7 +11,7 @@ sysinit(void)
for (s16 i = 0; i < 100; i++) { for (s16 i = 0; i < 100; i++) {
vga_write(howdy, strlen(howdy), 0x1f); vga_write(howdy, strlen(howdy), 0x1f);
vga_write(howday, strlen(howday), 0x0f); vga_write(howday, strlen(howday), 0x0f);
} }
vga_write("sid", 3, 0x07); vga_write("sid", 3, 0x07);
vga_write("did", 3, 0x07); vga_write("did", 3, 0x07);
@ -47,4 +47,6 @@ sysinit(void)
"bob", 2003, 0x0F); "bob", 2003, 0x0F);
vga_write("indian", 6, 0x07); vga_write("indian", 6, 0x07);
vga_write("indian", 6, 0x07); vga_write("indian", 6, 0x07);
printf("%s%x", "Omh", 0x2a);
} }