PROWAREtech
x86 Assembly: wcsncpy Procedure
Copy wide-char string with specified length.
Use MASM for Visual C++ Express Edition 2005 to compile this procedure.
The wcsncpy_asm
function copies the initial count characters of
source to destination and returns a string to the end of destination.
If count is less than or equal to the length of source, a null character is
not appended automatically to the copied string. If count is greater than the
length of source, destination is padded with null characters up to length
count.
The return value is the end of the text in destination.
TITLE 'extern "C" wchar_t *wcsncpy_asm(wchar_t *destination, const wchar_t *source, unsigned count);'
.386P
.model FLAT
PUBLIC _wcsncpy_asm
_TEXT SEGMENT
_wcsncpy_asm PROC NEAR
push esi
mov esi, DWORD PTR [esp+16] ; count
test esi, esi
je SHORT label4
mov ecx, DWORD PTR [esp+12] ; source
mov edx, DWORD PTR [esp+8] ; destination
label1:
mov ax, WORD PTR [ecx]
mov WORD PTR [edx], ax
add edx, 2
add ecx, 2
test ax, ax
je SHORT label3 ; count exceeded the length of source so copy zeros into destination from this point foward
dec esi
jne SHORT label1
label2:
lea eax, DWORD PTR [edx-2] ; return pointer to the end of the text in destination
pop esi
ret 0
label3:
test esi, esi
je SHORT label2
dec esi
je SHORT label2
push edi
xor eax, eax
mov ecx, esi
shr ecx, 1 ; divide by two
mov edi, edx
rep stosd ; DWORD, store contents of EAX in memory at the offset pointed to by EDI, increment EDI; repeat while ECX > 0
adc ecx, ecx
rep stosw ; WORD, store contents of AX in memory at the offset pointed to by EDI, increment EDI; repeat while ECX > 0
pop edi
lea eax, DWORD PTR [edx-2] ; return pointer to the end of the text in destination
pop esi
ret 0
label4:
lea eax, DWORD PTR [edx-2] ; return pointer to the end of the text in destination
pop esi
ret 0
_wcsncpy_asm ENDP
_TEXT ENDS
END
Comment