Follow+Star PublicAccount, don’t miss out on exciting contentSource | typedefThis article shares some basic knowledge related to memory commonly found in microcontrollers.
1. Two Types of Memory
FLASH
Flash Memory is a secure and fast storage medium that is small in size, has a large capacity, is cost-effective, and retains data without power. It has become the primary carrier for data and programs in embedded systems.
Flash is <span class="language-plaintext">block-structured</span>, meaning it is physically divided into several independent blocks.
Flash write operations must <span class="language-plaintext">erase before write</span>, as Flash can only change data bits from 1 to 0, not from 0 to 1. Therefore, an erase operation must be performed before writing to the memory, and the minimum unit for erasure is a block, not a byte.
RAM
RAM (Random Access Memory), also known as internal memory, is the internal storage that directly exchanges data with the CPU. It is very fast, but data is not retained when power is lost.
RAM is mainly used to store global variables, stacks, and other data used in programs.
2. Three Memory Areas
Three types of memory in the map
After compiling the project, a .map file is generated, which indicates the size of the ROM and RAM usage at the end, as shown in the figure below:

Here, ROM refers to the size of the program burned into FLASH, while RW refers to the size occupied by RAM.
RO
RO (Read Only): This is a read-only area that needs to be preserved for a long time and is burned into Flash. The text segment and constdata segment mentioned below belong to this category.
RW
RW (Read Write): This area is both readable and writable, typically for global and static variables. The .data and .bss segments mentioned below belong to the RW area.
ZI
ZI (Zero Init): This area is either uninitialized or initialized to 0. When the system powers on, it actively initializes this area to 0. The .bss segment mentioned below is one such area. Additionally, the map file compiled by the Keil tool also marks the Heap and Stack areas with the Zero attribute, so Heap and Stack can also be considered ZI areas.
Comparison of ROM and RAM Data
| Data Segment | Description | RAM | ROM |
|---|---|---|---|
| .bss | — | true | false |
| .data | true | true | |
| RO-data | Constant | false | true |
| .text | — | false | true |
| stack | Local variables, etc. | true | false |
| heap | malloc | true | false |
3. Six Segments

.text
.text code segment: Used to store program code, which is permanently stored in a read-only manner after compilation, belonging to the <span class="language-plaintext">code segment</span>
.constdata
.constdata read-only constant data segment: Data types defined with const are stored here, belonging to the <span class="language-plaintext">constant storage area</span>
.data
Used to store global (global) and static variables that are initialized to non-zero values. It is readable and writable, belonging to the <span class="language-plaintext">static storage area</span>
.bss
.bss stands for <span class="language-plaintext">Block Started by Symbol</span>, which translates to <span class="language-plaintext">a block started by a symbol</span>. This part is similar to the data section, but it does not occupy space in the executable file.
bss typically refers to a memory area used to store <span class="language-plaintext">uninitialized or zero-initialized global and static variables</span>, which is readable and writable, belonging to the <span class="language-plaintext">static storage area</span>. If a variable is uninitialized, the system will initialize it to 0.
heap
heap area: This is what we commonly refer to as dynamic memory allocation, using malloc/free for allocation and deallocation, belonging to the dynamic storage area.
stack
stack area: Used to save local variables and parameters during code execution, belonging to the dynamic storage area.
———— END ————

● Column “Embedded Tools”
● Column “Embedded Development”
● Column “Keil Tutorial”
● Selected Tutorials from the Embedded Column
Follow the public accountReply “Join Group” to join the technical exchange group according to the rules, reply “1024” to see more content.
Click “Read Original” to see more shares.