Fix printf args always being first arg when using padding, improved/added tests. NEW FOUND BUG: full qword byte misses 3 characters when using padding

This commit is contained in:
2025-07-13 15:03:59 +02:00
parent 1971c5b897
commit 483d9185de
2 changed files with 70 additions and 44 deletions

View File

@@ -79,7 +79,7 @@ puts:
; rax* (ret) amount of printed characters
; rdi* (arg) pointer to format[] to format and print >> pointer to buffer
; rsi* (optional arg) >> Used for inserting strings to buffer
; rdx* (optional arg) >> Keeps track of amount of processed format specifiers
; rdx* (optional arg) >> misc
; rcx* (optional arg) >> Stores arg for %x/%X
; r8* (optional arg) >> Used for moving characters
; r9* (optional arg) >> Keeps track of where to jump after flushing buffer
@@ -87,21 +87,20 @@ puts:
; r11* Keeps track of total characters (return value)
; r12 Padding for numbers (0=nopadding, >0=padding w/ spaces, NOT >0=padding w/ zeroes)
; r13 Padding: zeroes (=1) or spaces (=0)
; r14 Keeps track of amount of processed format specifiers
printf:
%macro load_arg 1
cmp rdx, 4
cmp r14, 4
ja %%fromStack
mov %1, [printfArgs + SIZE_QWORD * rdx]
mov %1, [printfArgs + SIZE_QWORD * r14]
jmp %%continue
%%fromStack:
mov %1, [rbp + RBP_OFFSET_CALLER + ((rdx-5) * SIZE_QWORD)]
mov %1, [rbp + RBP_OFFSET_CALLER + ((r14-5) * SIZE_QWORD)]
%%continue:
%endmacro
%macro push_regs 0
sub rsp, SIZE_QWORD
push rax
push rdx
push rdi
push r9
push r10
@@ -111,9 +110,7 @@ printf:
pop r10
pop r9
pop rdi
pop rdx
pop rax
add rsp, SIZE_QWORD
%endmacro
; entry:
@@ -121,6 +118,8 @@ printf:
mov rbp, rsp
push r12
push r13
push r14
sub rsp, SIZE_QWORD
cmp byte [rdi], EOS
je .emptyStr
@@ -135,6 +134,7 @@ printf:
xor rdx, rdx
xor r10, r10
xor r11, r11
xor r14, r14
.process:
cmp byte [rdi], EOS
@@ -226,7 +226,7 @@ printf:
;--- '%%' ---;
.rep_pct:
mov sil, '%'
dec rdx
dec r14
jmp .insertChar
;--- '%c' ---;
@@ -280,7 +280,6 @@ printf:
;--- '%b' ---;
.rep_b:
cmp rdx, 4
load_arg rsi
push_regs
mov rdi, rsi
@@ -310,8 +309,10 @@ printf:
inc r11
jmp .insertString
.endInsertString:
inc rdx
inc r14
add rdi, 2
xor r12, r12
xor r13, r13
jmp .process
;--- Insert char to buffer ---;
@@ -320,7 +321,9 @@ printf:
add rdi, 2
add r10, 1
add r11, 1
inc rdx
xor r12, r12
xor r13, r13
inc r14
jmp .process
.flushBuffer:
@@ -358,6 +361,8 @@ printf:
xor rax, rax
.quit:
add rsp, SIZE_QWORD
pop r14
pop r13
pop r12
leave

View File

@@ -29,23 +29,23 @@ extern hex2str
extern bin2str
section .rodata
TEST_print equ 1
TEST_puts equ 1
TEST_dec2str equ 1 ;includes udec2str
TEST_hex2str equ 1
TEST_bin2str equ 1
TEST_print equ 0
TEST_puts equ 0
TEST_dec2str equ 0 ;includes udec2str
TEST_hex2str equ 0
TEST_bin2str equ 0
TEST_printf equ 1
TEST_strlen equ 1
TEST_strcpy equ 1
TEST_strcat equ 1
TEST_strclr equ 1
TEST_islower equ 1
TEST_isupper equ 1
TEST_tolower equ 1
TEST_toupper equ 1
TEST_strcmp equ 1
TEST_min equ 1 ;includes minu
TEST_max equ 1 ;includes maxu
TEST_strlen equ 0
TEST_strcpy equ 0
TEST_strcat equ 0
TEST_strclr equ 0
TEST_islower equ 0
TEST_isupper equ 0
TEST_tolower equ 0
TEST_toupper equ 0
TEST_strcmp equ 0
TEST_min equ 0 ;includes minu
TEST_max equ 0 ;includes maxu
str1 db "Hello, world!",EOS
str2 db "Hello, World!",EOS
@@ -90,18 +90,20 @@ section .rodata
printf4Str db "%i|%d|%u , %i|%d|%u",NL,EOS
printf5 db TAB,"printf(",DQUO,"%x|%X , %x|%X\n",DQUO,", 0xabcdeffedcba, 0x069bc0e, 666, -1): ",NL,TAB,TAB,EOS
printf5Str db "%x|%X , %x|%X",NL,EOS
printf6 db TAB,"printf(",DQUO,"%b | %8b | %08b\n",DQUO,", 5, 5, 5): ",NL,TAB,TAB,EOS
printf6 db TAB,"printf(",DQUO,"%b | %8b | %08b\n",DQUO,", 5, 7, 9): ",NL,TAB,TAB,EOS
printf6Str db "%b | %8b | %08b",NL,EOS
printf7 db TAB,"printf(",DQUO,"%d | %8d | %08d\n",DQUO,", -234, -234, -234): ",NL,TAB,TAB,EOS
printf7 db TAB,"printf(",DQUO,"%d | %8d | %08d\n",DQUO,", -234, -666, 234): ",NL,TAB,TAB,EOS
printf7Str db "%d | %8d | %08d",NL,EOS
printf8 db TAB,"printf(",DQUO,"%i | %8i | %08i\n",DQUO,", -234, -234, -234): ",NL,TAB,TAB,EOS
printf8 db TAB,"printf(",DQUO,"%i | %8i | %08i\n",DQUO,", -234, -666, 234): ",NL,TAB,TAB,EOS
printf8Str db "%i | %8i | %08i",NL,EOS
printf9 db TAB,"printf(",DQUO,"%u | %8u | %08u\n",DQUO,", -234, -234, -234): ",NL,TAB,TAB,EOS
printf9 db TAB,"printf(",DQUO,"%u | %8u | %08u\n",DQUO,", -234, -666, 234): ",NL,TAB,TAB,EOS
printf9Str db "%u | %8u | %08u",NL,EOS
printf10 db TAB,"printf(",DQUO,"%x | %8x | %08x\n",DQUO,", 0xAB0F, 0xAB0F, 0xAB0F): ",NL,TAB,TAB,EOS
printf10 db TAB,"printf(",DQUO,"%x | %8x | %08x\n",DQUO,", 0xAB0F, 0xBA0F, 0xAB0F): ",NL,TAB,TAB,EOS
printf10Str db "%x | %8x | %08x",NL,EOS
printf11 db TAB,"printf(",DQUO,"%X | %8X | %08X\n",DQUO,", 0xAB0F, 0xAB0F, 0xAB0F): ",NL,TAB,TAB,EOS
printf11 db TAB,"printf(",DQUO,"%X | %8X | %08X\n",DQUO,", 0xAB0F, 0xBA0F, 0xAB0F): ",NL,TAB,TAB,EOS
printf11Str db "%X | %8X | %08X",NL,EOS
printf12 db TAB,"rax=0x1234567890ABCDEF",NL,TAB,"printf(",DQUO,"\nRAX = %064b\nEAX = %064b\n AX = %064b\n AH = %56b00000000\n AL = %064b\n",DQUO,", rax, eax, ax, ah, al): ",NL,TAB,TAB,EOS
printf12Str db NL,"RAX = %064b",NL,"EAX = %064b",NL," AX = %064b",NL," AH = %064b",NL," AL = %064b",NL,EOS
; strlen()
msgStrlen db NL,"TEST strlen()",NL,EOS
@@ -357,8 +359,8 @@ _start:
call print
lea rdi, [rel printf6Str]
mov rsi, 5
mov rdx, 5
mov rcx, 5
mov rdx, 7
mov rcx, 9
call printf
; TEST 7
@@ -366,8 +368,8 @@ _start:
call print
lea rdi, [rel printf7Str]
mov rsi, -234
mov rdx, -234
mov rcx, -234
mov rdx, -666
mov rcx, 234
call printf
; TEST 8
@@ -375,8 +377,8 @@ _start:
call print
lea rdi, [rel printf8Str]
mov rsi, -234
mov rdx, -234
mov rcx, -234
mov rdx, -666
mov rcx, 234
call printf
; TEST 9
@@ -384,8 +386,8 @@ _start:
call print
lea rdi, [rel printf9Str]
mov rsi, -234
mov rdx, -234
mov rcx, -234
mov rdx, -666
mov rcx, 234
call printf
; TEST 10
@@ -393,7 +395,7 @@ _start:
call print
lea rdi, [rel printf10Str]
mov rsi, 0xAB0F
mov rdx, 0xAB0F
mov rdx, 0xBA0F
mov rcx, 0xAB0F
call printf
@@ -402,9 +404,28 @@ _start:
call print
lea rdi, [rel printf11Str]
mov rsi, 0xAB0F
mov rdx, 0xAB0F
mov rdx, 0xBA0F
mov rcx, 0xAB0F
call printf
; TEST 12
lea rdi, [rel printf12]
call print
mov rax, 0x1234567890abcdef
xor rbx, rbx
xor rcx, rcx
xor r8, r8
xor r9, r9
lea rdi, [rel printf12Str]
mov rsi, rax
mov edx, eax
mov cx, ax
mov bh, ah
mov r8, rbx
mov r9b, al
call printf
%endif
;---