Reverse Engineering for Beginners

(avery) #1
CHAPTER 48. TOUPPER() FUNCTION CHAPTER 48. TOUPPER() FUNCTION

Chapter 48


toupper() function


Another very popular function transforms a symbol from lower case to upper case, if needed:

char toupper (char c)
{
if(c>='a' && c<='z')
return c-'a'+'A';
else
return c;
}

The'a'+'A'expression is left in the source code for better readability, it will be optimized by compiler, of course^1.

TheASCIIcode of “a” is 97 (or 0x61), and 65 (or 0x41) for “A”. The difference (or distance) between them in theASCIItable
is 32 (or 0x20).

For better understanding, the reader may take a look at the 7-bit standardASCIItable:

Figure 48.1:7-bitASCIItable in Emacs

48.1 x64


48.1.1 Two comparison operations


Non-optimizing MSVC is straightforward: the code checks if the input symbol is in [97..122] range (or in [‘a’..‘z’] range) and
subtracts 32 if it’s true. There are also some minor compiler artefact:

Listing 48.1: Non-optimizing MSVC 2013 (x64)

1 c$ = 8
2 toupper PROC
3 mov BYTE PTR [rsp+8], cl
4 movsx eax, BYTE PTR c$[rsp]
5 cmp eax, 97
6 jl SHORT $LN2@toupper
7 movsx eax, BYTE PTR c$[rsp]
8 cmp eax, 122


(^1) However, to be meticulous, there still could be compilers which can’t optimize such expressions and will leave them right in the code.

Free download pdf