What is the role of stack in a microprocessor?

后端 未结 10 890
-上瘾入骨i
-上瘾入骨i 2020-12-10 08:34

What is the role of stack in a microprocessor?

10条回答
  •  星月不相逢
    2020-12-10 08:59

    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.

提交回复
热议问题