Thinking about Programs (From MCU to PC)

Publisher:legend9Latest update time:2015-07-10 Source: 51heiKeywords:Program Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
I didn't think much about program execution before, and I didn't realize where a program reads instructions and where it writes data when it is running.

 

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

Keywords:Program Reference address:Thinking about Programs (From MCU to PC)

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

Use the microcontroller AT89C51 to make an 8-way running water lamp
ORG 0000H START:MOV P1,#01111111B;The bottom LED lights up LCALL DELAY ;Delay 1 second MOV P1,#10111111B ;The second LED at the bottom is on LCALL DELAY ;Delay 1 second MOV P1,#11011111B ;The third LED at the bottom is on (omitted below) LCALL DELAY MOV P1,#11101111B LCALL DELAY MOV P1,#11110111B LCALL DELAY MOV P1
[Microcontroller]
Design of high-precision frequency regulator based on AVR microcontroller
   1 Introduction   In modern industrial production and high-precision testing, we need a very accurate frequency to help determine the performance indicators of the equipment. And we hope to be able to fine-tune the frequency. The frequency obtained by using a voltage-controlled oscillator is not accurate enough, a
[Microcontroller]
Design of high-precision frequency regulator based on AVR microcontroller
Ordinary MCU Teaching Lesson 21 Serial Port Application Programming Examples
1. Serial port mode 0 application programming The serial port mode 0 of the 8051 microcontroller is a shift register mode. By connecting an external serial-input and parallel-output shift register, a parallel port can be expanded.                                                        Example: Use 8051 serial port t
[Microcontroller]
Ordinary MCU Teaching Lesson 21 Serial Port Application Programming Examples
51 MCU hardware knowledge: packaging and parameter introduction
When we look at a person, we usually look at his appearance. Similarly, electronic components also need to be looked at in appearance, or in other words, in shape, but the term is different. We call their appearance "package". 1. MCU packaging: The packages of microcontrollers can be roughly divided into: DIP (dir
[Microcontroller]
How to make KEIL (MDK) compatible with both STM32 and 51 series microcontrollers
keil4 method one:      First install KEIL4, then install KEIL3 to the same directory as MDK, skip the one that has been installed. Then open KEIL4 as an administrator, use KEIL4's registration machine to register ARM and C51. Cracking results Method Two: The following method can make keilMDK co
[Microcontroller]
Software process to improve the flash erase and write life of MSP430G series microcontrollers
    In embedded design, many application designs need to use EEPROM to store non-volatile data. Due to cost reasons, some microcontrollers do not have EEPROM integrated inside the chip. The MSP430G series processors are low-cost 16-bit processors launched by TI. There is no EEPROM in the MSP430G series microcontroller
[Microcontroller]
Software process to improve the flash erase and write life of MSP430G series microcontrollers
Application of μPSD 32XX MCU in the Development of Tax Controller
introduction --- Tax control equipment is one of the inevitable means for the national taxation department to implement tax management informatization. As one of the tax control equipment, the tax controller is an electronic device that realizes the tax control function in cooperation with computers and
[Microcontroller]
High-precision infrared temperature measurement system based on single-chip microcomputer design
  There are two main ways to measure temperature: one is the traditional contact measurement, and the other is non-contact measurement represented by infrared temperature measurement. Traditional temperature measurement not only has a slow response speed, but also must be in contact with the object being measured. Inf
[Microcontroller]
High-precision infrared temperature measurement system based on single-chip microcomputer design
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号