23.03.2013 Views

Quick introduction to reverse engineering for beginners

Quick introduction to reverse engineering for beginners

Quick introduction to reverse engineering for beginners

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

fld QWORD PTR _a$[esp-4]<br />

; current stack state: ST(0) = _a, ST(1) = _b<br />

fcom ST(1) ; compare _a and ST(1) = (_b)<br />

fnstsw ax<br />

test ah, 65 ; 00000041H<br />

jne SHORT $LN5@d_max<br />

fstp ST(1) ; copy ST(0) <strong>to</strong> ST(1) and pop register, leave (_a) on <strong>to</strong>p<br />

; current stack state: ST(0) = _a<br />

ret 0<br />

$LN5@d_max:<br />

fstp ST(0) ; copy ST(0) <strong>to</strong> ST(0) and pop register, leave (_b) on <strong>to</strong>p<br />

; current stack state: ST(0) = _b<br />

ret 0<br />

_d_max ENDP<br />

FCOM is different from FCOMP is that sense that it just comparing values and leave FPU stack in the same<br />

state. Unlike previous example, operands here in <strong>reverse</strong>d order. And that is why result of comparision in<br />

C3/C2/C0 will be different:<br />

∙ If a>b in our example, then C3/C2/C0 bits will be set as: 0, 0, 0.<br />

∙ If b>a, then bits will be set as: 0, 0, 1.<br />

∙ If a=b, then bits will be set as: 1, 0, 0.<br />

It can be said, test ah, 65 instruction just leave two bits — C3 и C0. Both will be zeroes if a>b: in<br />

that case JNE jump will not be triggered. Then FSTP ST(1) is following — this instruction copies ST(0)<br />

value in<strong>to</strong> operand and popping one value from FPU stack. In other words, that instruction copies ST(0)<br />

(where _a value now) in<strong>to</strong> ST(1). After that, two values of _a are at the <strong>to</strong>p of stack now. After that, one<br />

value is popping. After that, ST(0) will contain _a and function is finished.<br />

Conditional jump JNE is triggered in two cases: of b>a or a==b. ST(0) in<strong>to</strong> ST(0) will be copied, it<br />

is just like idle (NOP) operation, then one value is popping from stack and <strong>to</strong>p of stack (ST(0)) will contain<br />

what was in ST(1) be<strong>for</strong>e (that is _b). Then function finishes. That instruction used here probably because<br />

FPU has no instruction <strong>to</strong> pop value from stack and not <strong>to</strong> s<strong>to</strong>re it anywhere.<br />

Well, but it is still not over.<br />

GCC 4.4.1<br />

d_max proc near<br />

b = qword ptr -10h<br />

a = qword ptr -8<br />

a_first_half = dword ptr 8<br />

a_second_half = dword ptr 0Ch<br />

b_first_half = dword ptr 10h<br />

b_second_half = dword ptr 14h<br />

push ebp<br />

mov ebp, esp<br />

42

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!