Adds strcpy()

This commit is contained in:
2025-07-06 11:15:58 +02:00
parent a3a128354d
commit 2fb4312615
2 changed files with 95 additions and 0 deletions

View File

@@ -2,6 +2,7 @@
section .text
global strlen
global strcpy
;----- strlen(*str[]) -----;
; Gets the length of given string
@@ -19,3 +20,35 @@ strlen:
not rax
dec rax
ret
;----- strcpy(*dest[], *src[], maxLength) -----;
; Copies one string (src) into the other (dest), overwriting dest
; NOTE: maxLength must never exceed size of dest[].
; 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[]!
; 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:
dec rcx
jmp .copy
.setMaxLen:
mov rcx, rax
.copy:
mov rdi, r8
rep movsb
mov byte [rdi], EOS
mov rax, r8
ret