Cosmic UK Cosmic US Cosmic Germany Cosmic Italia Cosmic France

Cosmic Software Frequently Asked Questions

How do I place constants at a fixed memory address?

This example was developed for STM8, but the idea is the same for all Cosmic compilers.

The syntax:

	int var = 10 @0xe000;
would seem obvious, but is not accepted by the compiler. A variable that is located at an absolute memory address (@0x1000) cannot be initialized, because the compilers considers it as an extern. In order to accomplish this, we need to proceed in another way. In the C source, we declare the variable in this way:
	#pragma section const {iconst}
	const int var = 10;
	#pragma section const {}
The variable is thus created in a special section named .iconst (in this example). The second #pragma directive is needed to restore the compiler standard behaviour, in case there are other const variables declared after this.

Now we need to tell the linker where to place this section in memory, with the following directive in the linker file:
	+seg .iconst -b 0xe0000
placed before the object file where the variable is declared.
If you have more than one initialized variable to declare at an absolute address, there are two ways to control precisely the address of each:
  • Use a different section for each variable
  • Put them all in the same section, knowing that the compiler/linker will always allocate global initialized variables in the order in which they are defined (it could not be the case, for exampe, for non-initialized variables)