Common memory division methods in embedded systems

       Seeing that some friends are discussing the topic of single-chip memory, today I will describe the common division areas in conjunction with STM32.

In an STM32 program code, from the high address of the memory to the low address of the memory, the stack area, the heap area, the global area (static area), the constant area, and the code area are distributed in sequence. The address is distributed with .data segments.

The overall distribution looks like this:

memory high address

stack area

heap area

.bss section

.data section

constant area

memory low address

code area

1. Stack area (stack)

  • Temporarily created local variables are stored in the stack area.

  • When a function is called, its entry parameters are stored in the stack area.

  • When the function returns, its return value is stored in the stack area.

  • Local variables defined by const are stored in the stack area.

2. Heap

The heap area is used to store memory segments that are dynamically distributed during program execution, and can be increased or decreased.

Functions such as malloc can be used to dynamically distribute memory.

The memory allocated by the malloc function must be released with free, otherwise it will cause a memory leak.

3. Global area (static area)

The global area consists of the .bss segment and the .data segment, which are readable and writable.

4. The .bss section

Uninitialized global variables are stored in the .bss section.

Global variables initialized to 0 and static variables initialized to 0 are stored in the .bss section.

The .bss segment does not occupy the executable file space, and its content is initialized by the operating system.

5. The .data section

Initialized global variables are stored in the .data segment.

Static variables are stored in the .data segment.

The .data segment occupies the space of the executable file, and its content is initialized by the program.

Global variables defined by const are stored in the .rodata segment.

6. Constant area

Strings are stored in the constant area.

The contents of the constant area cannot be modified.

7. Code area

Program execution code is stored in the code area.

String constants may also be stored in the code area.

Through the above introduction, you may still be vague about the storage location of each data. Let's use a simple program to experience and understand it.

Through the above introduction, you may still be vague about the storage location of each data. Let's go through a simple program to experience and understand [extra paragraph]


#include <stdio.h>

static unsigned int val1 = 1;        //val1存放在.data段
unsigned int val2 = 1;               //初始化的全局变量存放在.data段
unsigned int val3 ;                  //未初始化的全局变量存放在.bss段
const unsigned int val4 = 1;         //val4存放在.rodata(只读数据段)

unsigned char Demo(unsigned int num) //num 存放在栈区
{
  char var = "123456";               //var存放在栈区,"123456"存放在常量区
  unsigned int num1 = 1 ;            //num1存放在栈区
  static unsigned int num2 = 0;      //num2存放在.data段
  const unsigned int num3 = 7;       //num3存放在栈区
  void *p;
  p = malloc(8);                     //p存放在堆区
  free(p);
  return 1;
}

void main()
{
  unsigned int num = 0 ;
  num = Demo(num);                   //Demo()函数的返回值存放在栈区。
}

Above we have conducted a comprehensive analysis of the heap, stack, global area, constant area, and code area, and also illustrated with examples. Below we discuss which media these areas are stored on.

8. Physical characteristics of RAM, ROM, and Flash Memory

First of all, we need to understand the physical characteristics of RAM, ROM, and Flash Memory.

9、RAM

RAM is also known as random access memory, and the stored content can be accessed by random read and write instructions. The data stored in the RAM will be lost when the power is turned off, so the data can only be stored when the power is turned on. Among them, RAM can be divided into two types, one is Dynamic RAM (DRAM), and the other is Static RAM (SRAM, static random access memory).

10、ROM

ROM, also known as read-only memory, can only read data from it and cannot write data arbitrarily. Compared with RAM, ROM has the disadvantage of slow reading and writing speed. But because it has the advantage that the data can remain unchanged after power failure, it is often used to store programs and data that are written once. For example, the chip of the main version of the BIOS program is the ROM memory.

11、Flash Memory

Because ROM has the characteristics of not easy to change, Flash Memory was developed later. Flash Memory not only has the characteristics of not losing data when the ROM is powered off, but also can change the data when needed, but the price is higher than that of ROM.

12. Storage location of different data

From the previous analysis, we know that the contents of the code area and the constant area are not allowed to be modified, and the ROM (STM32 is Flash Memory) is also not allowed to be modified, so the contents of the code area and the constant area are compiled and stored in the ROM.

The stack, heap, and global area (.bss segment, .data segment) are all stored in RAM.

So far, the area where different data is stored has been fully introduced. The following will also introduce Keil's Build Output window.

13. Keil's Build Output window

picture

As shown above, there are four code segment sizes of Code, RO-data, RW-data, and ZI-data.

Among them, Code is the code occupation size, RO-data is a read-only constant, RW-data is an initialized readable and writable variable, and ZI-data is an uninitialized readable and writable variable.

Sometimes, we need to know the usage of RAM and ROM, then we can use the following formula to calculate.

RAM  = RW-data + ZI-data

ROM = Code + RO-data + RW-data 

Guess you like

Origin blog.csdn.net/weixin_41114301/article/details/132415464