utoa/printf: Support octal (%o)

This commit is contained in:
2025-07-15 08:18:57 +02:00
parent 9f33e36883
commit dc49e29019
3 changed files with 31 additions and 2 deletions

View File

@@ -246,6 +246,8 @@ printf:
je .rep_x
cmp byte [rdi + 1], 'b'
je .rep_b
cmp byte [rdi + 1], 'o'
je .rep_o
cmp byte [rdi + 1], 's'
je .rep_s
@@ -323,6 +325,20 @@ printf:
pop_regs
jmp .insertString
;--- '%o' ---;
.rep_o:
load_arg rsi
push_regs
mov rdi, rsi
mov rsi, 8
mov rdx, r12
mov rcx, r13
mov r8, 0
call utoa
mov rsi, rax
pop_regs
jmp .insertString
;--- '%s' ---;
.rep_s:
load_arg rsi

View File

@@ -123,7 +123,7 @@ itoa:
;----- utoa(int, base, padLen, bool padZeroes, bool upperCase) -----;
; Converts an unsigned integer to a string
; Return value: Pointer to converted string or 0(EOS) if entered base is invalid
; Supported bases: 2 (binary), 10 (decimal), 16 (hexadecimal)
; Supported bases: 2 (binary), 8, (octal), 10 (decimal), 16 (hexadecimal)
; Used registers:
; rax* num to divide (DIV) >> Copy bytes when reversing string >> (ret) Pointer to converted string
; rbx Stores (upperCase)
@@ -143,6 +143,8 @@ utoa:
; Before doing anything, check if base is valid
cmp rsi, 2
je .ok
cmp rsi, 8
je .ok
cmp rsi, 10
je .ok
cmp rsi, 16
@@ -161,7 +163,7 @@ utoa:
mov r13, rsi
mov r14, rdx
mov r15, rcx
; First, assure 0 <= padLen <= 64 ;(64, even though max signed int64 = 19 characters; enables to pad with base 2)
; First, assure 0 <= padLen <= 64
mov rdi, rdx
xor rsi, rsi
mov rdx, 64

View File

@@ -99,6 +99,8 @@ section .rodata
printf11Str db "%X | %8X | %08X",NL,EOS
printf12 db TAB,"rax=0x1234567890ABCDEF",NL,TAB,"printf(",DQUO,"\nRAX\t%064b\nEAX\t\t\t\t\t%032b\n AX\t\t\t\t\t\t\t%016b\n AH\t\t\t\t\t\t\t%08b\t\n AL\t\t\t\t\t\t\t\t%08b\n",DQUO,", rax, eax, ax, ah, al): ",NL,TAB,TAB,EOS
printf12Str db "\nRAX\t%064b\nEAX\t\t\t\t\t%032b\n AX\t\t\t\t\t\t\t%016b\n AH\t\t\t\t\t\t\t%08b\t\n AL\t\t\t\t\t\t\t\t%08b\n",EOS
printf13 db TAB,"printf(",DQUO,"%o | %8o | %08o\n",DQUO,", 1500, 1500, 1500): ",NL,TAB,TAB,EOS
printf13Str db "%o | %8o | %08o\n",EOS
; strlen()
msgStrlen db NL,"TEST strlen()",NL,EOS
@@ -374,6 +376,15 @@ _start:
shr r8, 8
mov r9b, al
call printf
; TEST 13
lea rdi, [rel printf13]
call print
lea rdi, [rel printf13Str]
mov rsi, 1500
mov rdx, 1500
mov rcx, 1500
call printf
%endif
;---