What is the role of stack in a microprocessor?

Jay picture Jay · Jul 11, 2009 · Viewed 40k times · Source

What is the role of stack in a microprocessor?

Answer

nik picture nik · Jul 11, 2009

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,

  1. 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.