About the size analysis and storage location after Keil compilation

Article directory

        When we compile the program with keil, some information of the project will appear, as shown in the figure below, where we can see the compiled program size (Program Size): Code:=xxx Ro-data=xxx RW-data=xxx ZI- data=xxx.

1. What is Code Ro-data RW-data ZI-data?

  • Code : the code area, which stores all the codes we write;
  • Ro-data(Read Only) : read-only program segment, storing constants that can only be read, such as const type;
  • RW-data(Read Write) : Read and write data segment, store non-zero global variables;
  • ZI-data(Zero Initialze) : 0 data segment, which can be understood as a global variable without initialization;

        At this time, we open the Listings folder under the project directory, and there is a .map file in it, which can be opened with Notepad, and there is such a table at the bottom:

2. Storage location

We know that there are on-chip flash and on-chip RAM in the single-chip microcomputer. RAM is equivalent to running memory, and flash is equivalent to computer hard disk. 

   RO Size includes Code and Ro Data, indicating the size of Flash occupied by the program.

   RW Size includes RW Data and ZI Data, indicating the size of RAM occupied during runtime.

   ROM Size includes Code, RO Data and RW Data, which is the size of the space we burn into Flash.

Summarize: 

   Flash storage Code + RO-Data

   SRAM stores RW Data + ZI Data

        The reason why Rom does not include ZI Data is because the data in the ZI segment will be cleared before the program runs, so there is no need to include this block of memory.

        Before the program runs, a file entity needs to be burned into the STM32 Flash, usually a bin or hex file, and the burned file is called an executable image file. As shown in the left part of the figure below, it is the memory distribution after the executable image file is burned into STM32. It includes two parts: the RO segment and the RW segment: the RO segment stores the Code and RO-data data, and the RW segment stores the The data of RW-data is not included in the image file because ZI-data is all 0.

        STM32 starts from Flash by default after power-on. After startup, the RW-data (initialized global variables) in the RW segment will be transferred to RAM, but the RO segment will not be transferred, that is, the execution code of the CPU is read from Flash. , and allocate the ZI segment according to the ZI address and size given by the compiler, and clear this RAM area.

        ——Excerpt from RT_Thread documentation

 

   corresponding storage location

extension:

        Heap area: The memory requested by the programmer in this block, such as new or malloc, will cause a memory leak if it is not released in time after use.

        Stack area: Defined local variables and formal parameters of functions follow the first-in-last-out principle, and the compiler automatically allocates and releases them.

Guess you like

Origin blog.csdn.net/qq_53734051/article/details/126405477