This thought often popped into my head when I was reading CSAPP recently.
From the microcontroller, we know that at the moment of power-on, the MCU's program pointer PC will be initialized to the address at the power-on reset, from which address the instructions to be executed are read, and the program starts to execute on the MCU (of course, before calling the main of the program, there are a series of other initializations to be done, such as stack initialization, but we rarely go back to modify these). When the PC is powered on, it is similar to the MCU, but it reads the BIOS, which completes many initialization operations. Finally, the system initialization function is called to hand over control to the operating system, so we see the Windows and Linux systems start up. If the operating system is regarded as a large bare metal program running on the processor (that is, a program that runs directly on the hardware, because the operating system runs directly on the CPU, it is okay to look at it this way, but this bare metal program has many functions and is very powerful), then the startup of the operating system is very similar to the startup of the MCU program. The former has a large initialization program to complete very complex initialization, and the latter has a short assembly code to complete some simple initialization. In this respect, they are very similar in process.
What if it is a program on the system? They are determined by the system. After entering ./p in the shell on Linux, it first checks whether it is a built-in shell command; if not, the shell assumes that it is an executable file (usually in elf format on Linux), and then calls some related functions to copy the content of the p file on the hard disk to the memory (DDR RAM), and establish its operating environment (of course there are also memory mapping, virtual memory, connection and loading, and other things here), ready for execution.
From the above, we can see that there is a big difference between the programs on the MCU and the programs running on the system at startup (if the actions before the program calls main are abstracted as initialization, the startup of the program can be simplified to: establish the operating environment + call the main function, so the execution difference of the program is not big). Because the program running on the MCU (bare metal program) runs on the hardware like the operating system, they belong to the same level. The reason why some differences between programs on MCUs and programs on PCs were not distinguished in the past is that this point was not understood.
Thus, some previous doubts are solved. Why do programs on MCUs rarely use malloc, but often use it on PCs? Because there is no pre-written code for memory management algorithms on MCUs, and for programs running in the operating system on PCs, libc has already done all of this, and you only need to call it. If you want to use dynamic memory on a MCU, you can, but you have to implement these codes yourself and define a corresponding malloc. Sometimes some companies will provide some library functions that may implement malloc, but because the RAM memory on the MCU is very limited, if you don’t know how it works, it will probably be very dangerous. Similarly, because the programs running on the PC system are different from the logic machine programs, bare metal programs will not have dynamic links, but only static links.
When the program is executed, where to read instructions and where to read data, I was confused for a long time because I didn't understand the difference between the program on the system and the bare metal program. Although I know in the "Microcomputer Principles" class that when the program is running, it reads instructions and data from the memory for execution and write back. But there are only a few K of RAM on the microcontroller, and the flash is generally dozens of K or even 1M. At this time, are the instructions and data in the memory (the memory here refers only to RAM, because the memory we often talk about on the PC is DDR RAM memory, and we have a preconceived idea that the same is true for the microcontroller, and we don't understand that RAM and Flash are actually memory)? This is impossible, because the teacher only talked about memory in class, but the memory on the PC is generally DDR RAM, not the hard disk, which is where the data is stored; when making this analogy, I confused myself. The RAM of the microcontroller corresponds to DDR RAM, so does Flash correspond to the hard disk? In CSAPP, I understood that the reason why everything on the PC is in DDR RAM is the speed factor. The hard disk is too slow, even the upcoming SSD is still several orders of magnitude slower than DDR RAM, so it is copied to DDR RAM. At this time, the code and data of a program are stored continuously, where the code segment is a read-only area and the data segment is a readable and writable area (this is determined by the memory management mechanism of the operating system). During operation, they are copied to the faster SRAM to obtain faster execution speed. For the single-chip microcomputer, the operating frequency is only a few M or tens of M. The difference between reading from Flash and reading from RAM may not be obvious and will not become a bottleneck for program execution (for PC, the speed of Flash is too slow, the speed of DDRRAM is also very slow, and even SRAM is much slower, so increasing the operating frequency will not increase the execution speed of the program, so now the fastest CPU operating frequency is around 2003. A bottleneck has appeared. In order to improve the utilization rate of the CPU, think about it from another angle. Since the execution time of a program cannot be reduced, more programs can be executed in the same time. One core executes one program, and two cores can execute two programs, so multi-core CPU has become the mainstream now). Therefore, the bare metal program instructions are stored in Flash (Flash memory), and the data is placed in RAM (the number of writes to flash is limited, and its speed is still much worse than RAM). More generally speaking, RAM on a microcontroller stores data segments, bss segments, and stack segments; ROM (EPROM, EEPROM, Flash and other non-volatile storage devices) stores code and read-only data segments. In essence, this is the same as the storage of programs in RAM on a PC. On a PC, the operating system specifies readable and writable, while on a microcontroller, different storage devices are used to distinguish between readable and writable (of course, Flash is now readable and writable. If Flash has no limit on the number of writes and its speed is similar to RAM, then does a microcontroller only need Flash (directly equivalent to DDRRAM on a PC)? This will also be cheaper than one RAM and one Flash, saving more costs and being more cost-effective for manufacturers).
The storage and reading of instructions and data during the execution of the microcontroller program can be understood as follows:
After programming the microcontroller, the program's code segment, data segment, bss segment, rodata segment, etc. are all stored in Flash. When the microcontroller is powered on, the initialization assembly code copies the data segment and bss segment to RAM, and sets up the stack, and starts calling the main function of the program. Later, there was a distinction between program memory and data memory. During operation, instructions are read from Flash (i.e., instruction memory, code memory), and data is read and written from RAM. The significance of RAM is that it is faster.
Whether it is a microcontroller or a PC, the existing memory pyramid is the same. The speed factor and cost limitation lead to faster speed and higher cost of faster memory at each level. It should be said that the understanding of program execution is the understanding of the memory pyramid.
Note:
So, what are RAM, ROM, and Flash? Although they are all forms of computer memory, RAM, ROM, and FLASH all interact with the data they store in their own way. Here is a brief description of each type of memory.
RAM: stands for random access memory: the memory that the microprocessor can access by reading and writing. When we create something, it is done in memory. RAM is memory and vice versa.
ROM: Stands for Read Only Memory: The microprocessor can read ROM, but cannot write to or modify it. ROM is permanent. ROM chips often hold special computer instructions that are important and never change. The microprocessor can access the information stored in ROM at any time. Because these instructions cannot be erased, they are stored in ROM.
Flash Memory: A special memory that has the properties of both RAM and ROM. We can write data to Flash memory just like we do with RAM, but like ROM, the data is not lost when the power is off. Unfortunately, Flash memory is not as fast as RAM, so don't expect it to replace standard computer memory at any time.
http://www.dummies.com/how-to/content/ram-rom-and-flash-memory.html
ram, rom and flash
http://www.ehow.com/about_6398451_difference-between-flash-memory-ram.html
ram flash work
http://www.ehow.com/facts_6917638_difference-hard-disk-flash-memory_.html
flash disk work
Previous article:Single chip digital tube digital rotation display
Next article:Record your growth process, this year is critical
Recommended ReadingLatest update time:2024-11-17 00:05
- Popular Resources
- Popular amplifiers
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- Siemens Motion Control Technology and Engineering Applications (Tongxue, edited by Wu Xiaojun)
- Modern Electronic Technology Training Course (Edited by Yao Youfeng)
- Modern arc welding power supply and its control
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Signal linear transformation problem
- [GD32E231 DIY Contest] Part 4: Summary
- MSP430 microcontroller peripheral crystal oscillator design selection and reference solution
- 【Gravity:AS7341 Review】+ Sensor Color Value Detection
- Schematic to PCB
- [ESP32-Korvo Evaluation] (6) Preliminary evaluation of microphone array algorithm effect
- Brief Analysis of OSAL Principle of CC2540
- EPWM interrupt cannot be enabled
- "Power supply roadblock" + problems that are easily overlooked in power supply design
- Three closed loop servo motor motion control