EmbeddedRelated.com
Forums

Direction of Stack Growth

Started by karthikbalaguru October 21, 2007
Hi,

Why some processors have stack growing downwards and others have stack
growing upwards ?
Any advantages/disadvantages w.r.t both these designs.
Which is the best model ?

I serached the internet, but i did not find a good link that explains
these stuffs in detail.

Thx in advans,
Karthik Balaguru

karthikbalaguru wrote:
> Hi, > > Why some processors have stack growing downwards and others have stack > growing upwards ? > Any advantages/disadvantages w.r.t both these designs. > Which is the best model ? > > I serached the internet, but i did not find a good link that explains > these stuffs in detail. > > Thx in advans, > Karthik Balaguru
In the beginning, it was customary to start a peogram at or near the bottom of memory and start the return stack at or near the top. That allowed the largest amount of RAM for stack space. When there are two stacke, it is usual to make one grow down andthe other grow up and then to hope that they never meet. Jerry -- Engineering is the art of making what you want from things you can get. ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
On Sun, 21 Oct 2007 04:44:30 -0700, karthikbalaguru wrote:

> Hi, > > Why some processors have stack growing downwards and others have stack > growing upwards ? > Any advantages/disadvantages w.r.t both these designs. > Which is the best model ? > > I serached the internet, but i did not find a good link that explains > these stuffs in detail. > > Thx in advans, > Karthik Balaguru
I don't think it makes a big difference one way or another. After a designer has decided where to vector the program counter on reset, and where to put the interrupt vector table, there may be some slight advantage. But mostly I think the decision is arbitrary. -- Tim Wescott Control systems and communications consulting http://www.wescottdesign.com Need to learn how to apply control theory in your embedded system? "Applied Control Theory for Embedded Systems" by Tim Wescott Elsevier/Newnes, http://www.wescottdesign.com/actfes/actfes.html
karthikbalaguru <karthikbalaguru79@gmail.com> writes:

> Why some processors have stack growing downwards and others > have stack growing upwards?
Tradition.
> Any advantages/disadvantages w.r.t both these designs?
Not especially.
> Which is the best model?
Your choice.
> I serached the internet, but i did not find a good link > that explains these in detail.
There are some processors that can't perform negative offset indexing in which case an upward-growing stack is awkward to program.
karthikbalaguru wrote:

> Why some processors have stack growing downwards and others have stack > growing upwards ? > Any advantages/disadvantages w.r.t both these designs. > Which is the best model ?
Obviously either works. One old trick for allocating a memory block between two different uses when you don't know the amount needed before hand is to start allocating one from the bottom of the block and start allocating the second from the top of the block and allow the two increasing blocks to grow towards each other. This allows total use of the memory block without knowing the individual sizes in advance. Allocating the stack from the top down is sometimes used in conjunction with allocating heap storage from the bottom up within the same memory block. -- Thad
On 2007-10-21, karthikbalaguru <karthikbalaguru79@gmail.com> wrote:

> Why some processors have stack growing downwards and others > have stack growing upwards ?
The designers had different preferences.
> Any advantages/disadvantages w.r.t both these designs.
Not really.
> Which is the best model ?
The one that's supported by your CPU. :) -- Grant Edwards grante Yow! Yow! Are we wet yet? at visi.com
> In the beginning, it was customary to start a peogram at or near the > bottom of memory and start the return stack at or near the top.
Beginning of what? The earliest computer I used that had hardware stack support was the PDP-6, designed circa 1963, and its stacks grew upwards. The B5000 which was designed around the same time or slightly earlier also had upward growing stacks. I see two motivations for the switch in modern computers to stacks that grow downward. One was the use of programming techniques that used heap storage in a fixed address space, which made it attractive to have the heap and stack grow toward each other. (Back in the 60s memory was so valuable that you laid it all out when you wrote your program, and the stack was just one of the areas you laid out.) The other is the influence of the IBM S/360. Its desigers didn't put in explicit stack hardware because they knew it was easy to do a stack in software if you have base and index registers. But its addressing modes have a 12 bit unsigned offset that is added to the base register. If a stack grows upward, a program either needs a frame register separate from the base register to address local variables, or else use extra instructions to simulate negative offsets. If the stack grows down, locals are at positive offsets from the stack pointer. Most (all?) modern computers have signed offsets so it'd work either way, but once you're used to a programming style, why mess with it?
Jerry Avins wrote:
> karthikbalaguru wrote:
>> Why some processors have stack growing downwards and others have stack >> growing upwards ?
(snip)
> In the beginning, it was customary to start a peogram at or near the > bottom of memory and start the return stack at or near the top.
I thought of that, but I didn't see why programs couldn't load at the top and the stack grow from the bottom. I suppose, though, that it helps not to need to do relocation, if the load point is always the same. This is all without virtual storage (dynamic address translation), and a single task (no need to leave memory for anyone else).
> That > allowed the largest amount of RAM for stack space. When there are two > stacke, it is usual to make one grow down andthe other grow up and then > to hope that they never meet.
-- glen
karthikbalaguru wrote:

> Why some processors have stack growing downwards and others have stack > growing upwards ?
Because they can.
> Which is the best model ?
Neither. If there were one that was "the best", just like that, the other would probably no longer be around.
glen herrmannsfeldt <gah@ugcs.caltech.edu> writes:

> Jerry Avins wrote: >> karthikbalaguru wrote: > >>> Why some processors have stack growing downwards and others have stack >>> growing upwards ? > (snip) > >> In the beginning, it was customary to start a peogram at or near the >> bottom of memory and start the return stack at or near the top. > > I thought of that, but I didn't see why programs couldn't > load at the top and the stack grow from the bottom. > I suppose, though, that it helps not to need to do relocation, > if the load point is always the same. > > This is all without virtual storage (dynamic address translation), > and a single task (no need to leave memory for anyone else). > >> That allowed the largest amount of RAM for stack space. When there >> are two stacke, it is usual to make one grow down andthe other grow >> up and then to hope that they never meet. > > -- glen
My experience (which dates from about 1978) supports your observations and Jerry's original explanation. -- % Randy Yates % "Remember the good old 1980's, when %% Fuquay-Varina, NC % things were so uncomplicated?" %%% 919-577-9882 % 'Ticket To The Moon' %%%% <yates@ieee.org> % *Time*, Electric Light Orchestra http://www.digitalsignallabs.com