0

Why do we need these two functions at the start of every assembly program?

pushl %ebp
movl %esp,%ebp

I don't know what they do and they don't seem to contribute to the code functionality. I come from the Python world so this is pretty weird.

  • We don't. Where do you see it? – rustyx Apr 14 '18 at 19:56
  • In all of the programs in my assembly language course – asndonsadoasndo231213 Apr 14 '18 at 20:05
  • typical function prologue to create a stack frame. – Michael Petch Apr 14 '18 at 20:07
  • Is there a place I can learn more about this? – asndonsadoasndo231213 Apr 14 '18 at 20:09
  • https://en.wikibooks.org/wiki/X86_Disassembly/Functions_and_Stack_Frames – Michael Petch Apr 14 '18 at 20:11
  • 2
    See this SO answer: ["What is stack frame in assembly?"](https://stackoverflow.com/a/3700219/1305969) – zx485 Apr 14 '18 at 20:30
  • Thanks that makes more sense. Is it theoretically possible to access the local variables of the calling function? E.g. `EBP - 100` when we've only defined maybe 1 input variable? – asndonsadoasndo231213 Apr 14 '18 at 21:23
  • Locals of the caller will be at positive offsets not negative. You'd also need to know the exact layout of your caller (and you may of course have multiple call sites). – Jester Apr 14 '18 at 21:40
  • From what I understand `EBP - 4` will contain our first input parameter and `EBP - 100` will access some byte of the local variables of the caller function. – asndonsadoasndo231213 Apr 14 '18 at 21:47
  • no. try in debugger watching `esp`, `ebp`, and memory view into area of `esp` address. Watch the situation ahead/after `call` and after prologue instructions, and then after adjusting `esp` to reserve space for local variables. ... and answer to your question in title: it's not needed, you can write machine code which does not use `ebp` and stack frame at all, nor the calling convention putting arguments into stack. (but if you are writing something to interface with python, you have to follow certain calling convention and write code in accordance with it) – Ped7g Apr 14 '18 at 21:59

0 Answers0