Generally speaking, a program is essentially composed of three segments: bss segment, data segment, and text segment. This concept is a very important basic concept in current computer programming. It is also very important in the design of embedded systems, involving the memory size allocation and the space occupied by storage units when the embedded system is running.
BSS segment: BSS segment (bss segment) usually refers to a memory area used to store uninitialized global variables in the program. BSS is the abbreviation of Block Started by Symbol. BSS segment belongs to static memory allocation.
Data segment: A data segment is usually a memory area used to store initialized global variables in a program. The data segment belongs to static memory allocation.
Code segment: A code segment (code segment/text segment) usually refers to a memory area used to store program execution code. The size of this area is determined before the program runs, and the memory area is usually read-only. Some architectures also allow the code segment to be writable, that is, to allow the program to be modified. The code segment may also contain some read-only constant variables, such as string constants.
Heap: The heap is used to store memory segments that are dynamically allocated during process operation. Its size is not fixed and can be dynamically expanded or reduced. When a process calls functions such as malloc to allocate memory, the newly allocated memory is dynamically added to the heap (the heap is expanded); when using functions such as free to release memory, the released memory is removed from the heap (the heap is reduced).
The text and data segments are both in the executable file (usually solidified in the image file in embedded systems) and are loaded from the executable file by the system. The bss segment is not in the executable file and is initialized by the system.
The bss segment (data that is not manually initialized) does not allocate space for the data in this segment, but only records the size of the space required for the data. The data segment (data that has been manually initialized) allocates space for the data, and the data is stored in the target file. The DATA segment contains initialized global variables and their values. The size of the BSS segment is obtained from the executable file, and then the linker obtains a memory block of this size, which follows the data segment. When this memory area enters the address space of the program, all are cleared. The entire segment containing the DATA and BSS segments is usually called the data area at this time.
2. ARM image properties
The "ARM program" mentioned here refers to the program being executed in the ARM system, not the bin image file saved in ROM. Please pay attention to the difference.
An ARM program consists of 3 parts: RO, RW and ZI.
RO is the instruction and constant in the program.
RW is the initialized variable in the program.
ZI is the uninitialized variable in the program.
The image file contains RO and RW data. But it does not contain ZI data because ZI data is all 0, so there is no need to include it. Just clear the area where ZI data is before running the program. Including it will waste storage space.
The image file burned into ROM is not exactly the same as the ARM program in actual operation. Therefore, it is necessary to understand how the ARM program reaches the actual operation state from the image in ROM.
In fact, the instructions in RO should at least have the following functions:
1.
2.
ARM C are compiled as RO type data . Variables that are not initialized or initialized to 0 in
ARM C are compiled as ZI type data. Variables that have been initialized to non-zero values in
ARM C are compiled as RW type data.
The .text segment is the code segment. It is used to store program code. It is usually read-only (program code is finalized after compilation and cannot be changed).
The .data segment is the data segment. It is used to store initialized global variables (global) and initialized static variables (static). It is readable and writable.
The .bss segment is the global variable data segment. It is used to store uninitialized global variables (global) and uninitialized static variables (static). It is also readable and writable. bss is the abbreviation of Block Started by Symbol. The reason why bss is separated from data is that the system will clear the initial values of the variables in these bss segments to zero.
The .constdata segment is the constant data segment. It is used to store constants (const). It is also read-only.
The memory allocated by malloc in the source program is bss. Its size is not determined by the size of data, but mainly by the maximum value of memory allocated simultaneously in the program. However, if it exceeds the range, that is, the allocation fails, you can wait until the space is released before allocating it again.
For the above segments, users can flexibly define their first address and size. But for most users, the program code area is in ROM or FLASH, and the read-write area is in SRAM or DRAM. Consider the size of your program, the size of function calls, and the size of memory usage, and then refer to a link location file and make a slight modification.
The stack is what we usually call a stack. It is used to store the local variables and parameters of a function. Its operation is similar to the stack in the data structure, which is a "Last In First Out" (LIFO) data structure. This means that the last data put on the stack will be the first data removed from the stack. For information that is temporarily stored and does not need to be saved for a long time, the LIFO data structure is ideal. After calling a function or procedure, the system usually clears the local variables, function call information and other information saved on the stack. The top of the stack is usually at the end of the readable and writable RAM area, and its address space usually "decreases downward", that is, the more data is saved on the stack, the smaller the address of the stack.
The heap is what we usually call dynamic memory allocation. It is used to manage dynamic memory. Its operation method is different from the heap in the data structure.
In ARM's integrated development environment,
1. The read-only code segment is called the Code segment, which is the .text segment mentioned above.
2. The read-only constant data segment is called the RO Data segment, which is the .constdata segment mentioned above.
The above two segments are collectively referred to as RO segments (Read Only), which are placed in non-volatile devices such as ROM or FLASH.
3. The segment of initialized global variables and static variables that can be read and written is called the RW Data segment (ReadWrite), which is the .bss segment mentioned above.
4. The segment for uninitialized global variables and static variables that can be read and written is called the ZI Data segment (Zero Init), which is the .data segment mentioned above. Because the variables in this segment must be initialized to zero, it is called the ZI segment.
The above two segments are collectively referred to as the RW segment, and at runtime, it must be reloaded into the readable and writable RAM.
Previous article:Using STM32's SysTick to achieve precise delay
Next article:STM32 FSMC Learning Notes (I)
Recommended ReadingLatest update time:2024-11-16 15:57
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
- The microcontroller uses C language to generate sine wave DA data
- Prize-winning live broadcast: Registration for Novatek security surveillance solutions and future technology directions has begun!
- [Chuanglong TLA40i-EVM development board] +02. Power on, CPU and DDR test (zmj)
- Unpacking the GD32L233C-START evaluation board and downloading related resources
- New design rules after AD14...
- Characteristics and selection of common protection components for lightning overvoltage protection
- Failures in the functional safety concept
- Apple launches self-service repair program, you can repair your phone yourself in the future
- How to prevent PCB board from bending and warping during reflow oven
- Musk's "satellite dish" becomes a cat heater. Engineer: The cat is warm, but the Internet speed is so slow that it drives people crazy