Why is the stack pointer always the same?

From: Derek Fountain (nospam_at_example.com)
Date: 08/24/04


Date: Tue, 24 Aug 2004 10:38:57 +0800

Why is it that when starting a program, the stack pointer always starts
with the same value? That is, if I have this:

unsigned long get_sp(void) {
    __asm__("movl %esp,%eax");
}
void main() {
   printf("0x%x\n", get_sp());
}

and run it over and over, it always prints the same address?

In my ignorance I'd have thought that when a program is started up by
the shell, the stack would be placed somewhere according to some rules
which consider what memory is free etc. This obviously isn't the case.
What's going on?



Relevant Pages

  • Re: which object orient language is most suitable for embedded programming?
    ... All the 'boot' has to do is set the stack pointer, ... pass a pair of values defining the limits of memory. ... These things can be highly dependant on the particular board - you can't just use the startup code that comes with the compiler. ...
    (comp.arch.embedded)
  • Re: which object orient language is most suitable for embedded programming?
    ... We are talking about embedded systems here and ROM mounted code. ... All the 'boot' has to do is set the stack pointer, ... pass a pair of values defining the limits of memory. ...
    (comp.arch.embedded)
  • Re: In memory layout of the C++ program
    ... > I read the common object file format. ... > machine language is converted to exe file. ... > in done in memory i.e virtual memory and the physical memory also. ... [Stack Pointer] ...
    (microsoft.public.vc.language)
  • Re: Stack way linear interpolation
    ... This doesn't have the overflow problems that using * / separately ... in nearly all cases, the X,Y pairs came from memory. ... as moving the the return stack pointer. ...
    (comp.lang.forth)
  • Re: startup.s --> main.c (boot loader problem)
    ... You just have to initialize the stack pointer to use an area of memory that ... memory regions are used by eboot, ... Linux's boot loader can work fine, but mine will get that trouble. ...
    (microsoft.public.windowsce.platbuilder)