Init variable's memory content during comiple-link stage

Hello,

Some of the variables that my program uses(may contain also big ,multidimensional arrays) have default values, which are used during the code operations and may be modified during the program's runtime.

defining a variables using:

volatile xdata arrayName = [0x01, 0x03, 0x02, 0x04...];
Will case code generation that will occupy more then twice the size of the array. It will generate code are that will hold the array's default values, as well as code that in the beginning of the program startup will copy theses values into the arrays themselves.

On the other side, writing the code as follows:
volatile code arrayName = [0x01, 0x03, 0x02, 0x04...];

(Use of code area, instead of xdata area)

Will cause the array to be generated as part of the code.
There will not be any code line that will init the array into these values, and the init values will be directly located at the generated HEX file, at the place where the array is located, during the compile-link stage.
This is good since it saves time (of the variables init) and more important – it uses much less memory area.
The problem is that defining a variable as code, will prevent it from being modified.

My question:
Is there a way to define variables, that the init value of them will be located in the array's place during the compile-link stage, and that will be modifiable during the program runtime ? can you please tell me the syntax for such a declaration ?

Thanks.
Amit A.

Parents
  • No, there is no such method. Because there can't be.

    Initializers have to be in read-only code memory so you can load them into the device as part of the program, where they will survive a power-cycle of the device. But modifiable variables must be in RAM. Thus, an initialized modifiable variable must exist in two separate copies. Nothing to be done about it.

    There's one possible way around this, though: computed startup values. I.e. you can do the initialization manually, instead of leaving it to the compiler, and express it in a more compact manner than a list of values. If you're lucky.

Reply
  • No, there is no such method. Because there can't be.

    Initializers have to be in read-only code memory so you can load them into the device as part of the program, where they will survive a power-cycle of the device. But modifiable variables must be in RAM. Thus, an initialized modifiable variable must exist in two separate copies. Nothing to be done about it.

    There's one possible way around this, though: computed startup values. I.e. you can do the initialization manually, instead of leaving it to the compiler, and express it in a more compact manner than a list of values. If you're lucky.

Children
More questions in this forum