PROWAREtech

articles » current » assembly » x86 » procedures » strtowcs

x86 Assembly: strtowcs Procedure

ASCII string to wide-char string.

Use MASM for Visual C++ Express Edition 2005 to compile this procedure.

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);'

.386P

.model FLAT

PUBLIC	_strtowcs_asm

_TEXT	SEGMENT
_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

label1:

	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

label2:

	mov  WORD PTR [eax], 0

	ret  0

_strtowcs_asm ENDP
_TEXT	ENDS
END

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

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
CLOSE