diff --git a/src/string.asm b/src/string.asm index 0987865..b7ce26e 100644 --- a/src/string.asm +++ b/src/string.asm @@ -3,6 +3,7 @@ section .text global strlen global strcpy + global strcat ;----- strlen(*str[]) -----; ; Gets the length of given string @@ -24,7 +25,7 @@ strlen: ;----- 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 +; Return value: Pointer to dest[] ; Used registers: ; rax* (ret) Pointer to dest[] ; rdi* (arg) Pointer to dest[] @@ -43,9 +44,48 @@ strcpy: cmp rcx, rdx jb .copy dec rcx - .copy + .copy: mov rdi, r8 rep movsb mov byte [rdi], EOS mov rax, r8 ret + +;----- strcat(*dest[], *src[], maxLength) -----; +; Concatenate src[] into 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) Size of dest[] +; rcx* Counter for movsb +; r8* Backup for rdi +; r9* Backup for rsi +; r10* Current length of dest[] +strcat: + mov r8, rdi + mov r9, rsi + mov rdi, rsi + call strlen ;16 + mov rcx, rax + mov rdi, r8 + call strlen ;20 + mov r10, rax + mov rdi, r8 + add rcx, rax ;36 + cmp rcx, rdx ;36 <> 32 + cmova rcx, rdx + cmp rcx, rdx + jb .copy + dec rcx + .copy: + mov rdi, r8 + mov rsi, r9 + add rdi, r10 + sub rcx, r10 + cld + rep movsb + mov byte [rdi], EOS + mov rax, r8 + ret diff --git a/src/tests.asm b/src/tests.asm index bece942..0758fb3 100644 --- a/src/tests.asm +++ b/src/tests.asm @@ -8,6 +8,7 @@ extern puts ; string.asm extern strlen extern strcpy +extern strcat ; convert.asm section .rodata @@ -15,6 +16,7 @@ section .rodata TEST_puts equ 1 TEST_strlen equ 1 TEST_strcpy equ 1 + TEST_strcat equ 1 str1 db "Hello, world!",EOS str2 db "Hello, World!",EOS @@ -39,11 +41,16 @@ section .rodata strcpy3 db TAB,"strcpy(strBuff1, str1, 32): ",NL,TAB,TAB,EOS strcpy4 db TAB,"strcpy(strBuff1, str5, 32): ",NL,TAB,TAB,EOS + ; strcat() + msgStrcat db "TEST strcat()",NL,EOS + strcat1 db TAB,"strcat(strBuff2, str1, 32): ",NL,TAB,TAB,EOS + strcat2 db TAB,"strcat(strBuff2, str5, 32): ",NL,TAB,TAB,EOS + section .data section .bss strBuff1 resb 32 - strBuff2 resb 4 + strBuff2 resb 32 section .text global _start @@ -125,6 +132,34 @@ _start: call puts %endif +;--- +;--- strcat +;--- +%if TEST_strcat + lea rdi, [rel msgStrcat] + call print + + ; TEST 1 + lea rdi, [rel strcat1] + call print + lea rdi, [rel strBuff2] + lea rsi, [rel str1] + mov rcx, 32 + call strcat + mov rdi, rax + call puts + + ; TEST 3 + lea rdi, [rel strcat2] + call print + lea rdi, [rel strBuff2] + lea rsi, [rel str5] + mov rcx, 32 + call strcat + mov rdi, rax + call puts +%endif + ;--- ;--- exit() ;---