What is the role of stack in a microprocessor?
In the early days of computing, subroutine calls were handled by having a word of memory RAM with each subroutine to indicate where it was called from. To call a subroutine, one would do something like:
load foo_return with #LABEL_123 goto foo #LABEL_123: ...code to execute after return from foo foo: ... do stuff goto foo_return
This pattern might be optimized by having the caller place the return address into a register, and having the routine store it to the "return" spot on entry. This pattern worked, but it had a few problems. Not only did it generally waste memory--it also had no means of dealing with recursive or re-entrant code. Adding a stack made it possible to simplify the code by having the caller simply say "store the return address someplace suitable", without disturbing any earlier ones, and for the called function to simply say "return to the most recent caller that hasn't been returned to yet". This allowed for the development of re-entrant code, and meant that it was only necessary to store enough return addresses to handle the deepest nested chain of function calls that would ever actually occur.
Stack is used to store and retrieve return addresses during function calls. Its put to good use during nested function calls or recursive function calls. It is also used to transfer arguments to a function.
On a microprocessor it is also used to store the status register contents before a context switch.
cheers
A stack is an implementation of a LIFO (Last In - First Out) buffer. The FIFO (First In - First Out) is also known as a queue. But back to the LIFO.
Stacks in the x86 architecture allow software designers to dispense with such strange stuff as return address registers and interrupt return address registers that are found in RISC processors. Everything can reside on the stack which means that there is a single standardized and unified method of handling calls/returns, parameters/local variables and interrupts/interrupt returns. The use of the method on separate stacks simplifies implementation of multi-threading.
RISC, in contrast, uses a stack-like buffer though they keep significatnt parts of the related info elsewhere. RISC "stacks" may be faster (not sure) but they are definitely more difficult to understand than those of the x86.
Actually stack is not a teminology for processor, it is used for language's routine call. A routine can use stack to get parameters and save local variables, also call other routines.