From a5581d8b7d5bb9fb1ffb0f9284407d4bb6174d83 Mon Sep 17 00:00:00 2001 From: d0p1 Date: Thu, 19 Jan 2023 16:49:20 +0100 Subject: [PATCH] feat: log has now printf like formating --- kernel/drivers/serial.s | 2 +- lib/base/log.s | 99 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 88 insertions(+), 13 deletions(-) diff --git a/kernel/drivers/serial.s b/kernel/drivers/serial.s index 6d040b5..6d808c6 100644 --- a/kernel/drivers/serial.s +++ b/kernel/drivers/serial.s @@ -44,5 +44,5 @@ serial_write: mov ebp, esp mov ecx, [ebp + 8] COM_OUT COM1, THR, cl - pop ebp + leave ret diff --git a/lib/base/log.s b/lib/base/log.s index 8f7fa89..7ad8c19 100644 --- a/lib/base/log.s +++ b/lib/base/log.s @@ -9,9 +9,11 @@ section .text putstr: push ebp mov ebp, esp - mov ebx, [ebp + 8] + push edi + mov edi, [ebp + 8] + .loop: - mov eax, [ebx] + mov eax, [edi] cmp al, 0 je .end push eax @@ -20,30 +22,56 @@ putstr: %else %endif add esp, 4 - inc ebx + inc edi jmp .loop .end: - pop ebp + pop edi + leave ret -putuint: +puthex: + xchg bx, bx push ebp mov ebp, esp + push edi + push esi + mov edi, [ebp + 8] ; number + + mov eax, hexprefix + push hexprefix + call putstr + add esp, 4 - mov ebx, [ebp + 8] - mov edx, [ebp + 12] + mov edx, 0xF + mov ecx, 0x8 + mov esi, buffer +.loop: + rol edi, 4 + mov eax, edi + and eax, edx + mov al, [digits + eax] + mov [esi], al + inc esi + dec ecx + jnz .loop mov eax, buffer push eax call putstr + add esp, 4 - pop ebp + pop esi + pop edi + leave ret global log_impl log_impl: push ebp mov ebp, esp + push edi + push esi + push ebx mov eax, [ebp + 8] push eax @@ -51,19 +79,62 @@ log_impl: add esp, 4 %ifdef __KERNEL__ - push ':' + mov eax, ':' + push eax call serial_write add esp, 4 %else %endif + mov edi, [ebp + 12] + mov esi, 12 .loop: - mov eax, [ebp + 12] + mov eax, [edi] + cmp al, 0 + je .end + cmp al, '%' + jne .putchar + inc edi + mov eax, [edi] + cmp al, '%' + je .next + cmp al, 'x' + jne .check_s + add esi, 4 + mov ebx, ebp + add ebx, esi + mov eax, [ebx] + push eax + call puthex + add esp, 4 + jmp .next +.check_s: + cmp al, 's' + jne .unknown_format + add esi, 4 + mov ebx, ebp + add ebx, esi + mov eax, [ebx] push eax call putstr add esp, 4 + jmp .next +.unknown_format: + mov al, '?' +.putchar: + push eax +%ifdef __KERNEL__ + call serial_write +%else + ;; TODO +%endif + add esp, 4 +.next: + inc edi + jmp .loop .end: + ;; print new line %ifdef __KERNEL__ mov al, 0xA push eax @@ -71,8 +142,12 @@ log_impl: add esp, 4 %else %endif - pop ebp + pop ebx + pop esi + pop edi + leave ret digits db '0123456789ABCDEF' -buffer db '0000000000', 0 +hexprefix db '0x', 0 +buffer db '00000000', 0