Adds strcpy()
This commit is contained in:
@@ -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
|
||||
|
Reference in New Issue
Block a user