The storage location of each variable in MCU is ram or flash

Publisher:亚瑟摩根Latest update time:2018-05-05 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

I had nothing to do, so I studied the storage location
of variables in MCU. ps: There are three places where data can be stored in MCU: register, rom, and ram.

Standard C keywords involved: register: define and apply for a register-level variable
                          auto: define automatic variables, which are compiler defaults by default
                          const: define constants

involving IAR predefines: __no_init: indicates that the variable does not need to be initialized
                         section .text segment: stores code
                         section .rodata segment: stores read-only data
                         section .noinit segment: stores data that does not need to be initialized
                         section .bss segment: stores default initialized data
                         section .data segment: stores initialized data
                         CSTACK segment: stack
                         HEAP segment: heap

//Attribute: macro variable
//Location: It is assembled into the section .text segment during precompilation (usually placed in rom)
//Operation: It no longer exists during operation
#define ro_def 0x11111111UL

//Attribute: constant, read-only variable
//Location: directly exists in the section .rodata segment (usually placed in rom)
//Operation: All accesses in the program are in section .rodata segment (slower speed)
const uint32_t ro_var = 0x222222222;

//Attribute: Uninitialized global variable
//Location: It exists in section .noinit: DATA segment (usually placed in ram)
//Operation: When accessed in the program, it is accessed in section .noinit: DATA segment (faster speed)
__no_init uint32_t ni_global_var;                 

//Attribute: Default 0 initialized global variable
//Location: It exists in section .bss segment (usually placed in ram)
//Operation: Clear the bss segment at startup; When accessed in the program, it is accessed in section .bss segment (faster speed)
uint32_t zi_global_var;

//Attribute: Initialized global variable
//Location: The initialization value exists in section .data_init segment (usually placed in rom), and the variable itself exists in section .data segment (usually placed in ram)
//Operation: At startup, copy the initial value from the .data_init segment to the .data segment; when accessed in the program, it is accessed in the section .data segment (faster)
uint32_t i_global_var = 0x33333333; 

void test_variable(void) 
{
    //Attribute: Initialized register variable
    //Location: Its initialization value exists in the section .text segment (usually placed in ROM), and the variable itself exists in the CPU's register
    //Operation: If the application is successful, when accessed in the program, it is accessed in the CPU's register (fastest speed); if the application fails, it is an ordinary local variable
    register uint32_t i_register_var = 0x44444444;
    
    //Attribute: Initialized local variable
    //Location: Its initialization value exists in the section .rodata segment (usually placed in ROM), and the variable itself exists in CSTACK (usually placed in RAM)
    //Operation: When defining, copy the initial value from the .rodata segment to the CSTACK; when accessing in the program, it is accessed in the CSTACK segment (faster). It is said that pushing local variables into the stack is just a simple move of the SP pointer to free up space for local variables. Addressing uses the offset of SP as the relative address
    uint32_t i_local_iArray[4] = {0x55555555, 0x66666666, 0x77777777, 0x88888888};

    //Attribute: dynamic memory allocation
    //Location: The value is assigned to the section .text segment, and the requested memory area is in the HEAP (usually in RAM)
    //Operation: When accessing in the program, it is accessed in the HEAP segment (faster)
    uint32_t *zi_local_p;
    p = (uint32_t *)malloc(4);
    ...
}

Reference address:The storage location of each variable in MCU is ram or flash

Previous article:MCU on-chip memory programming
Next article:Detailed explanation of the use of MCU C language header file #ifndef/#define/#endif

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号