Our Latest News

Introduction to the execution process of microcontroller programs

I. Preface

From the microcontroller, we know that at the moment of power-on, the MCU program pointer PC will be initialized to the address of the power-on reset, from which address to read the instructions to be executed, so that the program on the MCU begins to execute (of course, before calling the main program, there are a series of other initialization to do, such as the initialization of the stack, but these we will rarely modify)

c8903ff4-6ae1-11ed-8abf-dac502259ad0.png

Second, the boot process

The PC is similar to the MCU when it is powered on, but the computer reads the BIOS, there it completes a lot of initialization operations, and finally calls the initialization function of the system, giving control to the operating system, so we see the Windows, Linux system start up.

If the operating system is seen as a very large bare-metal program running on the processor (that is, a program running directly on the hardware, because the operating system is running directly on the CPU, it is possible to see it this way, but this bare-metal program has many functions and is very powerful), then the operating system starts up much like the MCU program starts up.

The former has a very large initialization program to complete a very complex initialization, and the latter has a short piece of assembly code to complete some simple initialization. At this point, they are very similar in terms of flow.

This image has an empty alt attribute; its file name is image-415.png

What if it is a program on the system that starts? They are determined by the system. on Linux, after typing . /p, the first check is whether it is a built-in shell command;

If not, the shell assumes that he is an executable file (usually in elf format on Linux) and then calls some related functions to copy the contents of the p file on the hard disk into memory (DDR RAM) and create a running environment for it (of course there are memory mapping, virtual memory, connection and loading, and some other things here), ready for execution.

From the above, it can be seen that the program on the microcontroller and the usual program running on the system, the difference in the start-up is very large (if the program calls main before the action, are abstracted as initialization, the program can be simplified to start: establish the runtime environment + call main function, so that the program execution differences are not significant).

Because the program running on the microcontroller (bare metal program), is the same as the operating system running on the hardware, they belong to a level. The reason why in the past did not distinguish some differences between the program on the microcontroller and the program on the PC, is not to understand this point.

As a result, some of the previous doubts have been solved. Why do programs on microcontrollers not use malloc much, but often use it on PCs? Because there is no code for memory management algorithms already written on the microcontroller, while the program running in the operating system on the PC, libc has already done all this, just call it.

If you want to use dynamic memory on the microcontroller, you can, but the code has to implement itself and define a corresponding malloc. Sometimes some companies will give some library functions that may implement malloc.

Since RAM memory is very limited on a microcontroller, it is expected to be dangerous if you don’t know how it works. Similarly, because the program running on the PC’s system is different from the logical program, the bare-metal program will not have dynamic links, there are only static links.

Execution of the program

I was confused for a long time about where to read instructions and data when executing a program because I didn’t know the difference between a program on the system and a bare-metal program. Although in the “Principles of Microcomputers” class, I knew that when a program runs, it reads instructions and data from memory for execution and writes back.

But only a few K of RAM on the microcontroller, and flash generally have tens of K or even 1M, this time the instructions and data are in the memory (the memory here refers only to RAM, because the PC we often say that the memory is DDR RAM memory, preconceptions that the microcontroller is also the same, have not understood that in fact RAM and Flash are memory)?

This is not possible, because the teacher in class only said memory, but the PC memory is generally DDR RAM, not hard disk, hard disk is the place to save data; thus the analogy, when you make yourself dizzy, the microcontroller RAM corresponds to DDR RAM, then Flash is not corresponding to the hard disk? Understand on CSAPP, the reason why the PC is all on DDR RAM, is the speed factor.

The speed of the hard disk is too slow, even the upcoming SSD is still several orders of magnitude worse than DDRRAM, so copy to DDRRAM. At this point, the code and data of a program are stored consecutively, where the code segment is a read-only area and the data segment is a read-write area (this is determined by the memory management mechanism of the operating system).

At runtime, they are then copied to the faster SRAM to get faster execution speed. And for, microcontrollers operating at just a few M, tens of M, the difference between reading from Flash and from RAM may not be significant enough to be a bottleneck for program execution.

As for PC, the speed of Flash is too slow, DDRRAM is also very slow, even SRAM is also much slower, so then increase the operating frequency can not improve the execution speed of the program, so now the fastest CPU operating frequency is around 2003, a bottleneck appears.

In order to improve the CPU utilization rate, think about it from another perspective, since we can’t reduce the execution time of a program, we can execute more programs at the same time, one core executes one program, two cores can execute two programs, so multi-core CPUs have become the mainstream now).

So the bare-metal program instructions are stored in Flash (Flash memory), while the data is placed in RAM (flash has a limit on the number of writes, and its speed is still much worse than RAM). More generally, on a microcontroller RAM stores data segments, bss segments, and stack segments;

Essentially, this and the PC program are stored in RAM is the same, the PC is the operating system can be read and write, while the microcontroller is dependent on different storage devices to distinguish between read and write (of course, now the Flash is read and write, if the Flash does not have a limit on the number of writes, and the speed can be similar to RAM, the microcontroller is not as long as the Flash can be It (directly equivalent to DDRRAM on the PC)? This will also cost less than a RAM, a Flash, more cost-efficient, more cost-effective for manufacturers).

For the microcontroller program execution when the instructions and data storage and reading, understand the following.

After programming the microcontroller, the code segment, data segment, bss segment, rodata segment, etc. of the program are stored in the Flash. When the microcontroller is powered on, initialize the assembly code to copy the data segment, bss segment, into RAM, and build a good stack, and start calling the program’s main function.

Later, there is a difference between program memory and data memory, which is used to read instructions from Flash (i.e. instruction memory, code memory) and read and write data from RAM.

IV. Conclusion

Whether it is a microcontroller or a PC, the memory pyramid that exists is the same, the speed factor, the cost constraints lead to a level of faster memory faster speed and higher cost. It should be said that the understanding of them is the understanding of the memory pyramid.

    GET A FREE QUOTE

    FPGA IC & FULL BOM LIST

    We'd love to

    hear from you

    Highlight multiple sections with this eye-catching call to action style.

      Contact Us

      Exhibition Bay South Squre, Fuhai Bao’an Shenzhen China

      • Sales@ebics.com
      • +86.755.27389663