Hi! I´m new in this Forum.

My problem is:
I have two functions for replacing characters in a string. One in C++ other in inline Assembly:

Example: Source string "björk" to destination string "bjork"

In C++:

void ReplaceChar(char* s, int len)
{
	for (int i = 0; i < len; i++)
		if (s[i] < 0)
			if (s[i] >= 'À' && s[i] <= 'Ý')
			{
				if (s[i] <= 'Ï')
				{
					if (s[i] <= 'Ç')
					{
						if (s[i] <= 'Ä')
							s[i] = 'A';
						else 
							s[i] = 'C';
					}
					else if (s[i] <= 'Ë')
						s[i] = 'E';
					else
						s[i] = 'I';
				}
				else if (s[i] <= 'Ö')
					{
						if (s[i] == 'Ñ')
							s[i] = 'N';
						else
							s[i] = 'O';
					}
					else if (s[i] <= 'Ü')
						s[i] = 'U';
					else 
						s[i] = 'Y';
			}
			else if (s[i] >= 'à')
			{
				if (s[i] <= 'ï')
				{
					if (s[i] <= 'ç')
					{
						if (s[i] <= 'ä')
							s[i] = 'a';
						else
							s[i] = 'c';
					}
					else if (s[i] <= 'ë')
						s[i] = 'e';
					else
						s[i] = 'i';
				}
				else if (s[i] <= 'ö')
					{
						if (s[i] == 'ñ')
							s[i] = 'n'; 
						else 
							s[i] = 'o';
					}
					else if (s[i] <= 'ü')
						s[i] = 'u';
					else 
						s[i] = 'y';
			}
}

In inline Assembly:

void ReplaceChar(char* s)
{
	__asm 
	{
			mov ebx, s
			dec ebx

		Repeat:	add ebx, 1
			mov eax, [ebx]
			and eax, 0xff // 255
			cmp eax, 0x0 // '/0'
			je  End

			cmp eax, 0xc0 // 'À'
			jb  Repeat
			cmp eax, 0xe0 // 'à'
			jae LowerCase
			cmp eax, 0xc4 // 'Ä'
			jbe A
			cmp eax, 0xc7 // 'Ç'
			je C
			cmp eax, 0xcb // 'Ë'
			jbe E
			cmp eax, 0xcf // 'Ï'
			jbe I
			cmp eax, 0xd1 // 'Ñ'
			je N
			cmp eax, 0xd6 // 'Ö'
			jbe O
			cmp eax, 0xdc // 'Ü'
			jbe U
				
			mov BYTE PTR [ebx], 'Y'
			jmp Repeat

		A:	mov BYTE PTR [ebx], 'A'
			jmp Repeat
		C:	mov BYTE PTR [ebx], 'C'
			jmp Repeat
		E:	mov BYTE PTR [ebx], 'E'
			jmp Repeat
		I:	mov BYTE PTR [ebx], 'I'
			jmp Repeat
		N:	mov BYTE PTR [ebx], 'N'
			jmp Repeat
		O:	mov BYTE PTR [ebx], 'O'
			jmp Repeat
		U:	mov BYTE PTR [ebx], 'U'
			jmp Repeat

		LowerCase: cmp eax, 0xe4 // 'ä'
			jbe a_
			cmp eax, 0xe7 // 'ç'
			je c_
			cmp eax, 0xeb // 'ë'
			jbe e_
			cmp eax, 0xef // 'ï'
			jbe i_
			cmp eax, 0xf1 // 'ñ'
			je n_
			cmp eax, 0xf6 // 'ö'
			jbe o_
			cmp eax, 0xfc // 'ü'
			jbe u_

			mov BYTE PTR [ebx], 'y'
			jmp Repeat

		a_:	mov BYTE PTR [ebx], 'a'
			jmp Repeat
		c_:	mov BYTE PTR [ebx], 'c'
			jmp Repeat
		e_:	mov BYTE PTR [ebx], 'e'
			jmp Repeat
		i_:	mov BYTE PTR [ebx], 'i'
			jmp Repeat
		n_:	mov BYTE PTR [ebx], 'n'
			jmp Repeat
		o_:	mov BYTE PTR [ebx], 'o'
			jmp Repeat
		u_:	mov BYTE PTR [ebx], 'u'
			jmp Repeat

		End:
	}
}

My question is:

The function in inline Assembly is faster than the function in C++? And it is well optimized?

Please answer me.

Thanks. :)

If you want to know whether one chunk of code or another is faster you need to actually run the tests. Repeatedly and on your platform.

If you do it with profiling on, you will also find out what the hot spots are so that you can optimize as needed.

The three rules of optimization:
ONE: Is it fast enough? Then don't do it
TWO: Try to find a better algorithm
THREE: Only after ONE and TWO are considered, then measure first and only fix the things that will make an actual difference.

FOUR: If you want your code to be portable across operating systems and even compilers then don't do it in assembly. Assembly language is not portable. And inline assembly may not be portable between different compilers. Most C and C++ compilers today are smart enough to do a better job at producing assembly code than the majority of human coders.

If you want to know whether one chunk of code or another is faster you need to actually run the tests. Repeatedly and on your platform.

If you do it with profiling on, you will also find out what the hot spots are so that you can optimize as needed.

The three rules of optimization:
ONE: Is it fast enough? Then don't do it
TWO: Try to find a better algorithm
THREE: Only after ONE and TWO are considered, then measure first and only fix the things that will make an actual difference.

Thanks. :)

FOUR: If you want your code to be portable across operating systems and even compilers then don't do it in assembly. Assembly language is not portable. And inline assembly may not be portable between different compilers. Most C and C++ compilers today are smart enough to do a better job at producing assembly code than the majority of human coders.

Thanks. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.