8181 views|10 replies

298

Posts

0

Resources
The OP
 

When defining global variables in Keil, if an initial value is assigned, will the value of the variable remain the same every time the power is turned on again? [Copy link]

 
When defining a global variable in Keil, I assign an initial value to it. Will the value of the variable remain the same each time the power is turned on again? If so, why? Thank you!
This post is from 51mcu

Latest reply

This post was last edited by BasaraTama on 2018-11-26 22:42 Generally, arm programs contain Code segment, RW segment, and ZI segment by default; RW segment is an initialized global variable; ZI segment is an uninitialized global variable; its initial value is 0, so it does not occupy rom space, and ram can be directly cleared to 0, which will not affect the size of the bin/hex executable file output by the compiler. The initialization of the RW segment, ZI segment, and stack needs to be completed before the main function is executed. You can see what operations are done in crt0.o and startup.s. Generally, you can see that the main function is called in the crt0.o and startup.s codes, and there are memcpy (moving the initial value of the global variable in the load space rom to the execution space ram) and memset (clearing the execution space ram where the uninitialized global variable is located) operations before main is executed. The above segment addresses and space addresses can be described by the linker script of the linker. It is recommended to read the compiler principle/programmer's self-cultivation.   Details Published on 2018-11-26 22:33
 

1368

Posts

6

Resources
2
 
Every time the power is restarted, the program is executed from the beginning. Can you imagine what the value will be?
This post is from 51mcu

Comments

ena
Initial value? Is the value of a global variable stored in the code area?  Details Published on 2018-11-21 11:10
 
Personal signature专注智能产品的研究与开发,专注于电子电路的生产与制造……QQ:2912615383,电子爱好者群: void
 
 

6040

Posts

204

Resources
3
 
Global variables will be reinitialized every time the power is turned on. There is no reason for this. This is stipulated in the C language standard. This is one of the tasks that the C runtime helps you do.
This post is from 51mcu

Comments

ena
Reinitialization means the same as the initial value, but aren't global variables stored in the RAM area? Shouldn't the data be saved after power failure?  Details Published on 2018-11-21 11:02
 
 
 

298

Posts

0

Resources
4
 
lcofjp posted on 2018-11-21 09:22 Global variables will be reinitialized every time the power is turned on. There is no reason for this. The C language standard stipulates this. This is one of the tasks that the C runtime helps you do.
Reinitialization means the same as the initial value, but aren't global variables stored in the RAM area? The data should not be saved after power failure?
This post is from 51mcu

Comments

The initial value is stored in ROM, and each time the power is turned on, it will be read from ROM and assigned to RAM.  Details Published on 2018-11-21 11:28
 
 
 

298

Posts

0

Resources
5
 
Lazy Cat Loves Flying posted on 2018-11-21 08:56 Every time the power is turned on again, the program is executed from the beginning. Think about it yourself, what value it will have?
Initial value? Is the value of the global variable stored in the code area?
This post is from 51mcu
 
 
 

6040

Posts

204

Resources
6
 
ena posted on 2018-11-21 11:02 Reinitialization is the same as the initial value, but aren't global variables stored in the RAM area? The data should not be saved after power failure?
The initial value is stored in ROM, and each time the power is turned on, it will be read from ROM and assigned to RAM.
This post is from 51mcu

Comments

ena
Oh! So where are the variables without initial values stored? When actually compiling, adding or reducing variables does not affect the size of the code. So when downloading the program, are the variables stored in the RAM area? Or are they defined in the ROM area, and the variables are defined in the RAM area when the power is initialized? I can't figure it out now.  Details Published on 2018-11-21 15:55
 
 
 

298

Posts

0

Resources
7
 
This post was last edited by ena on 2018-11-21 15:56
lcofjp posted on 2018-11-21 11:28 The initial value is stored in ROM, and it will be read from ROM and assigned to RAM every time the power is turned on.
Oh! So where are the variables without initial values stored? When actually compiling, adding or reducing variables does not affect the size of the code. So are the variables stored in the RAM area when the program is downloaded? Or are they defined in the ROM area, and the variables are defined in the RAM area when the power is turned on? What I don’t understand now is whether global variables are stored in the code area or the RAM area? I didn’t think too much about it before, but now I feel like I don’t understand anything…

This post is from 51mcu

Comments

It depends on your data type. By default, the memory code is assigned to the ROM area. You cannot write xdata in the extended memory and pdata in the paged memory... Keil will clear all memory data to 0 during initialization. If this initialization clear is not executed in the assembly of the project under construction, and it is a software reset,  Details Published on 2018-11-21 16:16
 
 
 

4005

Posts

0

Resources
8
 
This post was last edited by huo_hu on 2018-11-21 16:17
ena posted on 2018-11-21 15:55 Oh! So where are the variables without initial values stored? When actually compiling, adding or reducing variables does not affect the size of the code. So...
It depends on what your data type looks like. The default is memory code. It is assigned to the ROM area. You cannot write xdata in the extended external memory and pdata in the paged external memory... You can see it by looking at the 51 extended keywords. Keil will clear all memory data to 0 during initialization. If this initialization clear is not executed in the assembly of the project under construction, and it is a software reset, then the original value will be used.
This post is from 51mcu
 
 
 

189

Posts

0

Resources
9
 
Unless the data you saved in E2 will not be lost, the value of other data will be uncertain when the power is turned on again, so it is best to initialize each variable
This post is from 51mcu
 
Personal signature单片机软件/硬件交流群:127034610
 
 

298

Posts

0

Resources
10
 
huo_hu posted on 2018-11-21 16:16 It depends on what your data type looks like. The default is memory code, which is assigned to the ROM area. You cannot write xdata in the extended memory or pdata in the paged memory.. ...
Well, when the keywords are defined, they are defined in each partition. So when downloading the program, are the variables directly assigned to each? Does keil initialization mean restarting the power?
This post is from 51mcu
 
 
 

62

Posts

0

Resources
11
 
This post was last edited by BasaraTama on 2018-11-26 22:42 Generally, arm programs contain Code segment, RW segment, and ZI segment by default; RW segment is an initialized global variable; ZI segment is an uninitialized global variable; its initial value is 0, so it does not occupy rom space, and ram can be directly cleared to 0, which will not affect the size of the bin/hex executable file output by the compiler. The initialization of the RW segment, ZI segment, and stack needs to be completed before the main function is executed. You can see what operations are done in crt0.o and startup.s. Generally, you can see that the main function is called in the crt0.o and startup.s codes, and there are memcpy (moving the initial value of the global variable in the load space rom to the execution space ram) and memset (clearing the execution space ram where the uninitialized global variable is located) operations before main is executed. The above segment addresses and space addresses can be described by the linker script of the linker. It is recommended to read the compiler principle/programmer's self-cultivation.

This post is from 51mcu
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list