0

I am working on Pintos.

Which is sort of like an educational tool for learning about building operating systems, and am on the second project which is geared around building support for user programs.

So, first order of business is to Set up The Stack! Great.

Problem is - since the beginning of the class I've been shuddering at those words The Stack - because I can never quite get a grasp around what The Stack is and how it plays into the execution of a program or thread. So I understand it is an area of memory set up in RAM, but that's about it.

My questions are as follows:

  • What is the function of the stack?
  • How does "The Stack" play into the execution of a thread in the CPU, with respect to the Program Counter, Registers, and Stack Pointer?
  • How are things added to the stack and how are they removed from it?
  • Furthermore, even if you don't know about Pintos, what does it mean to "set up the stack" when building support for user programs in an operating system?
Héctor M.
  • 2,302
  • 4
  • 17
  • 35

1 Answers1

0

A stack is just memory. The only thing that makes memory a stack is that the process accesses it Last In First Out.

What is the function of the stack?

The function of a stack in a computer is to support function calls. Function calls mirror the operation of a stack. Calling a function pushes it. Exiting a function pops.

How does "The Stack" play into the execution of a thread in the CPU, with respect to the Program Counter, Registers, and Stack Pointer?

From the CPU's perspective a thread is a process. Operating systems trick the CPU by having multiple processes share the same address space. Thus the process becomes a thread.

The program counter and stack pointer are registers. On most processors there are instructions that manipulate the stack pointer register. For example, a function call instruction will push the program counter on to the stack by decrementing the stack pointer and storing the program counter at the new location the referenced by the stack pointer.

How are things added to the stack and how are they removed from it?

Stack memory is allocated by decrementing the stack pointer. Something like:

   SUB   #32, SP

will allocate 32 bytes on the stack and

  ADD  #32, SP

will free that memory. The advantage of the stack is that it is very fast for allocating memory.

In addition, as mentioned above, some instructions are likely to manipulate the stack.

Furthermore, even if you don't know about Pintos, what does it mean to "set up the stack" when building support for user programs in an operating system?

To set up a stack you have to:

  1. Allocate memory for the stack.
  2. You might also want to allocate guard memory that is protected on either side of the stack to detect overflows and underflows.
  3. You assign move the address of the top of the stack into the state pointer register.

As I said before, a stack is just memory. A program can easily allocate its own memory and move its address into the stack pointer to create a new stack.

user3344003
  • 20,574
  • 3
  • 26
  • 62
  • Stack isn't there to support function calls. It is just a convention. OP doesn't understand why memory is needed for a program to run, this requires reading a book not asking on SO. – Tony Tannous Apr 13 '18 at 07:39
  • This is a good answer, but I still have a few questions. (On a side note, I'm not sure where the previous comment is coming from, I know why memory is needed for a program...) So is the stack only needed for handling function calls? I was under the impression that it had something to do with the actual loading of instructions in the CPU. I guess what I'm meaning to ask is, I get that it handles function calls, but what exactly in the program is it that is strictly held in the stack? What is something that would most definitely be pushed onto the stack during the lifetime of a process? –  Apr 13 '18 at 19:03
  • The stack also serves to handle interrupts. When you call a function, the PC value is pushed on the stack so when you return, it is popped off back into the PC so that you resume after the function call. The stack is also used for local variables in many programming language implementations. – user3344003 Apr 14 '18 at 04:56