GDT Tutorial Clarification

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
xls10
Posts: 2
Joined: Fri Dec 02, 2016 4:42 pm
Libera.chat IRC: xls10

GDT Tutorial Clarification

Post 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.
heat
Member
Member
Posts: 103
Joined: Sat Mar 28, 2015 11:23 am
Libera.chat IRC: heat

Re: GDT Tutorial Clarification

Post 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.
If some of you people keep insisting on having backwards compatibitity with the stone age, we'll have stone tools forever.
My Hobby OS: https://github.com/heatd/Onyx
xls10
Posts: 2
Joined: Fri Dec 02, 2016 4:42 pm
Libera.chat IRC: xls10

Re: GDT Tutorial Clarification

Post 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!
heat
Member
Member
Posts: 103
Joined: Sat Mar 28, 2015 11:23 am
Libera.chat IRC: heat

Re: GDT Tutorial Clarification

Post 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).
If some of you people keep insisting on having backwards compatibitity with the stone age, we'll have stone tools forever.
My Hobby OS: https://github.com/heatd/Onyx
rdos
Member
Member
Posts: 3303
Joined: Wed Oct 01, 2008 1:55 pm

Re: GDT Tutorial Clarification

Post 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.
Post Reply