What is the role of stack in a microprocessor?

2019-01-18 15:03发布

What is the role of stack in a microprocessor?

10条回答
神经病院院长
2楼-- · 2019-01-18 15:39

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.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-18 15:40

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.

查看更多
冷血范
4楼-- · 2019-01-18 15:41

In the early days of computing, subroutine calls were handled by having a word of memory RAM with each subroutine to indicate where it was called from. To call a subroutine, one would 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 pattern might be optimized by having the caller place the return address into a register, and having the routine store it to the "return" spot on entry. This pattern worked, but it had a few problems. Not only did it generally waste memory--it also had no means of dealing with recursive or re-entrant code. Adding a stack made it possible to simplify the code by having the caller simply say "store the return address someplace suitable", without disturbing any earlier ones, and for the called function to simply say "return to the most recent caller that hasn't been returned to yet". This allowed for the development of re-entrant code, and meant that it was only necessary to store enough return addresses to handle the deepest nested chain of function calls that would ever actually occur.

查看更多
Explosion°爆炸
5楼-- · 2019-01-18 15:43

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.

查看更多
登录 后发表回答