What is the role of stack in a microprocessor?
相关问题
- How do I write a function to compare and rank many
- Why does Haskell have non-strict functions (semant
- Endianness, “Most Significant”, and “Least Signifi
- How to set contents of a file that don't start
- 16 bit logic/computer simulation in Swift
相关文章
- Get specific values from JSON column in Presto
- Create a java program to solve quadratic equations
- What are fast LEA and slow LEA unit in the microar
- Minimum repetitions in merged array of characters
- Why is a monitor implemented in terms of semaphore
- Understanding regex string matching using Dynamic
- Real-world use of binding objects in ruby
- NP-Complete VS NP-Hard
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.
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
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.
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.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.