strtowcs in x86 Assembly

This procedure, strtowcs_asm, copies a C-string (8-bit) from the source to the wchar_t destination (16-bit) and returns a pointer to the end of the text in the destination buffer so that another string can be concatenated.

TITLE 'extern "C" wchar_t *strtowcs_asm(wchar_t *destination, const char *source);'


.model FLAT

PUBLIC	_strtowcs_asm

_strtowcs_asm PROC NEAR

	mov  edx, DWORD PTR [esp+8] ; source
	mov  cl, BYTE PTR [edx]
	test cl, cl
	mov  eax, DWORD PTR [esp+4] ; destination
	je   SHORT label2


	movsx cx, cl
	mov  WORD PTR [eax], cx
	mov  cl, BYTE PTR [edx+1]
	add  eax, 2
	inc  edx
	test cl, cl
	jne  SHORT label1


	mov  WORD PTR [eax], 0

	ret  0

_strtowcs_asm ENDP

This example C++ code uses this function.

#include <stdlib.h>

extern "C" wchar_t *strtowcs_asm(wchar_t *destination, const char *source);

wchar_t str[100];
int length;

length = strtowcs_asm(strtowcs_asm(strtowcs_asm(strtowcs_asm(str, "How "), "now "), "brown "), "cow!") - str;

// str == L"How now brown cow!"
// length == 18

Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site.