From 1a6ea4ffe64b0e3d93d1f5451637ab5cfd0ad555 Mon Sep 17 00:00:00 2001 From: Kwarde Date: Sun, 6 Jul 2025 11:59:49 +0200 Subject: [PATCH] Optimize strcpy(): 1- Conditional moves (I keep forgetting about them) 2- Better setting of RCX (better accounting for EOS) --- src/string.asm | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/string.asm b/src/string.asm index 011f110..0987865 100644 --- a/src/string.asm +++ b/src/string.asm @@ -27,26 +27,23 @@ strlen: ; Return value: Pointer to dest ; Used registers: ; rax* (ret) Pointer to dest[] -; rdi (arg) Pointer to dest[] -; rsi (arg) Pointer to src[] -; rdx (arg) Max amount of characters to copy / size of dest[] Must never be greater than size of dest[]! +; rdi* (arg) Pointer to dest[] +; rsi* (arg) Pointer to src[] +; rdx (arg) Size of dest[] ; rcx* Counter for movsb ; r8* Backup for rdi strcpy: mov r8, rdi mov rdi, rsi call strlen - mov rcx, rdx - cmp rcx, rax - je .decMaxLen ;rcx-1 for EOS - ja .setMaxLen ;set rcx to strlen(src) - jmp .copy - .decMaxLen: + inc rax + cmp rax, rdx + cmova rcx, rdx + cmovbe rcx, rax + cmp rcx, rdx + jb .copy dec rcx - jmp .copy - .setMaxLen: - mov rcx, rax - .copy: + .copy mov rdi, r8 rep movsb mov byte [rdi], EOS