PROWAREtech
x64 Assembly: strcpy Procedure
Copy ASCII string using x86-64 (64-bit) assembly.
This code compiled and tested with Visual Studio 2022.
This procedure, strcpy_asm_x64
, copies a C-string from the source to the destination and returns a pointer to the end of the destination so that another string can be concatenated.
The default behavior of strcpy
is to return the destination buffer but it is much more useful to return the end of the text in the destination buffer.
TITLE 'extern "C" char * strcpy_asm_x64(char *destination, const char *source);'
PUBLIC strcpy_asm_x64
_TEXT SEGMENT
strcpy_asm_x64 PROC
mov r8b, BYTE PTR [rdx] ; rdx = source
test r8b, r8b
mov rax, rcx ; rax = destination
je SHORT label2
label1:
mov BYTE PTR [rax], r8b
mov r8b, BYTE PTR [rdx+1]
inc rax
inc rdx
test r8b, r8b
jne SHORT label1
label2:
mov BYTE PTR [rax], 0
; could set rax to rcx if wanting to return the destination pointer instead of a pointer to the end of destination
ret 0
strcpy_asm_x64 ENDP
_TEXT ENDS
END
This example C++ code uses this function.
#include <iostream>
using namespace std;
extern "C" char* strcpy_asm_x64(char* destination, const char* source);
int main()
{
char buffer[256];
int length = strcpy_asm_x64(strcpy_asm_x64(strcpy_asm_x64(buffer, "HELLO "), "WORLD"), "!!!") - buffer;
cout << buffer << endl;
return 0;
}
Comment