From 3d47667c2f1f8964e9b17240dea7c91702b5adf7 Mon Sep 17 00:00:00 2001 From: pyao12 Date: Fri, 5 Jun 2026 20:18:53 +0800 Subject: [PATCH] =?UTF-8?q?[feat]=20=E4=B8=B2=E5=8F=A3hex=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E5=8F=AF=E9=80=89=E6=8B=A9=E6=98=AF=E5=90=A6=E8=A1=A5?= =?UTF-8?q?0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 2 +- include/serial.h | 2 +- kernel/serial.cpp | 30 +++++++++++++++++++++++------- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 0edf561..ac053ea 100644 --- a/Makefile +++ b/Makefile @@ -135,7 +135,7 @@ disk: vdir run: disk @echo "Launching QEMU" - @qemu-system-x86_64 -bios /usr/share/ovmf/OVMF.fd -net none -drive file=build/disk.img,index=0,format=raw -serial file:serial.log -s -S + @qemu-system-x86_64 -bios /usr/share/ovmf/OVMF.fd -net none -drive file=build/disk.img,index=0,format=raw -serial stdio -serial file:serial.log -s -S clean: @echo "Cleaning old files" diff --git a/include/serial.h b/include/serial.h index 72f296c..0af3796 100644 --- a/include/serial.h +++ b/include/serial.h @@ -13,5 +13,5 @@ extern serial_context g_serial; void serial_init(EFI_SERIAL_IO_PROTOCOL *SerialIo); // 初始化串行驱动 void serial_write(String str); // 往串行写string void serial_write_char(char c); // 往串行写char(不推荐使用) -void serial_write_hex(UINTN val); // 往串行写十六进制数字 +void serial_write_hex(UINTN val, bool fill = true); // 往串行写十六进制数字(fill=false 不补前导0) char serial_read_char(); // 读串行 \ No newline at end of file diff --git a/kernel/serial.cpp b/kernel/serial.cpp index 15e494e..5ae224e 100644 --- a/kernel/serial.cpp +++ b/kernel/serial.cpp @@ -32,15 +32,31 @@ void serial_write(String str) { } } -void serial_write_hex(UINTN val) { +void serial_write_hex(UINTN val, bool fill) { char buf[19]; - buf[0] = '0'; buf[1] = 'x'; - for (SSINT32 i = 17; i >= 2; i--) { - UINTN digit = val & 0xF; - buf[i] = digit < 10 ? '0' + digit : 'A' + digit - 10; - val >>= 4; + buf[0] = '0'; + buf[1] = 'x'; + SSINT32 pos = 2; + if (val == 0) { + buf[pos++] = '0'; + } else { + char tmp[17]; + SSINT32 len = 0; + while (val) { + UINTN digit = val & 0xF; + tmp[len++] = digit < 10 ? '0' + digit : 'A' + digit - 10; + val >>= 4; + } + if (fill) { + while (len < 16) { + buf[pos++] = '0'; + } + } + for (SSINT32 i = len - 1; i >= 0; i--) { + buf[pos++] = tmp[i]; + } } - buf[18] = '\0'; + buf[pos] = '\0'; serial_write(buf); }