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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

2.2 Function prologue and epilogue<br />

Function prologue is instructions at function start. It is often something like this:<br />

push ebp<br />

mov ebp, esp<br />

sub esp, X<br />

What these instruction do: save EBP register value, set EBP <strong>to</strong> ESP and then allocate space in stack <strong>for</strong><br />

local variables.<br />

EBP value is fixed over a period of function execution and it will be used <strong>for</strong> local variables and arguments<br />

access. One can use ESP, but it changing over time and it is not handy.<br />

Function epilogue annuled allocated space in stack, returning EBP value <strong>to</strong> initial state and returning<br />

control flow <strong>to</strong> callee:<br />

mov esp, ebp<br />

pop ebp<br />

ret 0<br />

Epilogue and prologue can make recursion per<strong>for</strong>mance worse.<br />

For example, once upon a time I wrote a function <strong>to</strong> seek right tree node. As a recursive function it would<br />

look stylish but because some time was spent at each function call <strong>for</strong> prologue/epilogue, it was working<br />

couple of times slower than the implementation without recursion.<br />

By the way, that is the reason of tail call 2 existence: when compiler (or interpreter) trans<strong>for</strong>ms recursion<br />

(with which it’s possible: tail recursion) in<strong>to</strong> iteration <strong>for</strong> efficiency.<br />

2 http://en.wikipedia.org/wiki/Tail_call<br />

114

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

Saved successfully!

Ooh no, something went wrong!