32

I have a few question about stack.

  1. Is stack in CPU or RAM?
  2. Is stack a place to run OPcode?
  3. Is EIP in CPU or RAM?
Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
abiati madana
  • 331
  • 1
  • 3
  • 3
  • Those are unrelated questions. No, processor instructions are rarely in the stack. The EIP register is not relevant to the stack except it is pushed onto the stack when a function is called and popped from the stack upon return. – Sam Hobbs May 18 '18 at 01:34

4 Answers4

69

Stack is always in RAM. There is a stack pointer that is kept in a register in CPU that points to the top of stack, i.e., the address of the location at the top of stack.

unxnut
  • 8,509
  • 3
  • 27
  • 41
  • 15
    +1 For specifying that a register from the cpu points to the stack – Juan Antonio Gomez Moriano Oct 25 '13 at 06:30
  • 1
    It depends on the processor. Some processors have no stack. Processors typically use main memory for the stack data but a stack also consists of stack registers and processor instructions and applications. A stack could not possibly work without the machine instructions or something to push onto the stack and pop from the stack. – Sam Hobbs May 18 '18 at 01:29
18

The stack is found within the RAM and not within the CPU. A segment is dedicated for the stack as seen in the following diagram:

enter image description here

From Wiki:

The stack area contains the program stack, a LIFO structure, typically located in the higher parts of memory.

Goaler444
  • 2,591
  • 6
  • 35
  • 53
6

Which CPU are you talking about?

  1. Some might contain memory that is used for callstacks, some contain memory that can be used for callstacks but require the OS to implement the callstack management code, and others contain no writable memory at all. For example, the x86 architecture tends to have one or more code caches and data caches built into the CPU.

  2. Some CPUs or OSes implement operations that make specific areas of memory non-executable. To prevent stack-based buffer overflows, for example, many OSes use hardware and/or software-based data execution prevention, which might prevent stack memory from being executed as code. Some don't; It's entirely possible that an x86 CPU data cache line might be used to store both the callstack and code to be executed in faster memory.

  3. EIP sounds like a register for the IA32 CPU architecture. If you're referring to IA-32, then yes, it's a CPU operation, though many OSes will switch it to/from RAM to emulate multi-tasking.

autistic
  • 1
  • 3
  • 35
  • 80
  • 1
    The cache in the x86 architecture is totally irrelevant, the cache does **nothing** except improve **performance**. Prevention of execution of instructions in the stack are not relevant to how stacks work. The EIP register is nothing more than a **pointer** to a memory location. It is not a CPU operation or **anything** except a **pointer**. The point that stack implementations are processor dependent is very relevant. – Sam Hobbs May 18 '18 at 01:23
  • Sorry, the prevention of execution of instructions in the stack is relevant because the question asked if a stack is a place to run OPcodes. – Sam Hobbs May 18 '18 at 01:36
  • @user34660 As the question is not tagged `[x86]`, we can't assume that this question is solely for common x86 configurations; we have to assume other architectures are also in mind unless OP specifies specifically otherwise. – autistic May 18 '18 at 03:50
0

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.

r9guy
  • 65
  • 7