我有几个关于堆栈的问题。
- 堆栈在 CPU 还是 RAM 中?
- 堆栈是运行 OPcode 的地方吗?
- EIP 在 CPU 还是 RAM 中?
堆栈始终在 RAM 中。有一个堆栈指针保存在CPU 中的一个寄存器中,它指向堆栈的顶部,即堆栈顶部位置的地址。
你说的是哪个CPU?
有些可能包含用于调用堆栈的内存,有些包含可用于调用堆栈但需要操作系统实现调用堆栈管理代码的内存,还有一些根本不包含可写内存。例如,x86 架构倾向于在 CPU 中内置一个或多个代码缓存和数据缓存。
一些 CPU 或操作系统实现了使特定内存区域不可执行的操作。例如,为了防止基于堆栈的缓冲区溢出,许多操作系统使用基于硬件和/或软件的数据执行预防,这可能会阻止堆栈内存作为代码执行。有些没有;完全有可能使用 x86 CPU 数据缓存行来存储调用堆栈和要在更快的内存中执行的代码。
EIP听起来像是 IA32 CPU 架构的寄存器。如果您指的是 IA-32,那么是的,这是一个 CPU 操作,尽管许多操作系统会将其切换到 RAM 或从 RAM 切换以模拟多任务处理。
In modern architectures stack is mapped in ram. Programming languages such ar C, C++, Pascal can allocate memory in ram, this is called Heap allocation, and other variables which live withing functions are stack allocated. This dictated processors and operating systems to consider stack mapped within ram segment. And for processors with Memory Management Unit this can be anywhere in the ram. However, intel 8080 had a state bit indicating when it reads/writes from stack, thus stack could be implemented physically isolated from RAM. It is not known to me if such machine was implemented, but think of the situation, what memory does a C pointer points to, Heap or Stack. Should Stack separation gain popularity we should have stack pointer and heap pointer in modern programming languages.