What is the role of stack in a microprocessor?
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.
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.
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.
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,
- Return address
- return value
- parameters to called function
- local variables in the called function
- processor registers that will be reused in the called function
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.
Hi
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
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:
- PUSH - put data onto the stack
- POP (or PULL) - "remove" data from the stack
- CALL - jump to a subroutine and put the return address on the stack
- RETURN - return from a subroutine by loading the program counter with the stack top
- INT (or SWI) - software interrupt; a specialized CALL
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.
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.
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.
strong text
BlockquoteI WANNA KNOW IF THR IS A ROLE OF STACK IN HANDLING INTERRUOTS!!!!
HELP... I AM IN DESPERATE NEED OF THIS ANSWER