Quick introduction to reverse engineering for beginners
Quick introduction to reverse engineering for beginners
Quick introduction to reverse engineering for beginners
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
There are some redundant code present, however, it’s shorter then MSVC version without optimization.<br />
Now let’s try GCC with optimization turned on -O3:<br />
public f<br />
f proc near<br />
arg_0 = dword ptr 8<br />
push ebp<br />
mov ebp, esp<br />
mov eax, [ebp+arg_0]<br />
pop ebp<br />
or ah, 40h<br />
and ah, 0FDh<br />
retn<br />
f endp<br />
That’s shorter. It is important <strong>to</strong> note that compiler works with EAX register part via AH register —<br />
that’s EAX register part from 8th <strong>to</strong> 15th bits inclusive.<br />
Important note: 16-bit CPU 8086 accumula<strong>to</strong>r was named AX and consisted of two 8-bit halves — AL<br />
(lower byte) and AH (higher byte). In 80386 almost all regsiters were extended <strong>to</strong> 32-bit, accumula<strong>to</strong>r was<br />
named EAX, but <strong>for</strong> the sake of compatibility, its older parts may be still accessed as AX/AH/AL registers.<br />
Because all x86 CPUs are 16-bit 8086 CPU successors, these older 16-bit opcodes are shorter than newer<br />
32-bit opcodes. That’s why or ah, 40h instruction occupying only 3 bytes. It would be more logical way<br />
<strong>to</strong> emit here or eax, 04000h, but that’s 5 bytes, or even 6 (if register in first operand is not EAX).<br />
It would be even shorter if <strong>to</strong> turn on -O3 optimization flag and also set regparm=3.<br />
public f<br />
f proc near<br />
push ebp<br />
or ah, 40h<br />
mov ebp, esp<br />
and ah, 0FDh<br />
pop ebp<br />
retn<br />
f endp<br />
Indeed — first argument is already loaded in<strong>to</strong> EAX, so it’s possible <strong>to</strong> work with it in-place. It’s worth<br />
noting that both function prologue (push ebp / mov ebp,esp) and epilogue can easily be omitted here, but<br />
GCC probably isn’t good enough <strong>for</strong> such code size optimizations. However, such short functions are better<br />
<strong>to</strong> be inlined functions 49 .<br />
1.14.3 Shifts<br />
Bit shifts in C/C++ are implemented via ≪ and ≫ opera<strong>to</strong>rs.<br />
Here is a simple example of function, calculating number of 1 bits in input variable:<br />
#define IS_SET(flag, bit) ((flag) & (bit))<br />
int f(unsigned int a)<br />
{<br />
int i;<br />
int rt=0;<br />
<strong>for</strong> (i=0; i