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);
...
}
Previous article:MCU on-chip memory programming
Next article:Detailed explanation of the use of MCU C language header file #ifndef/#define/#endif
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- ST NUCLEO_G431RB Review Summary
- Semiconductor Read Only Memory
- What is EMC?
- The missing solder mask on the high-speed line causes BGA solder joint failure
- Last three days! Free application for Qinheng USB PD power receiving protocol chip CH224, and gifts are waiting for you
- The confusion of working in an industry for a long time
- A D-type flip-flop and ADC sampling debounce problem
- ad7768-1 spi readback data
- 【NXP Rapid IoT Review】W4 Bluetooth Review
- Transistor problem