Reverse Engineering for Beginners

(avery) #1

CHAPTER 35. TEMPERATURE CONVERTING CHAPTER 35. TEMPERATURE CONVERTING


35.1.2 Optimizing MSVC 2012 x64.


The code is almost the same, but we can findINT 3instructions after eachexit()call.


xor ecx, ecx
call QWORD PTR __imp_exit
int 3

INT 3is a debugger breakpoint.


It is known thatexit()is one of the functions which can never return^1 , so if it does, something really odd has happened
and it’s time to load the debugger.


35.2 Floating-point values.


#include <stdio.h>
#include <stdlib.h>


int main()
{
double celsius, fahr;
printf ("Enter temperature in Fahrenheit:\n");
if (scanf ("%lf", &fahr)!=1)
{
printf ("Error while parsing your input\n");
exit(0);
};


celsius = 5 * (fahr-32) / 9;

if (celsius<-273)
{
printf ("Error: incorrect temperature!\n");
exit(0);
};
printf ("Celsius: %lf\n", celsius);
};


MSVC 2010 x86 usesFPUinstructions...


Listing 35.2: Optimizing MSVC 2010 x86

$SG4038 DB 'Enter temperature in Fahrenheit:', 0aH, 00H
$SG4040 DB '%lf', 00H
$SG4041 DB 'Error while parsing your input', 0aH, 00H
$SG4043 DB 'Error: incorrect temperature!', 0aH, 00H
$SG4044 DB 'Celsius: %lf', 0aH, 00H


real@c071100000000000 DQ 0c071100000000000r ; -273
real@4022000000000000 DQ 04022000000000000r ; 9
real@4014000000000000 DQ 04014000000000000r ; 5
real@4040000000000000 DQ 04040000000000000r ; 32


_fahr$ = -8 ; size = 8
_main PROC
sub esp, 8
push esi
mov esi, DWORD PTR impprintf
push OFFSET $SG4038 ; 'Enter temperature in Fahrenheit:'
call esi ; call printf()
lea eax, DWORD PTR _fahr$[esp+16]
push eax
push OFFSET $SG4040 ; '%lf'
call DWORD PTR impscanf
add esp, 12 ; 0000000cH
cmp eax, 1
je SHORT $LN2@main


(^1) another popular one islongjmp()

Free download pdf