I'm reading about 6502 assembly don't understand what the stack is for. I get it that it's a FIFO memory and understand the push/pop thing but what is it for? How is it different (the use) from the registers or the RAM? What sort of data do you put in the stack and why?
-
Consider what happens when you run out of registers. The data needs to go somewhere... – Mysticial Dec 14 '14 at 22:07
-
2Stack is generally LIFO, not FIFO. – nrz Dec 14 '14 at 22:07
-
maybe that can help. http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap – tanaydin Dec 14 '14 at 22:07
3 Answers
The primary reason for a hardware supported stack is to maintain the return address for function calls. Evidence for this argument can be seen in what machine language operations assume a stack exist. Other than operations which treat the stack as a source or destination in much the same way that a simple move operation does, the only instructions that assume the existance of the stack are call/return operations. The call is really just a push and jump. Return is really just a pop and jump indirect. Since it already exists for this purpose, it is also incredibly convenient to use it for local state of the function invocations (parameters and local variables).

- 8,154
- 4
- 36
- 49
-
Specifically with the 6502, look up the instructions JSR (jump to subroutine) and RTS (return from subroutine). They work almost like two JMPs, except that RTS "somehow" knows where to jump, even if multiple JSRs refer to the same subroutine. The stack is the "somehow". – hemflit Dec 15 '14 at 01:13
There is a thing call memory hierarchy, like this:
The stack is basically the RAM, or the Main Memory, like the image... The main difference between all those levels are the cost, space and the speed. At the top you have the most expensive, smallest and fastest storage devices, at the bottom you have the cheapest, biggest and slowest. The memory architecture works in a way that tries to balance those parameters...
The time that takes to pass data from the disk to the register was to long, so the solution was to create another storage device between the two.. and so on, until we reached a limit. That limit can be break, but its to expensive to do it...

- 860
- 9
- 29
-
I'm curious to know if there are any computers with 6502 processor and with more than 1 GB of disk storage... – nrz Dec 15 '14 at 00:05
The stack is for temporary storage, like registers but usually a larger supply. think about function calls, recursive or not, if you were to use a register for the return address of the first call, then what happens when that function calls a function? and that function calls a function? Using fixed registers simply doesnt work, you cannot handle an arbitrary depth to your nesting, cant handle recursion definitely. But it is trivial with a stack, push a return address, push copies of the registers you are going to modify, allocate storage for local variables and then you can use the general purpose registers in your function, clean up when returning from the function and use that return address. Each function performs these tasks and so long as you have ram available you can nest as much as you want or call a function recursively, etc.

- 69,149
- 8
- 89
- 168