Last Life Hump
Last Life Hump

Reputation: 39

When you push & pop data on & off the stack, what exactly are you moving?

This regards basic computer architectures, i.e. ones that have stacks, none in particular.

With the FILO (first-in, last-out) approach, like with Intel x86, you use push and pop, but what signifies or determines what has been pushed?

For example, I want to push a "variable", let's say ... we know that everything in the chip is just two volt binary bits, logic-driven circuits, gates, transistors, etc. When I push, say, a byte containing the address signified in binary like this: 1000 0101, 133, or 85 (in hexadeciaml), a value is moved to the stack ... here are some questions:

1.Where exactly does the stack reside in the chip?

2.How does the configuration of the stack on the chip enable/disable blocks of data going in and out of it?

3.In a multitasking environment there's usually multiple stacks. How or where do the extra stacks come from?

I know this is likely off-topic, but I want a better vision of the stack itself from the hardware-side. No, I do not want to see pictures of plates, I get the idea, but I want to envision how the data goes to it, where exactly the stack is, what it is in the chip, and how it's configured.

Upvotes: 5

Views: 3367

Answers (1)

Filipe Gonçalves
Filipe Gonçalves

Reputation: 21213

Your question is very interesting, and not subjective at all. I don't think it will be closed.

First of all, I'd rather refer to a stack as using a LIFO approach - Last In First Out. FILO is equivalent, but it's not very usual, in fact, I think I never heard of it.

  1. Where exactly does the stack reside in the chip?

There is no such thing as "the chip" where a stack is stored. Stack is stored in memory, when you issue a push instruction, you are pushing that value to memory. The CPU does not hold a stack - it has some registers, but push and pop always use memory. And where in memory does the stack sit? Well, there's a register, the stack pointer register, that indicates the memory address of the top of the stack. The assembler can initialize this address for you, or you can do it manually (it depends on the environment you're working on).

If you really want to go deep down on this topic, think about how a CPU works: it is no more than a bunch of logical circuits with a control unit that decodes an instruction and activates or deactivates the necessary bits and ports. When you write push in an assembly source file, the assembler translates that into a bunch of bits that represent an opcode and the arguments for the instruction.

Later on, during execution, the CPU's control unit will decode this information. It looks at the opcode, and it says "Hey, this is a push instruction. Hey, you, memory, prepare for write.". It will eventually access the bus that interconnects CPU and memory, send binary data through that bus, activate a WRITE flag, and indicate the value stored in the stack pointer register as the target address. Deep down, all it does is it activates a series of bus and bits to write to a memory position. The binary data that is written comes from whatever argument you passed to push. If it's a register, the CPU's register file is accessed to read the value before it is passed to the memory bus.

After writing to memory, the stack pointer is simply incremented to the next memory position. pop is the opposite: it decrements the stack pointer and then reads whatever is stored at that position (this can be different, e.g. in push you can write and then increment, or you can increment and then write, you just have to be consistent in both push and pop).

You can implement a stack in assembly relatively quickly, and I advice you to do so - it's a great exercise that will give you useful knowledge.

2.How does the configuration of the stack on the chip enable/disable blocks of data going in and out of it?

The stack is nothing more than a special memory location that you can keep track from using the stack pointer register. Incrementing and decrementing this register will let you read arbitrary stack positions. If you change this register to a completely different memory position, you now have another stack.

push and pop is not magic, it's just reading and writing to memory, and incrementing or decrementing the stack pointer. Simple.

3.In a multitasking environment there's usually multiple stacks. How or where do the extra stacks come from?

Again, everything's in memory. In a multitasking environment, every process has its own context. Before a process is given CPU time, the operating system kernel code will recover a process context, so that the process itself doesn't even realize what happened. A process' context includes the state of the registers - which also includes the stack pointer. So, when a process is brought back to CPU, its stack pointer register will point to HIS stack - his position in memory that is being used as a stack, because the kernel had this information stored and put it back there for the process to use as if nothing had happened in the meantime.

Always remember, at a different layer, that memory positions are interposed by L1, L2 and L3 caches to speed up common access.

And that's how the bits flow...

Upvotes: 3

Related Questions