Adds strcat()

This commit is contained in:
2025-07-06 12:20:34 +02:00
parent 1a6ea4ffe6
commit a2daf9602b
2 changed files with 78 additions and 3 deletions

View File

@@ -3,6 +3,7 @@
section .text section .text
global strlen global strlen
global strcpy global strcpy
global strcat
;----- strlen(*str[]) -----; ;----- strlen(*str[]) -----;
; Gets the length of given string ; Gets the length of given string
@@ -24,7 +25,7 @@ strlen:
;----- strcpy(*dest[], *src[], maxLength) -----; ;----- strcpy(*dest[], *src[], maxLength) -----;
; Copies one string (src) into the other (dest), overwriting dest ; Copies one string (src) into the other (dest), overwriting dest
; NOTE: maxLength must never exceed size of dest[]. ; NOTE: maxLength must never exceed size of dest[].
; Return value: Pointer to dest ; Return value: Pointer to dest[]
; Used registers: ; Used registers:
; rax* (ret) Pointer to dest[] ; rax* (ret) Pointer to dest[]
; rdi* (arg) Pointer to dest[] ; rdi* (arg) Pointer to dest[]
@@ -43,9 +44,48 @@ strcpy:
cmp rcx, rdx cmp rcx, rdx
jb .copy jb .copy
dec rcx dec rcx
.copy .copy:
mov rdi, r8 mov rdi, r8
rep movsb rep movsb
mov byte [rdi], EOS mov byte [rdi], EOS
mov rax, r8 mov rax, r8
ret 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

View File

@@ -8,6 +8,7 @@ extern puts
; string.asm ; string.asm
extern strlen extern strlen
extern strcpy extern strcpy
extern strcat
; convert.asm ; convert.asm
section .rodata section .rodata
@@ -15,6 +16,7 @@ section .rodata
TEST_puts equ 1 TEST_puts equ 1
TEST_strlen equ 1 TEST_strlen equ 1
TEST_strcpy equ 1 TEST_strcpy equ 1
TEST_strcat equ 1
str1 db "Hello, world!",EOS str1 db "Hello, world!",EOS
str2 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 strcpy3 db TAB,"strcpy(strBuff1, str1, 32): ",NL,TAB,TAB,EOS
strcpy4 db TAB,"strcpy(strBuff1, str5, 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 .data
section .bss section .bss
strBuff1 resb 32 strBuff1 resb 32
strBuff2 resb 4 strBuff2 resb 32
section .text section .text
global _start global _start
@@ -125,6 +132,34 @@ _start:
call puts call puts
%endif %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() ;--- exit()
;--- ;---