views:

1233

answers:

9

What is the role of stack in a microprocessor?

A: 

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.

klez
He didn't say the stack was in the microprocessor.
Nosredna
Well, he did say "the stack in a microprocessor," so I suppose it's a useful clarification.
Chuck
The stack can have a _role_ in a microprocessor without _being_ in the microprocessor. For instance, a7 is the stack pointer in a 68000, so you can certainly say the stack plays a role in the microprocessor without the stack memory physically residing in the microprocessor.
Nosredna
A: 

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.

arsane
Not a terminology for a processor? PUSH / POP assembly instructions anyone? :P
Thorarin
@Thorarin, Cisc processor like Intel provides push/pop assembly instruction to explicitly support stack operation, also has sp register; but other risc processor like MIPS does not provide push/pop instruction for stack operation, no sp register specifiied. Though they all can be used for stack implementation, but I really think stack is a language concept, like c, like java. You can write assembly for processor without stck needed at all.
arsane
@arsane: What I see when I look at MIPS is stack, stack, stack. http://en.wikibooks.org/wiki/MIPS_Assembly/Subroutines
Nosredna
@Nosredna, that's a kind of programming view, sure, you can use the assembly to implement stack, but it does not mean stack is for processor. Stack is for language's routine call. Processor itself just take charge of jump/branch/excution/load/save thing.
arsane
A: 

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.

fortran
+4  A: 

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
  2. return value
  3. parameters to called function
  4. local variables in the called function
  5. 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.

nik
A: 

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

Andriyev
+1  A: 

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:

  1. PUSH - put data onto the stack
  2. POP (or PULL) - "remove" data from the stack
  3. CALL - jump to a subroutine and put the return address on the stack
  4. RETURN - return from a subroutine by loading the program counter with the stack top
  5. 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.

George Phillips
A: 

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.

A: 

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.

CapBBeard
A: 

strong text

BlockquoteI WANNA KNOW IF THR IS A ROLE OF STACK IN HANDLING INTERRUOTS!!!!

HELP... I AM IN DESPERATE NEED OF THIS ANSWER

Indra C