为什么堆栈通常成长向下? [英] Why do stacks typically grow downwards?

查看:257
本文介绍了为什么堆栈通常成长向下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,在架构我个人熟悉(86,6502,等等),堆栈通常是向下生长(即每一个项目压入堆栈导致一个递减SP,而不是增加一个)。

I know that in the architectures I'm personally familiar with (x86, 6502, etc), the stack typically grows downwards (i.e. every item pushed onto the stack results in a decremented SP, not an incremented one).

我想了解一下这个历史的理由。我知道,在一个统一的地址空间,这是方便的,开始在数据段(例如)的相对端堆栈所以如果双方的中间碰撞这里只有一个问题。但为什么堆栈历来获得的顶部?尤其是考虑到这是怎么了概念模式相反的?

I'm wondering about the historical rationale for this. I know that in a unified address space, it's convenient to start the stack on the opposite end of the data segment (say) so there's only a problem if the two sides collide in the middle. But why does the stack traditionally get the top part? Especially given how this is the opposite of the "conceptual" model?

(和注意,在6502体系结构中,叠层也增长向下,即使它是有界的单个256字节的页,并且该方向选择​​似乎任意的。)

(And note that in the 6502 architecture, the stack also grows downwards, even though it is bounded to a single 256-byte page, and this direction choice seems arbitrary.)

推荐答案

作为历史悠久的理由,我不能肯定地说(因为我没有设计过)。我的的想法的对此事的是,早期的CPU得到了他们原来的程序计数器设置为0,这是一个自然的愿望在另一端开始堆栈和向下发展,因为他们的code自然生长上行。

As to the historic rationale, I can't say for certain (because I didn't design them). My thoughts on the matter are that early CPUs got their original program counter set to 0 and it was a natural desire to start the stack at the other end and grow downwards, since their code naturally grows upward.

顺便说一句,请注意,上电复位程序计数器为0的这个设置并不适合的所有的早期的CPU的情况。例如,摩托罗拉6809将获取从地址程序计数器 0xfffe /˚F,所以你可以开始在任意位置运行,具体取决于该地址被提供(通常情况下,但绝不限于,ROM)的

As an aside, note that this setting of the program counter to 0 on reset is not the case for all early CPUs. For example, the Motorola 6809 would fetch the program counter from addresses 0xfffe/f so you could start running at an arbitrary location, depending on what was supplied at that address (usually, but by no means limited to, ROM).

之一的第一件事情某些年前的系统会做将是从顶部扫描存储器,直到它发现将回读写入相同的值的位置,以便它知道安装了实际的RAM(例如,用一个Z80 64K地址空间不一定有64K或RAM,其实64K本来的大规模的在我早期天)。一旦找到前实际地址,会适当设置堆栈指针,然后就可以开始调用子程序。这种扫描一般会由CPU运行code在ROM中所做作为初创的部分。

One of the first things some historical systems would do would be to scan memory from the top until it found a location that would read back the same value written, so that it would know the actual RAM installed (e.g., a z80 with 64K address space didn't necessarily have 64K or RAM, in fact 64K would have been massive in my early days). Once it found the top actual address, it would set the stack pointer appropriately and could then start calling subroutines. This scanning would generally be done by the CPU running code in ROM as part of start-up.

关于堆栈增长,而不是所有的人都往下成长,看到<一个href=\"http://stackoverflow.com/questions/664744/what-is-the-direction-of-stack-growth-in-most-modern-systems/664779#664779\">this回答了解详情。

With regard to the stacks growth, not all of them grow downwards, see this answer for details.

这篇关于为什么堆栈通常成长向下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆