在大多数现代系统中,堆栈增长的方向是什么? [英] What is the direction of stack growth in most modern systems?

查看:117
本文介绍了在大多数现代系统中,堆栈增长的方向是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在准备一些用C编写的培训资料,我希望我的示例适合典型的堆栈模型.

I am preparing some training materials in C and I want my examples to fit the typical stack model.

在Linux,Windows,Mac OSX(PPC和x86),Solaris和最新的Unix中,C堆栈的发展方向如何?

What direction does a C stack grow in Linux, Windows, Mac OSX (PPC and x86), Solaris, and most recent Unixes?

推荐答案

堆栈的增长通常不取决于操作系统本身,而是取决于运行的处理器.例如,Solaris在x86和SPARC上运行.如前所述,Mac OSX在PPC和x86上运行.从运行中的大型System z到

Stack growth doesn't usually depend on the operating system itself, but on the processor it's running on. Solaris, for example, runs on x86 and SPARC. Mac OSX (as you mentioned) runs on PPC and x86. Linux runs on everything from my big honkin' System z at work to a puny little wristwatch.

如果CPU提供任何选择,则OS使用的ABI/调用约定指定了您希望代码调用其他所有人的代码时需要做出的选择.

If the CPU provides any kind of choice, the ABI / calling convention used by the OS specifies which choice you need to make if you want your code to call everyone else's code.

处理器及其方向是:

  • x86:关闭.
  • SPARC:可选.标准的ABI会使用down.
  • PPC:我想.
  • 系统z:在一个链接列表中,我没有骗你(但至少在zLinux中还是这样).
  • ARM:可选,但Thumb2仅具有向下编码(LDMIA =之后递增,STMDB =之前递减).
  • 6502:向下(但仅256个字节).
  • RCA 1802A:可以根据SCRT的实施采用任何方式.
  • PDP11:关闭.
  • 8051:向上.

1802是显示我的年龄的最后几年,它是用来控制早期航天飞机的芯片(基于它的处理能力,我怀疑门是否打开了:-)和我的第二台计算机, COMX-35 (在我的

Showing my age on those last few, the 1802 was the chip used to control the early shuttles (sensing if the doors were open, I suspect, based on the processing power it had :-) and my second computer, the COMX-35 (following my ZX80).

PDP11详细信息来自此处,8051详细信息来自

PDP11 details gleaned from here, 8051 details from here.

SPARC体系结构使用滑动窗口寄存器模型.在体系结构上可见的细节还包括寄存器窗口的循环缓冲区,该缓冲区有效并在内部缓存,并且在上溢/下溢时会出现陷阱.有关详细信息,请参见此处.正如 SPARCv8手册所述,SAVE和RESTORE指令类似于ADD指令加上寄存器窗口旋转.使用正数常量而不是通常的负数将产生向上增长的堆栈.

The SPARC architecture uses a sliding window register model. The architecturally visible details also include a circular buffer of register-windows that are valid and cached internally, with traps when that over/underflows. See here for details. As the SPARCv8 manual explains, SAVE and RESTORE instructions are like ADD instructions plus register-window rotation. Using a positive constant instead of the usual negative would give an upward-growing stack.

前面提到的SCRT技术是另一种-1802使用了一部分或16个16位寄存器用于SCRT(标准调用和返回技术).其中一个是程序计数器,您可以通过SEP Rn指令将任何寄存器用作PC.一个是堆栈指针,两个始终设置为指向SCRT代码地址,一个用于调用,一个用于返回. No 寄存器以特殊方式处理.请记住,这些细节来自内存,它们可能并不完全正确.

The afore-mentioned SCRT technique is another - the 1802 used some or it's sixteen 16-bit registers for SCRT (standard call and return technique). One was the program counter, you could use any register as the PC with the SEP Rn instruction. One was the stack pointer and two were set always to point to the SCRT code address, one for call, one for return. No register was treated in a special way. Keep in mind these details are from memory, they may not be totally correct.

例如,如果R3是PC,R4是SCRT调用地址,R5是SCRT返回地址,R2是堆栈"(引用是在软件中实现的),则SEP R4会将R4设置为PC并开始运行SCRT呼叫代码.

For example, if R3 was the PC, R4 was the SCRT call address, R5 was the SCRT return address and R2 was the "stack" (quotes as it's implemented in software), SEP R4 would set R4 to be the PC and start running the SCRT call code.

然后它将R3存储在R2堆栈"上(我认为R6用于临时存储),向上或向下调整它,抓取R3之后的两个字节,将它们装入,然后执行SEP R3并在新地址上运行.

It would then store R3 on the R2 "stack" (I think R6 was used for temp storage), adjusting it up or down, grab the two bytes following R3, load them into R3, then do SEP R3 and be running at the new address.

要返回,将SEP R5将旧地址从R2堆栈中拉出,向其添加两个地址(以跳过调用的地址字节),将其加载到R3和SEP R3中以开始运行先前的地址.代码.

To return, it would SEP R5 which would pull the old address off the R2 stack, add two to it (to skip the address bytes of the call), load it into R3 and SEP R3 to start running the previous code.

最初,在完成所有基于6502/6809/z80堆栈的代码之后,很难把头缠起来,但仍然以一种碰头撞墙"的方式保持优雅.该芯片的最大卖点之一是全套的16个16位寄存器,尽管事实上您立即丢失了其中的7个(5个用于SCRT,两个用于DMA和内存中断).啊,营销胜过现实:-)

Very hard to wrap your head around initially after all the 6502/6809/z80 stack-based code but still elegant in a bang-your-head-against-the-wall sort of way. Also one of the big selling features of the chip was a full suite of 16 16-bit registers, despite the fact you immediately lost 7 of those (5 for SCRT, two for DMA and interrupts from memory). Ahh, the triumph of marketing over reality :-)

系统z实际上非常相似,使用其R14和R15寄存器进行呼叫/返回.

System z is actually quite similar, using its R14 and R15 registers for call/return.

这篇关于在大多数现代系统中,堆栈增长的方向是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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