An interesting explanation of the concept of microcontroller stack

Publisher:诗意世界Latest update time:2016-01-18 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Because the MCU has CPU, memory, IO, etc., it looks like a relatively small computer (to be more humane to match the following text). Therefore, if you can integrate your previous understanding of computers, which may only be intuitive, into the understanding of MCU, it may be of great help to you in learning the concept of MCU, at least for me.
I think among the many concepts about MCU that make you dizzy, confused, or even want to hit the wall, "stack" may be the most hateful one, because even if you understand this word from the perspective of Chinese, it will make you dizzy. In fact, I couldn't figure out who came up with this idea at first, but don't worry, we won't discuss the problem of this word here (this word is actually very well used. "Heap" and "stack" have their own meanings, which accurately summarize the functions of this area. If you are interested, you can Baidu it). Here I will make a more interesting analogy to bypass those concepts that make you want to hit the wall, and give you a deep understanding of the concept of "stack" intuitively.
You should basically know that there are storage areas and CPUs in MCUs. If you don't know, then I just told you, please remember. Now, please think of the CPU in the single chip as a person (you can think of him as the classmate in your dormitory who quarrels with you every day, you will find it very interesting in a while), here we call him Brother C, but this person is different from ordinary people, and has some characteristics, which we will slowly explain in a while. Now I want to tell you about the first characteristic of this person: his memory is very poor. Next, please imagine the storage area as small boxes arranged one by one. The functions of these boxes can be roughly divided into two categories: 1. Save the notes with your commands, such as the note in a box that says: Go wash my socks! ; 2. Save some of your things, such as your pair of smelly socks that are polluting the air in the dormitory. Because Brother C is a person with a poor memory, these boxes have their own numbers to facilitate his search.
So, now, we can explain how the microcontroller works. First, you need to put all the commands and things that need to be processed into those small boxes, such as the pair of socks to be washed and the note mentioned earlier. At this time, you should find another characteristic of Brother C: stupid - he will only do what you clearly tell him to do. In other words, if you don't write "go wash my socks!" on the note, Brother C is very likely to look at your socks indifferently until he faints from the fumes. Of course, it is more likely that he can't find your socks at all... Well, after you write down what you want to do and how to do it in the box, the following task is handed over to Brother C. Brother C is really principled in doing things. He will open the boxes one by one in the order you give or - if you don't give it, according to the number on the box in order from small to large, read the commands inside, and process the corresponding events until everything is executed, and he will rest. Please remember this simple and interesting process, because in fact, the microcontroller works like this. Of course, many details are ignored here, but this is enough for you to intuitively understand the concept of microcontrollers.
Next, I will start to explain the concept of stack. After thinking about it, I still feel that if I use the word "stack" directly in the text, it is really not in line with the style of this article. Considering that the "stack" is actually a storage area (you have to remember that the stack is not a special area like a dedicated register, it is specified by you in the general RAM area.), according to this article, it is a box. So, now we change the name of "stack" to "memory box". You can feel that the role of "stack" is closely related to memory, but you don't have to worry about the origin of this name here. I will explain it below.
Now, please pay attention, I will start to explain the "memory box", that is, the "stack". Generally speaking, the role of the "memory box" is to save the current task (including the box number and the contents of the box) when Brother C is halfway through executing a task and suddenly has something more urgent to execute. You must be confused by this. Actually, to put it simply, when Brother C was halfway through washing socks, he suddenly received your order to open another box (the note in that box might say "change my diaper") and execute the order inside. Because Brother C's memory is very poor, he can't remember which box to return to after completing that urgent task to continue executing the order of "washing socks". At this time, he has to save the things on hand in the "memory box". The things to be saved are: 1. The number of the box with the note and socks (note that there are actually two items here); 2. The pair of socks. In this way, when he completes the urgent task, he will go to the memory box and find two notes and a pair of socks from it (at this time, Brother C still hasn't remembered that he wants to wash socks, he must go to the note with the order to wash socks). He knows which box he should go to to wash socks according to the information on the two notes, and continues to complete the task of washing socks there. You may find that there is an important loophole in this explanation. That is, how did Brother C know which "memory box" the box number storing the original mission information was stored in after he completed the emergency mission? Don't worry, I will explain it below.
In essence, the "memory box" is no different from an ordinary box. They are all storage units in the microcontroller. The best evidence to prove this is that the stack needs to be specified by you, that is, you have to specify some boxes as "memory boxes" in advance. Next, I will explain how to specify the "memory box". In fact, this process is very simple. There is an SP pointer (81H) in the dedicated register of the microcontroller. This pointer records the address of the beginning of the stack. To explain it in the words that fit this article, there is a pocket on Brother C's clothes (that is, the SP pointer), and the "magic note" in this pocket records the number of the first "memory box", and the process of specifying a "memory box" is that you write the number of a box on this "magic note" (as the number of the first "memory box"), and this note will automatically add 1 or subtract 1 to the number on the note, so a box in a currently uncertain area has the possibility of becoming a "memory box". Note that the size of the stack cannot be specified, which is why the word "growth" is used to describe the stack.
Now, the concept of the stack has been basically introduced, but I know that you may still be dizzy, even more dizzy than before, because the process just described is separate and logically not sequential. Below, I will explain it in sequence, and I believe you will understand it immediately.
The protagonist is still the silly but hardworking Brother C, who opens the boxes one by one and performs the tasks you specify according to the instructions on the note inside. And you, in order to prevent him from making a fool of himself when performing a complex task, designated a box as a "memory box" and wrote the location of this "memory box" on a "magic note" and put it in Brother C's pocket. Now, Brother C is washing your socks. At this time, he suddenly receives your order to change your diaper. Brother C knows that he is stupid, so he automatically takes out the note in his pocket, finds the first "memory box", and then takes out a blank note, writes the number of the box containing the note "wash my socks" on it and puts it in the "memory box". Then, he puts the "magic note" back into his pocket. When this task is completed, the "magic note" will automatically add 1 to the number written on it, that is, write the number of a new, empty "memory box" on it. After that, he will put the number of the box containing the socks and the socks themselves into different memory boxes according to the process just now (now there are three boxes that have become "memory boxes", the stack has grown, and the red underlined fonts are the contents of these three boxes. Note that there is a sequence). Then, he went to change your diaper...
Now, the diaper has been changed. As expected, Brother C has completely forgotten that he was going to wash your socks. However, he remembered one thing, which was to look at the note in his pocket. So he took out the note in his pocket. Of course, it was the number of a "memory box". He found the first "memory box" according to the number (according to the order of the previous paragraph, it should be the third "memory box"), which should contain a pair of your socks, so he took your socks. However, he still didn't know what to do, so he took out the "magic note" again. At this time, the number on the note had automatically decreased by 1, so he found a new "memory box". The note inside recorded the number of the box where the socks were originally placed, so he put the socks in that box. Well, you can imagine that Brother C still doesn't know what to do with the socks. He patiently took out the "magic note" again. This time, according to the number on it, he found a note with the number of a box written on it. Brother C found the box according to the number and found a note in the box saying "Wash my socks!"... At this point, Brother C returned to his original task - washing socks.
Now, I hope you have understood that the stack is actually a set of storage units you specify. These storage units are designated to store only some special information, such as addresses (to protect breakpoints) or some data (to protect the scene). If you must say that there is something special about this storage area, it is: 1. The contents of these storage units are some related parameters when the CPU is interrupted in the middle of executing a task; 2. The addresses of these storage units are recorded in a place called the stack pointer, which is the note in Brother C's pocket!
Keywords:MCU Reference address:An interesting explanation of the concept of microcontroller stack

Previous article:About timer interrupts 1 and 3 in MCU
Next article:Control Method of 28BYJ48 Stepper Motor

Recommended ReadingLatest update time:2024-11-16 22:24

RTOS-AVRX application of AVR microcontroller
Introduction   With the development of technology, the design and application of embedded systems have had a great impact on people's lives and will gradually change people's future lifestyles. Developing applications on a specific operating system allows developers to ignore many underlying hardware details, making
[Microcontroller]
Design and implementation of a six-degree-of-freedom automatic tracking robot based on single-chip microcomputer control
One of the characteristics of the development of contemporary science and technology is the combination of mechanical technology, electronic technology and information technology. Robots are one of the products of this combination. Modern robots are all developed from machinery. The difference from traditional machine
[Microcontroller]
Design and implementation of a six-degree-of-freedom automatic tracking robot based on single-chip microcomputer control
Design of Intelligent Temperature Controller for Dry-Type Transformer Based on ATmega16 Single Chip Microcomputer
1 Working Principle The temperature controller consists of three parts: temperature monitoring, signal processing, and output control. The system block diagram is shown in Figure 1. It obtains the winding temperature value through three platinum resistance sensors embedded in the three-phase winding of th
[Microcontroller]
Design of Intelligent Temperature Controller for Dry-Type Transformer Based on ATmega16 Single Chip Microcomputer
Design of intracranial hemorrhage detection equipment based on MSP430 microcontroller
1 Introduction In China, there are many patients with traumatic brain injury who are in urgent need of emergency treatment, but a considerable number of patients with intracranial hemorrhage are delayed in rescue and treatment due to the failure to make timely diagnosis. As a result, brain hematoma or brain
[Industrial Control]
Design of intracranial hemorrhage detection equipment based on MSP430 microcontroller
Summary of LCD1602 based on 51 single chip microcomputer to realize minute and second timing
Completion requirements 1. Realize the timing of minutes and seconds. 2. Use buttons to control the adjustment of minutes and seconds (two buttons control the increase of minutes and seconds respectively). 3. Realize the function of telling time in whole and minute (buzzer sounds). 4. Drawing and welding of PCB board
[Microcontroller]
Summary of LCD1602 based on 51 single chip microcomputer to realize minute and second timing
Transplantation of μC/OS-Ⅱ on ARM Series MCU S3C44B0x
At present, embedded systems have been more and more widely used in various fields such as industrial control, household appliances, mobile communications, PDAs, etc. As users have higher and higher requirements for the performance of embedded products, program design has become more and more complicated, which require
[Microcontroller]
Transplantation of μC/OS-Ⅱ on ARM Series MCU S3C44B0x
Principle and Application of 80C196MC Single Chip Microcomputer Waveform Generator
These integrated circuits include HFE4752, SLE4520, MA8X8/SA8X8, SAXXXX, etc. Most of them need to be connected to a single-chip microcomputer to complete the SPWM control function, which is still not simple enough for inverter systems with higher requirements. The 16-bit single-chip microcomputer 8XC196MC launched
[Microcontroller]
Principle and Application of 80C196MC Single Chip Microcomputer Waveform Generator
Measurement of 51 microcontroller edge triggered interrupt response time
The MCS51 microcontroller series is an 8-bit microcontroller. It is a product launched by Intel in 1980 following the successful design of the MCS48 series. Because the MCS51 series has strong on-chip functions and command systems, it has made a leap forward in the application of microcontrollers. This series of produ
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号