Reverse Engineering for Beginners

(avery) #1

CHAPTER 16. REPLACING ARITHMETIC INSTRUCTIONS TO OTHER ONES CHAPTER 16. REPLACING ARITHMETIC INSTRUCTIONS TO OTHER ONES


Chapter 16


Replacing arithmetic instructions to other ones


In the pursuit of optimization, one instruction may be replaced by another, or even with a group of instructions.


For example,ADDandSUBcan replace each other: line 18 in listing.52.1.


For example, theLEAinstruction is often used for simple arithmetic calculations:A.6.2 on page 887.


16.1 Multiplication.


16.1.1 Multiplication using addition


Here is a simple example:


Listing 16.1: Optimizing MSVC 2010

unsigned int f(unsigned int a)
{
return a*8;
};


Multiplication by 8 is replaced by 3 addition instructions, which do the same. Apparently, MSVC’s optimizer decided that
this code can be faster.


_TEXT SEGMENT
_a$ = 8 ; size = 4
_f PROC
; File c:\polygon\c\2.c
mov eax, DWORD PTR _a$[esp-4]
add eax, eax
add eax, eax
add eax, eax
ret 0
_f ENDP
_TEXT ENDS
END


16.1.2 Multiplication using shifting.


Multiplication and division instructions by a numbers that’s a power of 2 are often replaced by shift instructions.


unsigned int f(unsigned int a)
{
return a*4;
};


Listing 16.2: Non-optimizing MSVC 2010

_a$ = 8 ; size = 4
_f PROC
push ebp

Free download pdf