Adds strlcpy()

This commit is contained in:
2025-06-24 15:53:24 +02:00
parent d3e7f47259
commit e620754b13
2 changed files with 70 additions and 1 deletions

View File

@ -1,6 +1,7 @@
section .text
global strlen
global strcpy
global strlcpy
global strcat
global strcmp
@ -48,6 +49,36 @@ strcpy:
.quit:
leave
ret
;----- strlcpy(char* dest, char* src, int size) -----;
; return value: Length of src
strlcpy:
push rbp
mov rbp, rsp
mov r9, rsi
test rdx, rdx
jz .quit
js .quit
dec rdx
.loop:
cmp byte [rsi], 0x0
je .0f
mov r10b, byte [rsi]
mov byte [rdi], r10b
inc rsi
inc rdi
dec rdx
cmp rdx, 0x0
ja .loop
.0f:
mov byte [rdi], 0x0
.quit:
mov rdi, r9
call strlen
leave
ret
;----- strcat(char* dest, char* src) -----;
; return value: pointer to dest
strcat:

View File

@ -15,6 +15,7 @@ extern puts
;string.asm
extern strlen
extern strcpy
extern strlcpy
extern strcat
extern strcmp
@ -29,6 +30,7 @@ section .rodata
TEST_islower equ 1
TEST_isupper equ 1
TEST_strcpy equ 1
TEST_strlcpy equ 1
TEST_strcat equ 1
TEST_tolower equ 1
TEST_toupper equ 1
@ -57,6 +59,9 @@ section .rodata
msgIsupper db "isupper(str1[0]): %d",10,0
msgIsupper2 db "isupper(str1[1]): %d",10,0
msgStrcpy db "strcpy(strBuff, str1): %s",10,0
msgStrlcpy db "strlcpy(strBuff2, str1, -1): (%d): %s",10,0
msgStrlcpy2 db "strlcpy(strBuff2, str1, 0): (%d): %s",10,0
msgStrlcpy3 db "strlcpy(strBuff2, str1, 8): (%d): %s",10,0
msgStrcat db "strcat(strBuff, str3): %s",10,0
msgStrlen3 db "strlen(strBuff): %d",10,0
msgTolower db "tolower(str1[0]): %c",10,0
@ -71,6 +76,7 @@ section .rodata
section .data
section .bss
strBuff resb 32
strBuff2 resb 5
section .text
global main
main:
@ -215,6 +221,38 @@ main:
lea rsi, [rel strBuff]
call printf
%ENDIF
%IF TEST_strlcpy
; TEST: strlcpy(strBuff2, str1, -1)
lea rdi, [rel strBuff2]
lea rsi, [rel str1]
mov rdx, -1
call strlcpy
mov rsi, rax
xor rax, rax
lea rdi, [rel msgStrlcpy]
lea rdx, [rel strBuff2]
call printf
; TEST: strlcpy(strBuff2, str1, 0)
lea rdi, [rel strBuff2]
lea rsi, [rel str1]
mov rdx, 0
call strlcpy
mov rsi, rax
xor rax, rax
lea rdi, [rel msgStrlcpy2]
lea rdx, [rel strBuff2]
call printf
; TEST: strlcpy(strBuff2, str1, 8)
lea rdi, [rel strBuff2]
lea rsi, [rel str1]
mov rdx, 5
call strlcpy
mov rsi, rax
xor rax, rax
lea rdi, [rel msgStrlcpy3]
lea rdx, [rel strBuff2]
call printf
%ENDIF
%IF TEST_strcat
; TEST: strcat()
lea rdi, [rel strBuff]