sTACK GRO UPWARD OR DOWNWARD

Discussion in 'General Electronics Chat' started by aamirali, Nov 1, 2012.

  1. aamirali

    Thread Starter Member

    Feb 2, 2012
    415
    1
    Stcak grow upward or downward or it is architecture dependent.

    Like in 8051 it grows upward.
    But then how does heap grow- upward/downward.
    I read that in some mcu stack grow downward & heap grow upward & both should never cross.
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,447
    3,363
    Upward and downward are relative terms.

    Memory addresses increase from 0 to max address. So we would regard going from low address to high address as upward.

    Some folks draw memory maps differently from others. If you draw memory maps starting with address zero from the top, that would confuse the meaning of downwards and upwards. So let us not use memory maps.

    Program code goes from low memory to high memory. And so does data space.
    The stack is usually placed in RAM space at the high end of memory.
    Hence the stack grows from high address to low address.

    Is that downward or upward?

    To avoid confusion, one should draw memory maps with address 0 at the bottom.

    The problem is we use the analogy of a stack of cafeteria trays or dinner plates when we introduce stacks. This means that when we push and pop stacks, our stack of plates would have to start at the ceiling and defy gravity.

    You can't win them all.
     
    Last edited: Nov 1, 2012
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,606
    Correct answer is yes. Or no. Or it depends.

    Even when there is a hardware stack there is no reason a compiler cannot create a software stack.
     
  4. aamirali

    Thread Starter Member

    Feb 2, 2012
    415
    1
    Considering 0 to high address as stack grow upward & vice versa.

    Now in 89C51, SP points to 07 initially & with each push SP increases i.e. address increases & each POP SP decreases i.e. address decreses.

    So doesn't that mean stack grow upward.
     
  5. aamirali

    Thread Starter Member

    Feb 2, 2012
    415
    1
    Considering 0 to high address as stack grow upward & vice versa.

    Now in 89C51, SP points to 07 initially & with each push SP increases i.e. address increases & each POP SP decreases i.e. address decreses.

    So doesn't that mean stack grow upward.
     
  6. atferrari

    AAC Fanatic!

    Jan 6, 2004
    2,648
    764
    As in many other related subjects the graphic you use to represent it, matters: one of the 18F PIC (maybe 4550?) shows one of the memory spaces starting at 0 increasing the pointer to let's say H'FFFF'. Problem is that the "0" memory position is drawn at the top of the page. Upwards in numbers, downwards in the graphic.

    To avoid confusing myself with that contradiction (?) I decided to think of it not using any graphic for reference. And since then, that stack or whatever it was, was growing upwards.

    IIRC the stack in the Timex Sinclair was "growing" downards. First contact with real life contradiction (?).
     
  7. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    Many CPUs grow the stack downwards.
     
Loading...