Weekend · A Moment of Relaxation
Written in Advance I
Information printed from the Keil and IAR compilation (Build) window:
Program Size: Code=2596 RO-data=268 RW-data=44 ZI-data=1028
72,765 bytes of readonly code memory
3,508 bytes of readonly data memory
20,202 bytes of readwrite data memory
5,676 bytes of CODE memory
926 bytes of CONST memory
1,148 bytes of DATA memory
Everyone has likely seen the above information and should have a general understanding of its meaning, but how many friends have delved into the details regarding storage?
To know where the code, constants, global, and local variables are stored, one needs to understand some characteristics of FLASH and RAM. In a project, it is necessary to analyze their sizes and corresponding storage addresses, which can be done by analyzing the map file.
Brief Explanation of Its Meaning II
To accommodate most people, let’s briefly explain the meaning of the printed information above. The microcontroller here would like to remind everyone that there is a lot of related information online, but much of it can be misleading, such as: RW-data refers to initialized global variables. Variables modified with static are called static variables, which are still different from global variables, but initialized static variables will also fall under RW-data.
1. Keil Compilation Window Information
Program Size: Code=2596 RO-data=268 RW-data=44 ZI-data=1028
Code: refers to the size of the code;
Ro-data: refers to constant data excluding inline data;
RW-data: refers to readable and writable (RW), initialized variable data;
ZI-data: refers to uninitialized (ZI) variable data;
Code, Ro-data: are located in FLASH;
RW-data, ZI-data: are located in RAM;
Reminder: RW-data initialized data will be stored in Flash and will be moved to RAM upon power-up.
The relationships are as follows:
RO Size = Code + RO Data
RW Size = RW Data + ZI Data
ROM Size = Code + RO Data + RW Data
2. IAR Compilation Window Information
IAR has two situations, but the meanings are similar, just displayed differently:
When compiling a project (click Make), the following information will appear:
72,765 bytes of readonly code memory
3,508 bytes of readonly data memory
20,202 bytes of readwrite data memory
When compiling a single file (click Compile), the following information will appear (if there is no DATA, that line will not appear):
5,676 bytes of CODE memory
926 bytes of CONST memory
1,148 bytes of DATA memory
readonly code: size of the code (similar to CODE);
readonly data: constant data (similar toCONST);
readwrite data: writable variable data (similar to DATA);
Explanation:
In IAR, Data corresponds to RW Data + ZI Data in Keil
Memory (RAM) Allocation III
This section expands on some knowledge of RAM, which will help everyone understand the situation of RAM during program execution.
Memory is divided into five categories:
1. Stack: Automatically allocated and released by the compiler, storing function parameter values, local variable values, etc.
2. Heap: Generally allocated by the programmer (using malloc) and released (using free); if the programmer does not release it, it will remain occupied. Generally, programs for microcontrollers do not use the heap.
3. Global Area (Static): The storage of global variables and static variables is in one area; initialized global variables and static variables are in one area, while uninitialized global variables and uninitialized static variables are in an adjacent area.
4. Constant Area (const): Constant strings are stored here.
5. Program Code Area (code): Stores the binary code of function bodies.
Classic Example:
int a = 0; // Global Initialization Area
char *p1; // Global Uninitialized Area
main(void)
{
int b; // Stack
char s[] = “abc”; // Stack
char *p2; // Stack
char *p3 = “123456”; // In Constant Area, p3 is on the stack.
static int c = 0; // Global (Static) Initialization Area
p1 = (char *)malloc(10);
p2 = (char *)malloc(20);
// The allocated 10 and 20 bytes are in the heap.
strcpy(p1, “123456”); //123456\0 is in the constant area, the compiler may optimize it to the same location as p3 points to “123456”.
}
Finally IV
Search for “EmbeddDeveloper” on WeChat or scan the QR code below to follow and see more exciting content.

