Optimize strcpy(): 1- Conditional moves (I keep forgetting about them) 2- Better setting of RCX (better accounting for EOS)

This commit is contained in:
2025-07-06 11:59:49 +02:00
parent 2fb4312615
commit 1a6ea4ffe6

View File

@@ -27,26 +27,23 @@ strlen:
; Return value: Pointer to dest ; Return value: Pointer to dest
; Used registers: ; Used registers:
; rax* (ret) Pointer to dest[] ; rax* (ret) Pointer to dest[]
; rdi (arg) Pointer to dest[] ; rdi* (arg) Pointer to dest[]
; rsi (arg) Pointer to src[] ; rsi* (arg) Pointer to src[]
; rdx (arg) Max amount of characters to copy / size of dest[] <!>Must never be greater than size of dest[]! ; rdx (arg) Size of dest[]
; rcx* Counter for movsb ; rcx* Counter for movsb
; r8* Backup for rdi ; r8* Backup for rdi
strcpy: strcpy:
mov r8, rdi mov r8, rdi
mov rdi, rsi mov rdi, rsi
call strlen call strlen
mov rcx, rdx inc rax
cmp rcx, rax cmp rax, rdx
je .decMaxLen ;rcx-1 for EOS cmova rcx, rdx
ja .setMaxLen ;set rcx to strlen(src) cmovbe rcx, rax
jmp .copy cmp rcx, rdx
.decMaxLen: jb .copy
dec rcx dec rcx
jmp .copy .copy
.setMaxLen:
mov rcx, rax
.copy:
mov rdi, r8 mov rdi, r8
rep movsb rep movsb
mov byte [rdi], EOS mov byte [rdi], EOS