What is the role of the stack in the microprocessor?

What is the role of the stack in the microprocessor?

+4
source share
10 answers

The stack is used mainly during function calls, but depending on the language and programming level, it can be used to temporarily store processor register data or other variables.

In addition, the stack can also be used for short-term large-scale data storage using recursive functions that store partial data on the stack and call themselves again.

shared stack usage for

  • Return address
    • return value
    • parameters of the called function
    • local variables in the called function
    • processor registers to be reused in the called function

And, yes, the stack is also used for exploits .
Its nature of transferring the return address to where the called function returns, combined with the weakness of checking the boundaries of an array in C , provides a very good way to cause a buffer overflow on the stack of a vulnerable (unauthorized) program.

+6
source

At the lowest level, the stack is the place where certain instructions store or retrieve data and where the data is stored when an interrupt occurs. Microprocessors are different, but there are 5 general types of instructions for the stack:

  • PUSH - push data onto the stack
  • POP (or PULL) - "delete" data from the stack
  • CALL - go to the subroutine and put the return address on the stack
  • RETURN - return from the subroutine by loading the program counter with the top stack
  • INT (or SWI) - interruption of software; specialized call

When a processor interrupt occurs (due to an external device), the CPU saves the current program counter and (usually) the flag register on the stack and proceeds to the processing routine. This allows the processing routine to handle the interrupt and return to what the processor was doing while maintaining its current state.

While the microprocessor only has one stack at a time, the operating system can make it look as if there were several stacks. At least one for the OS, one for each process and one for each thread. In fact, the threads themselves can implement many stacks.

At a higher level, any language used to implement the stream often uses the stack for its own purposes to store function call parameters, local variables and function call return values ​​(broadly speaking here - consult with your languages) low-level documentation for specific details).

Thus, we conclude the stack explanation from the bottom up.

+4
source

In ancient times, subroutine calls were processed using RAM memory with each subroutine to indicate where it was called from. To call a subroutine, you need to 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 template can be optimized if the caller places the return address in the register and, after completing the procedure, saves it in the "return" field when writing. This template worked, but it had several problems. Not only did this lose memory altogether - it also had no means of dealing with recursive or repeated code. Adding a stack made it possible to simplify the code if the caller simply said “save the return address somewhere suitable” without violating any earlier ones, and for the called function simply say “return to the most recent caller who hasn’t returned yet”. This allowed us to develop a re-entry code and meant that we only needed to store enough return addresses to handle the deepest nested chains of function calls that had ever taken place.

+2
source

It depends on the microprocessor. As a rule, its role is to preserve the parameters of local variables and functions.

And actually it’s not in the microprocessor, it is in the central memory.

+1
source

The stack is used to store and retrieve the return address during function calls. It can be used during nested function calls or recursive function calls. It is also used to pass arguments to a function .

On the microprocessor, it is also used to store the contents of the status register until the context switches.

amuses

+1
source

Some microprocessors have stack registers to increase efficiency; see the SPARC wikipedia article ; others have a microstack for micro-rotes ... This is a very broad term, actually.

0
source

http://www.hobbyprojects.com/microprocessor_systems/images/stack.gif

The stack is a temporary data store.

The CPU can push important data onto the stack while it is processing other data.

When it completes this task, it removes the stored data from the stack.

It's like a bunch of plates. The bottom plate is the first bit of data that has been pushed onto the stack. The top plate is the last data to press. The top plate extends first, and the bottom plate is the last data to be pulled out. This is the LAST IN, FIRST OUT stack.

In diagrams X, it will be pressed first, then Y, and finally A. The CPU leaves to process other data. Upon completion of this task, it returns to pull out the stored data. First, A stretches, then Y, and finally X.

Data entry instructions are PHAs. Only data in the battery can be pushed onto the stack. Other data may be pressed if it is first transferred to the battery.

Instructions for retrieving data from the stack - PLA. Stack data is transferred to the drive.

Stack 6502 consists of 256 bytes and occupies page 1, addresses from 256 to 511.

0
source

To add some of these answers, some lower-level chips, such as the PIC line, have a hardware column, which means that it cannot be dynamically allocated because it is located in the hardware.

The consequence of this is that you can make so many function calls before you finish the stacks; this, of course, also applies to software, but often the hardware stack can be very limited and may require you to rethink your program in order to “smooth out” your function calls.

0
source

The stack is an implementation of the LIFO buffer (Last In - First Out). FIFO (First In-First Out) is also known as a queue. But back to LIFO.

The stacks in the x86 architecture allow software developers to dispense with such strange things as return address registers and interrupt return address registers that reside in RISC processors. Everything can be on the stack, which means that there is a single standardized and unified method for handling calls / returns, parameters / local variables and interrupts / interrupts. Using the method on separate stacks simplifies multithreading.

RISC, by contrast, uses a stack-like buffer, although they contain significant pieces of related information elsewhere. RACC stacks can be faster (not sure), but they are definitely harder to understand than x86.

0
source

In fact, the stack is not a terminology for the processor; it is used to call a routine program. A routine can use the stack to retrieve parameters and store local variables, as well as call other routines.

-5
source

All Articles