Minor optimizations in printf() (stop using rbx and r13, use smarter checks instead + pop r11 correctly (prevent stack corruption))

This commit is contained in:
2025-06-25 20:40:35 +02:00
parent d3b801a97b
commit cef713db14

View File

@ -55,12 +55,9 @@ printf:
push rbp
mov rbp, rsp
push rbx ; used to check if error must be sent (reached buff len)
push r12 ; used for writing byted to printBuff
push r13 ; (also) used for writing bytes to printBuff (why again? gotta check that out - probably not needed, just lost count of register usage at some point)
push r14 ; count length of printBuff
xor rbx, rbx
xor r10, r10
xor r14, r14
lea r11, [rel printfBuff]
@ -185,7 +182,6 @@ printf:
mov [printfNBuff], dil
inc r14
cmp r14, bufferLength-1
; Print error to stdout
je .finish_L
mov dil, 0
mov [printfNBuff+1], dil
@ -230,8 +226,8 @@ printf:
je .finish_L
cmp byte [rsi], 0x0
je .s0f
mov r13b, byte [rsi]
mov byte [r11], r13b
mov r12b, byte [rsi]
mov byte [r11], r12b
inc r14
inc rsi
inc r11
@ -255,8 +251,8 @@ printf:
call print
lea rdi, [rel printfBuff]
call strlen
test rbx, rbx
jz .final
cmp r14, bufferLength-1
jl .final
push r11
mov rax, NR_write
@ -264,13 +260,11 @@ printf:
lea rsi, [rel ERR_buffLen]
mov rdx, lERR_buffLen
syscall
pop r11
.final:
pop r11
pop r14
pop r13
pop r12
pop rbx
leave
ret