This is really a theoretical question. My example will use C but the language isn’t that important.
Let’s say I create lots and lots of variables on the stack
int x0 = 0;
int x1 = 1;
.
.
.
int x100 = 100;
Now I want to call x0. Since the stack is LIFO, where are x100,…,x1 stored, temporarily, while x0 is being fetched? By this I mean, won’t they have to be placed on registers? And if so, there simply aren’t enough registers. Using the standard analogy of cafeteria trays, If I’m trying to get to the bottom tray, I need lots of people to hold onto the other trays while I get it, unless there are three stacks and I can do some “tower of hanoi” solution…
Obviously this question shows my ignorance of the stack and how it works. Thanks in advance.
The top of the stack is simply a memory address so you can access it with an offset. That said, this would make your code extremely fragile.
The whole point of LIFO, is the last value you put on there is the next one you need, so in your example you’d either push them in reverse order, or you’d define a structure with them all in there and push it’s address on to the stack.