How does the microcontroller execute the instruction process? How is the MCU memory allocated?

Detailed explanation of the instruction process of the microcontroller

The process of the single-chip computer executing the program is actually the process of executing the program we have compiled. That is, the instruction-by-instruction process. Each instruction executed by the computer can be divided into three stages. Instantly fetch instructions-----Analyze instructions-----Execute instructions.

How does the microcontroller execute the instruction process? How is the MCU memory allocated?

The task of fetching instructions is to read the current instructions from the program memory according to the value in the program counter PC and send them to the instruction register.

The task of the analysis instruction stage is to decode the instruction opcode in the instruction register and analyze the nature of the instruction. If the instruction requires an operand, look for the address of the operand.

The process of the computer executing the program is actually repeating the above operation process instruction by instruction, until it encounters a shutdown instruction, it can wait for instructions in a loop.

When a general computer is working, the program and data must first be sent to the memory through the input interface circuit and the data bus through an external device, and then taken out and executed one by one. But the program in the single-chip microcomputer is generally solidified in the on-chip or off-chip program memory through the writer in advance. Therefore, commands can be executed as soon as the machine is turned on.

Below we will give an example to illustrate the execution process of the instruction:

When powering on, the program calculator PC changes to 0000H. Then the single-chip microcomputer automatically enters the process of executing the program under the action of the sequential circuit. The execution process is actually a cyclic process of fetching instructions (fetching the instruction phase stored in the memory in advance) and executing instructions (analyzing and executing instructions).

For example, the execution instruction: MOV A, #0E0H, its machine code is "74H E0H", the function of this instruction is to send the operand E0H to the accumulator, 74H has been stored in unit 0000H, and E0H has been stored in unit 0001H. When the microcontroller starts to run, it first enters the instruction fetching phase, and the sequence is:

1 The content of the program counter (0000H at this time) is sent to the address register;

2 The content of the program counter is automatically incremented by 1 (changes to 0001H);

3 The content of the address register (0000H) is sent to the memory through the internal address bus, and the address in the memory is decoded to make the unit with the address 0000H selected;

4 The CPU makes the read control line effective;

5 Under the control of the read command, the content of the selected memory unit (it should be 74H at this time) is sent to the internal data bus. Because it is the instruction fetch stage, the content is sent to the instruction register through the data bus.

At this point, the instruction fetching stage is completed, and the decoding analysis and instruction execution stage is entered.

Since the content in the instruction register this time is 74H (opcode), after the decoder is decoded, the microcontroller will know that the instruction is to send a number to the A accumulator, and the number is next to the code Storage unit. Therefore, to execute this instruction, the data (E0H) must be fetched from the memory to the CPU, that is, the second byte must be fetched from the memory. The process is very similar to the fetching phase, except that the PC is now 0001H. The instruction decoder combines the timing components to generate a series of micro-operations of the 74H opcode, and the digital E0H is taken out from the 0001H unit.

Because the instruction requires that the obtained number be sent 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 complete. PC=0002H in the single-chip microcomputer, the PC automatically adds 1 every time the CPU fetches an instruction or a number from the memory, and the single-chip microcomputer enters the next instruction fetching stage. This process has been repeated until a pause command is received or a loop waiting command is paused. The CPU executes instructions one by one, completing all specified functions.

For a mcu, the sram will be told during the performance description, and for the beginners, they will not consider and pay attention to these things, and only use the things when they get them. In fact, these quantities are very important. Think carefully about why the code can run, how much code is, and how the defined variables of int, short, etc. are allocated and stored. These problems are related to internal dimensions. relationship.

First of all, the memory of the single chip microcomputer can be divided into ram and rom in size. The difference between ram and rom is no longer explained here. We can be equivalent to flash and sram. According to the definition of flash and sram, the data in flash is dropped. Electricity can be saved, sram is not, but the execution speed of sram is faster than flash. The program of the single-chip microcomputer can be divided into code (code storage area), RO-data (read-only data storage area), RW-data ( Read and write data storage area) and ZI-data (zero initialization data area). You can observe the values ​​of these 4 quantities in the code under the MDK compiler.

The code and RO-data are stored in the flash, so the sum of the two is the size of the space allocated to them by the flash in the microcontroller (and equal to the size of the .bin file generated by the code). In addition, RW-data and ZI-data are stored in In sram, the sum of the two is the space size that sram in the microcontroller needs to allocate to them.

In addition, we will inevitably think of the relationship between stack, heap, global (static) (staTIc), text constant area and program code area and the code, RO-data, etc. introduced above.

1. Stack area (stack): automatically allocated and released by the compiler, storing function parameter values, local variable values, etc. Its operation is similar to the stack in the data structure. These values ​​are readable and writable, so the stack should be included in the RW-data (read and write data storage area), which is the sram of the microcontroller.

2. Heap: It is generally allocated and released by the programmer. If the programmer does not release it, it may be reclaimed by the OS when the program ends. Understandably, these are also included in the sram of the microcontroller.

3. Global area (static area) (staTIc): The storage of global variables and static variables are placed together, initialized global variables and static variables are in one area, and uninitialized global variables and uninitialized static variables are adjacent to each other. The other area of ​​the program will be released by the system after the program ends. These data are also readable and writable, and are included in sram like stack and heap.

4. Text constant area: The constant string is placed here. These data are read-only, allocated in RO-data (read-only data storage area), are included in the flash.

5. Program code area: The binary code of the function body is stored in the flash, because for the MCU, when it is powered on again, the code will continue to run and will not disappear, so it is stored in the flash.

To sum up, the memory allocation of MCU is basically the same, which does not mention the flash and sram addresses corresponding to the storage space, which will be discussed later! If there is an error, please correct me.

Smart Board For Conference

Smart Board For Conference,Whiteboard Smart Board,Interactive Whiteboard Smart Board,Smart Board Interactive Whiteboard

APIO ELECTRONIC CO.,LTD , https://www.apiodisplays.com