From 2fb43126152646435f72bc1242d36533bf3231ae Mon Sep 17 00:00:00 2001 From: Kwarde Date: Sun, 6 Jul 2025 11:15:58 +0200 Subject: [PATCH] Adds strcpy() --- src/string.asm | 33 +++++++++++++++++++++++++++ src/tests.asm | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/src/string.asm b/src/string.asm index 9205533..011f110 100644 --- a/src/string.asm +++ b/src/string.asm @@ -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 diff --git a/src/tests.asm b/src/tests.asm index 0ce111b..bece942 100644 --- a/src/tests.asm +++ b/src/tests.asm @@ -7,12 +7,20 @@ extern print extern puts ; string.asm extern strlen +extern strcpy ; convert.asm section .rodata TEST_print equ 1 TEST_puts equ 1 TEST_strlen equ 1 + TEST_strcpy equ 1 + + str1 db "Hello, world!",EOS + str2 db "Hello, World!",EOS + str3 db "Hello world!",EOS + str4 db "Howdy environment!",EOS + str5 db "The quick brown fox jumps over the lazy dog",EOS ; print() msgPrint db "TEST print()",NL,EOS @@ -24,9 +32,18 @@ section .rodata strlenStr1 db "Hello",EOS strlenStr2 db "Hello, world!",NL,EOS + ; strcpy() + msgStrcpy db "TEST strcpy()",NL,EOS + strcpy1 db TAB,"strcpy(strBuff1, str1, 12): ",NL,TAB,TAB,EOS + strcpy2 db TAB,"strcpy(strBuff1, str1, 13): ",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 + section .data section .bss + strBuff1 resb 32 + strBuff2 resb 4 section .text global _start @@ -63,6 +80,51 @@ _start: call strlen %endif +;--- +;--- strcpy() +;--- +%if TEST_strcpy + lea rdi, [rel msgStrcpy] + call print + + ; TEST 1 + lea rdi, [rel strcpy1] + call print + lea rdi, [rel strBuff1] + lea rsi, [rel str1] + mov rdx, 12 + call strcpy + mov rdi, rax + call puts + ; TEST 2 + lea rdi, [rel strcpy2] + call print + lea rdi, [rel strBuff1] + lea rsi, [rel str1] + mov rdx, 13 + call strcpy + mov rdi, rax + call puts + ; TEST 3 + lea rdi, [rel strcpy3] + call print + lea rdi, [rel strBuff1] + lea rsi, [rel str1] + mov rdx, 32 + call strcpy + mov rdi, rax + call puts + ; TEST 4 + lea rdi, [rel strcpy4] + call print + lea rdi, [rel strBuff1] + lea rsi, [rel str5] + mov rdx, 32 + call strcpy + mov rdi, rax + call puts +%endif + ;--- ;--- exit() ;---