From f5e114313822914642a436ff22a7e2135ea06b1f Mon Sep 17 00:00:00 2001 From: Kwarde Date: Wed, 25 Jun 2025 13:29:26 +0200 Subject: [PATCH] Fix issue in printf() with %d --needed to push+pop several registers before/after itoa() call --- console.asm | 14 ++++++++++---- core.asm | 5 +++++ tests.asm | 1 + 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/console.asm b/console.asm index 9794263..f45605b 100644 --- a/console.asm +++ b/console.asm @@ -86,8 +86,7 @@ printf: je .rep_d_r9 ;get from stack - ; rsp + 5*8 : RIP to printf call (+1), PUSH rbp,r12,r13,rdi (+4) - mov rsi, qword [rsp + 5*8 + (r10-5)*8] + mov rsi, qword [rbp + 16 + (r10-5)*8] jmp .convertInt .rep_d_rsi: @@ -107,7 +106,15 @@ printf: .convertInt: lea rsi, [rel printfNBuff] + push rcx + push rdx + push r8 + push r10 call itoa + pop r10 + pop r8 + pop rdx + pop rcx mov rsi, rax jmp .sinsertLoop @@ -125,8 +132,7 @@ printf: je .rep_s_r9 ;get from stack - ; rsp + 5*8 : RIP to printf call (+1), PUSH rbp,r12,r13,rdi (+4) - mov rsi, qword [rsp + 5*8 + (r10-5)*8] + mov rsi, qword [rbp + 16 + (r10-5)*8] jmp .sinsertLoop .rep_s_rdx: diff --git a/core.asm b/core.asm index 13a2a66..b3eb267 100644 --- a/core.asm +++ b/core.asm @@ -160,12 +160,17 @@ itoa: inc rcx test rax, rax jnz .convert + test rbx, rbx + jz .toString + push byte '-' + inc rcx .toString: pop rdx mov byte [rsi], dl inc rsi loop .toString + mov byte [rsi], 0 .quit: pop rsi diff --git a/tests.asm b/tests.asm index 51898f7..28b4edc 100644 --- a/tests.asm +++ b/tests.asm @@ -49,6 +49,7 @@ section .rodata TEST_toupper equ 1 TEST_strcmp equ 1 TEST_file1 equ 1 + ;TEST_itoa equ 0 ; not included in these tests; printf() calls itoa for %d specifiers num1 dq 69 num2 dq 0xFFFFFF