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

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

问题描述

我知道在我个人熟悉的架构(x86、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,并且很自然地希望从另一端开始堆栈并向下增长,因为它们的代码自然会向上增长.

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 获取程序计数器,因此您可以在任意位置开始运行,具体取决于在该地址提供的内容(通常,但绝不限于, 只读存储器).

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 在 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.

关于堆栈增长,并非所有堆栈都向下增长,请参阅 this answer 了解详情.

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

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

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