Reputation:
What is the role of stack in a microprocessor?
Upvotes: 6
Views: 44211
Reputation: 81307
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.
Upvotes: 2
Reputation: 3274
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.
Upvotes: 0
Reputation: 998
Just to add to some of these answers, some lower-end micros such as the PIC line have a hardware callstack, which means it can not be dynamically allocated as it is in hardware.
The implications of this are that you can only go so many function calls deep before you run out of stack; this is true of software also of course, but often the hardware based stack can be very limiting, and can require you to rethink your program in order to 'flatten' out your function calls.
Upvotes: 0
Reputation:
http://www.hobbyprojects.com/microprocessor_systems/images/stack.gif
The stack is a temporary store for data.
The CPU may PUSH important data onto the stack, while it is processing other data.
When it finishes that task, it PULLS the saved data off the stack.
Its like a pile of plates. The bottom plate is the first bit of data that was pushed onto the stack. The top plate is the last data to be pushed. The top plate is pulled first and the bottom plate is the last data to be pulled. It is a LAST IN, FIRST OUT stack.
In the diagrams, X is the first to be pushed, then Y and lastly A. The CPU goes away to process other data. Upon completion of that task it returns to pull the saved data. First A is pulled, then Y and lastly X.
The instruction for pushing data is PHA. Only data in the accumulator can be pushed onto the stack. Other data can be pushed if it is transferred to the accumulator first.
The instruction for pulling data from the stack is PLA. Data on the stack is transferred to the accumulator.
The 6502 stack consists of 256 bytes and occupies page 1, addresses 256 to 511.
Upvotes: 0
Reputation: 4654
At the lowest level the stack is the place where certain instructions store or retrieve data and where data is stored when an interrupt occurs. Microprocesors vary, but there are 5 general types of stack-specific instructions:
When a processor interrupt occurs (due to an external device), the CPU will save the current program counter and (usually) the flags register on the stack and jump to the handling subroutine. This allows the handling subroutine to process the interrupt and return to whatever the CPU was doing with its current state preserved.
While a microprocessor has only one stack active at a time, the operating system can make it appear as if there are multiple stacks. At least one for the OS, one for each process and one for each thread. In fact, the threads themselves may implement multiple stacks.
At a higher level, whatever language is used to implement a thread will often use the stack for its own purposes to store functional call parameters, local variables and function call return values (speaking in broad strokes here -- consult your languages' low-level documentation for specific detail).
Thus concludes my bottom-up explanation of the stack.
Upvotes: 4
Reputation: 13468
Stack is used largely during a function call but depending on the language and level of programming it may be used to temporarily store processor register data or other variables.
Further, the stack may also be used for short-term large-scale storage of data when using recursive functions that store partial data in the stack and call themselves again.
The generic use of stack is for,
And, yes, stack is also used for exploits.
Its nature of carrying the return-address to where a called function returns back,
coupled with the weakness of array-bounds checks in the C
language, gives a very
nice way to cause buffer overflows in the stack of a vulnerable (unsafely written) program.
Upvotes: 8
Reputation: 30442
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
Upvotes: 1
Reputation: 76157
Some microprocessors have stack registers to improve efficiency, take a look at the SPARC article in wikipedia; others have a microstack for the microroutines... It's a very wide term, in fact.
Upvotes: 0
Reputation: 46163
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.
Upvotes: -5
Reputation: 27149
It depends on the microprocessor. Generally its role is keeping local variables and functions' parameters.
And actually it's not in the microprocessor, it's in the central memory.
Upvotes: 1