0

C function uses call stack(stack frame) to push/pop registers before/after function call. If ebp is the frame pointer that's used to visit all variables on stack, then seems esp is redundant?

Then why in prolog/epilog of a function call, we operate both esp and ebp? If I use only one register to do this job, any inconvenient places?

Thanks.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Hind Forsum
  • 9,717
  • 13
  • 63
  • 119
  • 2
    ebp keeps track of the current stack frame, esp keeps track of the top of stack. ebp is the one that's technically not needed, but if you're hand-writing assembly, it's much easier to use both. – fuz Sep 18 '16 at 11:25
  • call/ret is still based on esp, so even if you only use ebp-based addressing, you always have to drag esp along anyway. –  Sep 18 '16 at 11:55
  • 2
    If you have a question about the code generation of your C compiler then you need to mention its name and show the compile options you used. Standard mistake is to look at non-optimized code. In general, a half-decent compiler will push esp because it is going to be used in a subsequent call anyway and will eliminate ebp in a leaf function. – Hans Passant Sep 18 '16 at 13:10

0 Answers0