Hi friends
I am trying to understand function call mechanism. I have questions about prolog/epilog code.
What are the benefits of writing prolog epilog code for our proc? I mean what will happen if we write our proc without prolog epilog code? Although i didnt write prolog/epilog code will it contiue to work?

I asked this because i disassembly some exes with VC++ . And i see that compiler generate prolog/epilog code for all functions. But i know that some asm codes don't include prolog/epilog code.
So Is "writing prolog/epilog code"  a rule or better coding ?

This is sample code. For example, i removed prolog/epilog part, it compiles, no error. But will it be a problem for a long program.
I am looking for your answers.


; Create a stack frame
push ebp
mov ebp,esp

; Allocate temp
sub esp,4

; temp = arg1 + arg2
mov eax,
add eax,
mov ,eax

; Other things with temp

; Set the return value
mov eax,

; Clean up the stack frame
mov esp,ebp
pop ebp

; Return

push 4
push 2
push .next

; Call
jmp foo


; Pop arg1, arg2, and the return addr
add esp,12

; return 0;
mov eax,0

Posted on 2007-04-25 02:08:01 by sawer
Consider the lack of of prologs:
1) without referencing vars by ebp, on every push/pop, the relative offsets of local vars change. Hard to track when doing some worthy asm proc
2) we'd have to "sub esp,LOCALS_SIZE" every time we're about to call a proc inside our proc.

Posted on 2007-04-25 03:36:18 by Ultrano
Hmm OK.
Thank you Ultrano.
Posted on 2007-04-25 09:35:57 by sawer
prolog/epilog is usually omitted when you want to optimize particular function (leaving you with a spare register - ebp, etc..)
it absolutely does not make sense to force yourself writing such (frame free) code for every proc.

also i think its a good practice to write some code that accesses variables/parameters trough stack register, so you learn how the stack is laid out.

btw, i believe that all good C/C++ compilers will produce frame free code if you turn on some optimizations.
Posted on 2007-04-25 12:16:19 by drizz
Drizz is right - when we need to optimize some proc (that usually won't use the "call" opcode), we remove the prologue and epilogue.

Also, VC++2005 generates frameless code when possible/needed. IIRC VC++6 did that, too.
Posted on 2007-04-25 12:54:47 by Ultrano
I read an interesting article about recursive func. It says:
Every time you call a function, a thingy (very technical term) called an activation record is pushed onto the stack. The activation record, also known as a stack frame, contains things such as local variables for the function, arguments, and the address of the previous activation record so that it knows how to return from whence it came. Because of these activation records, two calls to the same function aren't actually the same activation record, that's why recursion works. Each new call to fact_recursive has its own memory, its own argument copies, its own everything.

Because this all works like a stack, each new activation record covers up the previous record. When the function finally reaches the base case and returns, each activation record is popped from the stack until execution finally leaves the first call to fact_recursive. This is called unwinding the stack, you'll hear that term again sometime in your programming careers, but I won't say where. :)

push ebp
mov ebp,esp
We push bp so we can store it.
So If we optimize compiler that do not add prolog/epilog code, can't we use recursive functions?
Is this right?
Posted on 2007-04-25 13:19:46 by sawer