From a47ab3cdc6e215d19e8209658dabf0645d85754b Mon Sep 17 00:00:00 2001 From: Kwarde Date: Sun, 6 Jul 2025 19:13:05 +0200 Subject: [PATCH] strcat: Better logic for determining max amount of characters to copy --- src/string.asm | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/string.asm b/src/string.asm index a91e1fc..d257102 100644 --- a/src/string.asm +++ b/src/string.asm @@ -60,30 +60,25 @@ strcpy: ; rsi* (arg) Pointer to src[] ; rdx (arg) Size of dest[] ; rcx* Counter for movsb -; r8* Backup for rdi +; r8* Backup for rdi (point to start of dest) ; r9* Backup for rsi -; r10* Current length of dest[] +; r10* Backup for rdi (point to end of dest) strcat: mov r8, rdi mov r9, rsi + call strlen + sub rdx, rax + dec rdi + mov r10, rdi mov rdi, rsi call strlen - mov rcx, rax - mov rdi, r8 - call strlen - mov r10, rax - mov rdi, r8 - add rcx, rax - cmp rcx, rdx - cmova rcx, rdx - cmp rcx, rdx + cmp rax, rdx jb .copy - dec rcx - .copy: - mov rdi, r8 + dec rdx + .copy: + mov rcx, rdx mov rsi, r9 - add rdi, r10 - sub rcx, r10 + mov rdi, r10 cld rep movsb mov byte [rdi], EOS