Fix issue in printf() with %d --needed to push+pop several registers before/after itoa() call

This commit is contained in:
2025-06-25 13:29:26 +02:00
parent 4438abda11
commit f5e1143138
3 changed files with 16 additions and 4 deletions

View File

@ -86,8 +86,7 @@ printf:
je .rep_d_r9 je .rep_d_r9
;get from stack ;get from stack
; rsp + 5*8 : RIP to printf call (+1), PUSH rbp,r12,r13,rdi (+4) mov rsi, qword [rbp + 16 + (r10-5)*8]
mov rsi, qword [rsp + 5*8 + (r10-5)*8]
jmp .convertInt jmp .convertInt
.rep_d_rsi: .rep_d_rsi:
@ -107,7 +106,15 @@ printf:
.convertInt: .convertInt:
lea rsi, [rel printfNBuff] lea rsi, [rel printfNBuff]
push rcx
push rdx
push r8
push r10
call itoa call itoa
pop r10
pop r8
pop rdx
pop rcx
mov rsi, rax mov rsi, rax
jmp .sinsertLoop jmp .sinsertLoop
@ -125,8 +132,7 @@ printf:
je .rep_s_r9 je .rep_s_r9
;get from stack ;get from stack
; rsp + 5*8 : RIP to printf call (+1), PUSH rbp,r12,r13,rdi (+4) mov rsi, qword [rbp + 16 + (r10-5)*8]
mov rsi, qword [rsp + 5*8 + (r10-5)*8]
jmp .sinsertLoop jmp .sinsertLoop
.rep_s_rdx: .rep_s_rdx:

View File

@ -160,12 +160,17 @@ itoa:
inc rcx inc rcx
test rax, rax test rax, rax
jnz .convert jnz .convert
test rbx, rbx
jz .toString
push byte '-'
inc rcx
.toString: .toString:
pop rdx pop rdx
mov byte [rsi], dl mov byte [rsi], dl
inc rsi inc rsi
loop .toString loop .toString
mov byte [rsi], 0
.quit: .quit:
pop rsi pop rsi

View File

@ -49,6 +49,7 @@ section .rodata
TEST_toupper equ 1 TEST_toupper equ 1
TEST_strcmp equ 1 TEST_strcmp equ 1
TEST_file1 equ 1 TEST_file1 equ 1
;TEST_itoa equ 0 ; not included in these tests; printf() calls itoa for %d specifiers
num1 dq 69 num1 dq 69
num2 dq 0xFFFFFF num2 dq 0xFFFFFF