Hello Jonathon, If understand your question correctly, you asked, why the base pointer %ebp is saved to the stack at the beginning of a function and then popped back off at the end of the function, but NOT at the beginning of the program. I think the answer is this: There's no reason to!
The stack pointer is saved to the base pointer to set up the base pointer to point at the base of the stack at the beginning of the main program. That means: To the base (or top?) of the main program's stack frame. The place the stack pointer was at the very beginning of the program, before it was used to save new variables, etc. The base pointer is just a copy of the stack pointer when that is at the "base" such as when the main program or a function starts. We save the base pointer address on the stack when we start a function, so we can use the base pointer register as a base pointer for that function's stack values. - WITHOUT loosing the base pointer of the main program! Remember the "stack frame"? Both the main program and each function have a "stack frame", that is a set of variables (parameters or arguments) which are used by the program or function and which are passed to it by saving them on the stack. (Manually in case of functions, by LINUX in case of the main program.) The base pointer register is used as a "ground 0", from where the offset of each variable gives the stack address of the wanted variable on the stack. HOWEVER: We are using the SAME stack both for the main program AND all functions. So the main program and all functions must have their stack frame or variables saved at different locations on the stack. The base pointer just points at the base address of the corresponding current code. At the base stack address for the main program in the main program and at the base stack address of a function during that function. Since we are returning to the main program after all functions, we might NEED the base pointer of the main program AGAIN to access arguments / parameters / variables of the main program saved on the stack. But if we used the base pointer as the base for a function's stack frame (which is a different address), we would have lost our base pointer value for the main program!!! HELP!! ;-) That's why we save the base pointer on the stack in every function BEFORE we set it to that current function's stack base (pointer). Then when we return to the main program, we restore the base pointer (register) to what it was before calling the function, namely the main program's stack base address (or ground 0 of it's stack frame). Why don't we save the base pointer on the stack at the beginning of the main program? EASY: Because when we are done with the main program, we do NOT NEED to restore the base pointer to anything it could have been before the main program was started. When the main program ends, EVERYTHING of that program, including the entire stack is destroyed. All that remains of the main program is the return value passed by the interrupt call. I hope this wasn't confusing. If it is, I suggest you re-read chapter 4 about functions very carefully and pay attention why the base pointer is saved and what exactly it actually is and what it's needed for. Hope that helps. Regards, Mark -------- Original-Nachricht -------- > Datum: Sun, 21 Oct 2007 11:46:36 -0600 > Von: "Jonathon Donaldson" <[EMAIL PROTECTED]> > An: [email protected] > Betreff: [Pgubook-readers] Question Regarding Initial Stack Frame > Well, I tried to search the pgubook mailing list archives for an > answer to this question but it seems to be "out of order". I've been > receiving "404 Not Found" for the last few days. I've also looked on > Google but can't seem to find any specific answer, but I might not be > using the correct terms. So forgive me if this question has already > been answered. > > At the very beginning of the main method for the programs provided in > Chapters 5 (toupper.s) and 6 (read-records.s) the very first > instruction saves the stack pointer using the instruction: > > movl %esp, %ebp > > However, Bartlett neglects to push the original %ebp register to the > stack beforehand. I doubt this is a bug as he does this on more than > one occasion. I have looked everywhere for a reason as to why the > "push %ebp" instructions is always used first inside functions but not > within the main method. > > Why do we not have to save the original base pointer register before > we destroy it in the main method? > > I hope there is still someone listening out there. Thanks in advance > for your help! > > -- > Jonathon > > > _______________________________________________ > Pgubook-readers mailing list > [email protected] > http://lists.nongnu.org/mailman/listinfo/pgubook-readers -- Psssst! Schon vom neuen GMX MultiMessenger gehört? Der kanns mit allen: http://www.gmx.net/de/go/multimessenger _______________________________________________ Pgubook-readers mailing list [email protected] http://lists.nongnu.org/mailman/listinfo/pgubook-readers
