Finally implement %l and %z format modifiers

Finally, we can use %zx with a 64-bit number instead of %x%x with 2 32-bit values split from a 64-bit number!
This commit is contained in:
apio 2022-09-05 16:59:45 +02:00
parent b47b86884f
commit 1367e88d88
6 changed files with 174 additions and 26 deletions

View File

@ -1,7 +1,10 @@
#pragma once
#include <stdint.h>
char* itoa(int number, char* arr, int base);
char* utoa(unsigned int number, char* arr, int base);
char* itoa(int32_t number, char* arr, int base);
char* utoa(uint32_t number, char* arr, int base);
char* ltoa(int64_t number, char* arr, int base);
char* ultoa(uint64_t number, char* arr, int base);
void sleep(uint64_t ms);

View File

@ -1,4 +0,0 @@
#pragma once
#include <stdint.h>
#define hex64(bit64num) (uint64_t) bit64num >> 32, (uint64_t)bit64num & 0xFFFFFFFF

View File

@ -9,9 +9,8 @@ extern "C" void common_handler(SavedContext* context)
if (context->number < 0x20) { Debug::DebugStatus::the()->ThrowException(context->number); }
if (context->number == 13)
{
printf("General protection fault at %x%x, %d, %d, %x%x, %d, %x%x\n", context->rip >> 32,
context->rip & 0xFFFFFFFF, context->cs, context->ss, context->rsp >> 32, context->rsp & 0xFFFFFFFF,
context->error_code, context->cr2 >> 32, context->cr2 & 0xFFFFFFFF);
printf("General protection fault at %zx, %d, %d, %zx, %d, %zx\n", context->rip, context->cs, context->ss,
context->rsp, context->error_code, context->cr2);
while (1) halt();
}
if (context->number >= 0x20 && context->number < 0x30) { IRQ::interrupt_handler(context); }

View File

@ -21,7 +21,6 @@
#include "std/stdio.h"
#include "std/stdlib.h"
#include "std/string.h"
#include "utils.h"
extern BOOTBOOT bootboot;
@ -66,6 +65,18 @@ extern "C" void _start()
kinfoln("Prepared PIT");
uint64_t mmap_entries = (bootboot.size - 128) / 16;
MMapEnt* entry = (&bootboot.mmap);
for (uint64_t i = 0; i < mmap_entries; i++)
{
if (MMapEnt_IsFree(entry))
{
printf("Free region at 0x%zx, of size 0x%zx\n", MMapEnt_Ptr(entry), MMapEnt_Size(entry));
}
else { printf("Used region at 0x%zx, of size 0x%zx\n", MMapEnt_Ptr(entry), MMapEnt_Size(entry)); }
entry++;
}
Debug::DebugStatus::the()->StartBootStage(Color::Yellow);
Interrupts::enable();
Debug::DebugStatus::the()->PassBootStage(Color::Yellow);

View File

@ -40,10 +40,15 @@ static int internal_printf(const char* format, PutString put_string_callback, ss
}
};
bool is_long = false;
bool is_unsigned_long = false;
bool preserve_format = false;
while (format_index < format_size)
{
char current_char = format[format_index];
if (current_char == '%')
if (current_char == '%' || preserve_format)
{
if (format_index + 1 == format_size) // end of format string
{
@ -51,7 +56,9 @@ static int internal_printf(const char* format, PutString put_string_callback, ss
}
else
{
format_index++;
if (!preserve_format) format_index++;
else
preserve_format = false;
current_char = format[format_index];
switch (current_char)
{
@ -65,22 +72,90 @@ static int internal_printf(const char* format, PutString put_string_callback, ss
if (buffer_insert_index == 1024) flush_buffer();
break;
}
case 'z': {
is_unsigned_long = true;
preserve_format = true;
break;
}
case 'l': {
is_long = true;
preserve_format = true;
break;
}
case 'd': {
char result[41];
itoa(va_arg(ap, int), result, 10);
if (is_unsigned_long)
{
char result[25];
ultoa(va_arg(ap, uint64_t), result, 10);
if (buffer_insert_index + strlen(result) > 1024) flush_buffer();
memcpy(buffer + buffer_insert_index, result, strlen(result));
buffer_insert_index += strlen(result);
if (buffer_insert_index == 1024) flush_buffer();
is_unsigned_long = is_long = false;
}
else if (is_long)
{
char result[25];
ltoa(va_arg(ap, int64_t), result, 10);
if (buffer_insert_index + strlen(result) > 1024) flush_buffer();
memcpy(buffer + buffer_insert_index, result, strlen(result));
buffer_insert_index += strlen(result);
if (buffer_insert_index == 1024) flush_buffer();
is_unsigned_long = is_long = false;
}
else
{
char result[25];
itoa(va_arg(ap, int32_t), result, 10);
if (buffer_insert_index + strlen(result) > 1024) flush_buffer();
memcpy(buffer + buffer_insert_index, result, strlen(result));
buffer_insert_index += strlen(result);
if (buffer_insert_index == 1024) flush_buffer();
}
break;
}
case 'u': {
if (is_unsigned_long || is_long)
{
char result[25];
ultoa(va_arg(ap, uint64_t), result, 10);
if (buffer_insert_index + strlen(result) > 1024) flush_buffer();
memcpy(buffer + buffer_insert_index, result, strlen(result));
buffer_insert_index += strlen(result);
if (buffer_insert_index == 1024) flush_buffer();
is_unsigned_long = is_long = false;
}
else
{
char result[25];
utoa(va_arg(ap, uint32_t), result, 10);
if (buffer_insert_index + strlen(result) > 1024) flush_buffer();
memcpy(buffer + buffer_insert_index, result, strlen(result));
buffer_insert_index += strlen(result);
if (buffer_insert_index == 1024) flush_buffer();
}
break;
}
case 'x': {
char result[50];
utoa(va_arg(ap, unsigned int), result, 16);
if (is_unsigned_long || is_long)
{
char result[25];
ultoa(va_arg(ap, uint64_t), result, 16);
if (buffer_insert_index + strlen(result) > 1024) flush_buffer();
memcpy(buffer + buffer_insert_index, result, strlen(result));
buffer_insert_index += strlen(result);
if (buffer_insert_index == 1024) flush_buffer();
is_unsigned_long = is_long = false;
}
else
{
char result[25];
utoa(va_arg(ap, uint32_t), result, 16);
if (buffer_insert_index + strlen(result) > 1024) flush_buffer();
memcpy(buffer + buffer_insert_index, result, strlen(result));
buffer_insert_index += strlen(result);
if (buffer_insert_index == 1024) flush_buffer();
}
break;
}
case 's': {

View File

@ -17,7 +17,7 @@ static void strrev(char* arr, int start, int end)
strrev(arr, start, end);
}
char* itoa(int number, char* arr, int base)
char* itoa(int32_t number, char* arr, int base)
{
int i = 0, r, negative = 0;
@ -55,7 +55,71 @@ char* itoa(int number, char* arr, int base)
return arr;
}
char* utoa(unsigned int number, char* arr, int base)
char* ltoa(int64_t number, char* arr, int base)
{
int i = 0, r, negative = 0;
if (number == 0)
{
arr[i] = '0';
arr[i + 1] = '\0';
return arr;
}
if (number < 0 && base == 10)
{
number *= -1;
negative = 1;
}
while (number != 0)
{
r = number % base;
arr[i] = (r > 9) ? (r - 10) + 'a' : r + '0';
i++;
number /= base;
}
if (negative)
{
arr[i] = '-';
i++;
}
strrev(arr, 0, i - 1);
arr[i] = '\0';
return arr;
}
char* utoa(uint32_t number, char* arr, int base)
{
int i = 0, r;
if (number == 0)
{
arr[i] = '0';
arr[i + 1] = '\0';
return arr;
}
while (number != 0)
{
r = number % base;
arr[i] = (r > 9) ? (r - 10) + 'a' : r + '0';
i++;
number /= base;
}
strrev(arr, 0, i - 1);
arr[i] = '\0';
return arr;
}
char* ultoa(uint64_t number, char* arr, int base)
{
int i = 0, r;