Page 1 of 1

GDT Tutorial Clarification

Posted: Fri Dec 02, 2016 4:56 pm
by xls10
Why in none of the example functions for setGdt in the GDT tutorial is there a line for push ebp? I guess I'm just confused since I thought when calling a function from C that the ebp value had to be pushed on the stack to save the frame pointer.

Re: GDT Tutorial Clarification

Posted: Fri Dec 02, 2016 5:09 pm
by heat
xls10 wrote:Why in none of the example functions for setGdt in the GDT tutorial is there a line for push ebp? I guess I'm just confused since I thought when calling a function from C that the ebp value had to be pushed on the stack to save the frame pointer.
Hi,

Stack frames aren't exactly needed when you're calling functions from C. Sometimes they're even eliminated for optimization purposes. Stack frames (what your push %ebp; mov %esp, %ebp; and pop %ebp are setting up) are usually just a debugging feature for you to be able to trace the stack. So yeah, it's safe to not include push %ebp; although there's no harm in including it as well.

Re: GDT Tutorial Clarification

Posted: Fri Dec 02, 2016 5:15 pm
by xls10
Oh I see. I'm just starting to learn how to write assembly callable from C and all of the examples I've seen have that function preamble which includes push ebp. In what cases would absolutely need to have it? Would it be when the function needs to use variables local to the called function? Thanks for the reply though!

Re: GDT Tutorial Clarification

Posted: Fri Dec 02, 2016 6:22 pm
by heat
xls10 wrote:Oh I see. I'm just starting to learn how to write assembly callable from C and all of the examples I've seen have that function preamble which includes push ebp. In what cases would absolutely need to have it? Would it be when the function needs to use variables local to the called function? Thanks for the reply though!
For your assembly function to be callable from C, you essentially need to respect your compiler's ABI. That means that if you're using the SYSV ABI, you need to respect it(saving the registers specified by the ABI, for example).

But yeah, I think you can access local variables without ebp. If I recall correctly, it's only for debugging purposes(take that with a grain of salt though).

Re: GDT Tutorial Clarification

Posted: Sat Dec 03, 2016 3:12 am
by rdos
It depends on the C compiler. A few compilers have ways to specify how parameters are passed, and some even allow you to do this for each function, and allow you to use registers instead of the stack.