Assembly Language for Beginners

(nextflipdebug2) #1

3.16 toupper() function


move $v0, $s0
lw $s0, 0x20+saved_S0($sp)
jr $ra
addiu $sp, 0x20 ; branch delay slot

Registers prefixed with S- are also called “saved temporaries”, so $S0 value is saved in the local stack
and restored upon finish.

3.16 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^20.

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 3.3:7-bitASCIItable in Emacs

3.16.1 x64


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 artifact:

Listing 3.70: 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
9 jg SHORT $LN2@toupper


(^20) 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