Detailed analysis of the execution flow of 51 single-chip program

The process of executing the program by the microcontroller, focusing on the hardware process

In order to deepen the beginner's understanding of the 51 single-chip instructions, the process of executing the instructions is now detailed here, I hope to be instructive to you! The process of executing a program by a microcontroller is actually the process of executing the program we have programmed. That is, the process of instruction by instruction. Each step of the computer can be divided into three phases. That is, the instruction-----analysis instruction-----execution instruction. The task of fetching instructions is to read the current instruction from the program memory according to the value in the program counter PC and send it to the instruction register. The task of analyzing the instruction phase is to take out the instruction opcode in the instruction register, decode it, and analyze the nature of the instruction. If the instruction requires an operand, look for the operand address.

The process of the computer executing the program is actually repeating the above operation process one by one, until the stop instruction can be cycled to wait for the instruction. When a general computer is working, the program and data are first sent to the memory through the input interface circuit and the data bus through an external device, and then taken out one by one. However, the programs in the microcontroller are generally fixed in the on-chip or off-chip program memory by the writer in advance. Therefore, the instruction can be executed upon startup. Below we will give an example to illustrate the execution of the instruction: At startup, the program calculator PC becomes 0000H. Then the microcontroller automatically enters the execution program process under the action of the sequential circuit. The execution process is actually a loop process of fetching instructions (taking out the stage of instructions stored in memory beforehand) and executing instructions (analysing and executing instructions). For example, the execution instruction: MOV A, #0E0H, the machine code is "74H E0H", the function of the instruction is to send the operand E0H to the accumulator, the 74H unit has been stored in the 0000H unit, and the E0H has been stored in the 0001H unit. When the microcontroller starts running, it first enters the fetching phase, in the order:

1. The contents of the program counter (in this case, 0000H) are sent to the address register. 2. The contents of the program counter are automatically incremented by 1 (becomes 0001H). 3. The contents of the address register (0000H) are sent to the memory through the internal address bus. The address decoding in the memory is followed by the cell with the address 0000H being selected; 4. The CPU makes the read control line valid; 5. The content of the selected memory cell (in this case, 74H) is sent to the internal data bus under the control of the read command. Above, because it is the fetch stage, the content is sent to the instruction register via the data bus. At this point, the instruction phase is completed and enters the stage of decoding analysis and execution instructions.

Since the content entered into the instruction register is 74H (opcode), after decoding by the decoder, the microcontroller knows that the instruction is to send a number to the A accumulator, and the number is the next one in the code. Storage unit. Therefore, to execute the instruction, the data (E0H) must be taken out of the memory and sent to the CPU, that is, the second byte is also taken in the memory.

The process is very similar to the fetching phase, except that the PC is already 0001H. The instruction decoder, in conjunction with the timing component, generates a micro-operation series of 74H opcodes that cause the digital E0H to be fetched from the 0001H unit. Because the instruction is required to send the fetched number to the A accumulator, the fetched number enters the A accumulator via the internal data bus instead of entering the instruction register. At this point, the execution of an instruction is completed.

PC = "0002H" in the single-chip microcomputer, the PC automatically increments 1 every time the CPU fetches or retrieves the memory, and the MCU enters the next fetching stage. This process is repeated until a pause command is received or the loop waits for a command pause. The CPU executes the instructions one by one to complete all the specified functions.

Detailed analysis of the execution flow of 51 single-chip program

The MCU is something that has no operating system. The code written in keil is bare-metal code, that is, the code you write is similar to the operating system. In-depth writing of bare-metal code helps to understand the characteristics of the hardware.

If the hardware is not fixed, the other processes are code. Suddenly thought of exploring the execution process of 51 single-chip microcomputer. This idea originated from the fact that the main function in each of the 51 programs was originally found to hang a while(1); statement. Why add a while loop to let the program stay in the main function. What is the effect of removing the while(1); statement?

Write a very simple program to try.

Execute the above procedure, the water lamp controlled by the P1 port flashes. The program finally enters while(1); it is entangled, this is a good explanation.

Now mask the while(1); statement. I thought that the program could not be executed correctly, because the main function was exited, just as Render needed a loop to implement (although the program that just flashed was not in the loop, I couldn't help but create this illusion). The result of the program execution is: the water light flashes constantly!

Guess and action after seeing this phenomenon ^-^:

(1) This board is broken! (After running a C language file without an infinite loop with an operating system such as linux character interface, it will return to the linux shell program). Hurry and change the board and test it again, obviously the same result.

(2) The last (some) statement in the main function will always be executed in the MCU? (Based on the experience of running standard C language files with the OS platform, it has never been thought that the main function is called multiple times or multiple times)

(3) The C language instruction is taken out from the MCU and loaded into the MCU. The MCU automatically generates a main program to execute the main function of the C language cyclically (although it is ridiculous, still think)

(4) Hurry up Google Baidu's execution process of the MCU (although in the Google Baidu when the "51 MCU program execution process" search, did not find the relevant content). Change the simple search term: "51 single-chip main". Then there is a question with the same question as me: Why doesn't the main function contain while(1); after the statement, the program will be executed repeatedly? The key words for the answer include "program runaway, watchdog, reset".

(5) The opportunity of embedded embedded will move out the “51 MCU program execution flow” and tell the teacher about the phenomenon of the program I wrote, including how to verify it.

Teacher's answer: The Keil C51 program automatically loads a file named "STARTUP.A51". After a series of initialization operations in this file, it enters the main function of the C language program entry written by the user. After the main function is executed, After the STARTUP.A51 file, there is a statement that jumps to the main function of the program entry, so it will enter the main function of the C main program again to execute the relevant content.

Then I used the keil software to simulate running the above code:

The program starts running at the first statement of the main function of the program entry. The Disassembly window is the window corresponding to the C language code and the assembly code. The front is the address, followed by the assembly statement corresponding to the C language. The window below is the location of the running code for the corresponding file, with a yellow arrow pointing to the code currently being executed. Then click the single-step run toolbar to guide the jump out of the main function, the program jumps to the following code position in STARTUP.A51:

Continue to click on the single step until you enter a loop:

Here is a loop, according to the function of the DJNZ instruction: every time DJNZ RO is executed, IDATALOOP decrements the value of R0 by one. If the value of R0 is not 0, it jumps to the IDATALOOP address. Obviously this is a loop, then what is the value of RO, which is displayed in the following window:

It can be seen that the initial value of r0 is 0x7f, and 0x7f(128) times will be looped here. The meaning of r0 value can be seen here. So where will the program go after this loop? The place where the program runs after skipping this loop is as follows:

Run it once more:

According to the content of Disassembly, this statement will be returned to the main function after execution. Try it out:

Yes!

Therefore, in the 51 single-chip microcomputer, the execution flow of the program is continuous (using the value of r0 as the delay condition, the specific meaning can continue to explore) into the main function to execute the code in the main function.

Why do we terminate the program itself after running C code without dead loop on Linux, etc. This is a different operation process:

(1) C51 microcontroller does not have OS (operating system), the execution of the code seems to be arranged by STARTUP.A51, there is no bigger program to manage how to call the main function.

(2) A platform like Linux comes with an OS. Running a C program is a task for Linux. There are other tasks besides running the C program. When running a C language program, the task is complete. For example, if you run a C program named "hello.c" in the linux shell interface to output "hello world!", the process is as follows:

Compile: gcc hello.c –o hello

Run: ./hello

When running the hello executable, you can call the hello executable as a shell. After the hello is finished running, return the return value and so on to the shell interface. The entire C language file life and death have linux shell program management.

Plastic

Nantong Boxin Electronic Technology Co., Ltd. , https://www.ntbosen.com