diff --git a/libc/inc/debug.h b/libc/inc/debug.h index 7e515fe..f660f62 100644 --- a/libc/inc/debug.h +++ b/libc/inc/debug.h @@ -38,7 +38,7 @@ const char *to_human_size(unsigned long bytes); /// @brief Transforms the given value to a binary string. /// @param value The decimal value. /// @return String representing the binary value. -const char *dec_to_binary(unsigned long value); +const char *dec_to_binary(unsigned long value, unsigned length); /// Prints a debugging message. #define pr_debug(...) dbg_printf(__FILENAME__, __func__, __LINE__, __DEBUG_HEADER__ __VA_ARGS__) diff --git a/libc/src/debug.c b/libc/src/debug.c index 84ab2f5..d421768 100644 --- a/libc/src/debug.c +++ b/libc/src/debug.c @@ -11,6 +11,7 @@ #include #include #include +#include /// Serial port for QEMU. #define SERIAL_COM1 (0x03F8) @@ -96,10 +97,10 @@ const char *to_human_size(unsigned long bytes) return output; } -const char *dec_to_binary(unsigned long value) +const char *dec_to_binary(unsigned long value, unsigned length) { static char buffer[33]; - for (int i = 0; i < 32; ++i) - buffer[i] = bit_check(value, 31 - i) ? '1' : '0'; + for (int i = 0, j = 32 - min(max(0, length), 32); j < 32; ++i, ++j) + buffer[i] = bit_check(value, 31 - j) ? '1' : '0'; return buffer; } \ No newline at end of file diff --git a/mentos/inc/io/debug.h b/mentos/inc/io/debug.h index 6383626..7733c0e 100644 --- a/mentos/inc/io/debug.h +++ b/mentos/inc/io/debug.h @@ -60,7 +60,7 @@ const char *to_human_size(unsigned long bytes); /// @brief Transforms the given value to a binary string. /// @param value The decimal value. /// @return String representing the binary value. -const char *dec_to_binary(unsigned long value); +const char *dec_to_binary(unsigned long value, unsigned length); /// Prints a KERN_DEFAULT. #define pr_default(...) dbg_printf(__FILENAME__, __func__, __LINE__, __DEBUG_HEADER__, KERN_DEFAULT __VA_ARGS__) diff --git a/mentos/src/io/debug.c b/mentos/src/io/debug.c index 901fc28..358645e 100644 --- a/mentos/src/io/debug.c +++ b/mentos/src/io/debug.c @@ -12,6 +12,7 @@ #include "string.h" #include "stdio.h" #include "io/video.h" +#include "math.h" /// Serial port for QEMU. #define SERIAL_COM1 (0x03F8) @@ -185,10 +186,10 @@ const char *to_human_size(unsigned long bytes) return output; } -const char *dec_to_binary(unsigned long value) +const char *dec_to_binary(unsigned long value, unsigned length) { static char buffer[33]; - for (int i = 0; i < 32; ++i) - buffer[i] = bit_check(value, 31 - i) ? '1' : '0'; + for (int i = 0, j = 32 - min(max(0, length), 32); j < 32; ++i, ++j) + buffer[i] = bit_check(value, 31 - j) ? '1' : '0'; return buffer; }