Linker Directives

The mikroC PRO for ARM uses an internal algorithm to distribute objects within memory. If you need to have a variable or routine at specific predefined address, use the linker directives absolute and org.

When using these directives, be sure to use them in proper memory segments, i.e. for functions it is the KSEG0 and for variables it is the KSEG1. Linker directives are used with the virtual addresses.

Directive absolute

Directive absolute specifies the starting address in RAM for a variable or a starting address in ROM for a constant. If the variable or constant is multi-byte, higher bytes will be stored at the consecutive locations.

Directive absolute is appended to declaration of a variable or constant :

// Variable x will occupy 1 byte at address 0x20000000 :
short x absolute 0x20000000;

// Variable y will occupy 2 bytes at addresses 0x20000000 and 0x20000001 :
int y absolute 0xA0000000;

// Const array elements will be placed on the consecutive locations starting from -0x70000 :
const short ConstantArray[] = {1,2,3} absolute 0x70000;
  Note :

If you want to place simple type constant into Flash memory, instead of following declaration:

const short SimpeConstant = 0xAA absolute 0x20000000;

use an array consisting of single element :

const short SimpleConstant[] = {0xAA} absolute 0x20000000;

In first case, compiler will recognize your attempt, but in order to save Flash space, and boost performance, it will automatically replace all instances of this constant in code with it's literal value.
In the second case your constant will be placed in Flash in the exact location specified.

Be careful when using the absolute directive, as you may overlap two variables by accident. For example:

// Variable i will occupy 1 byte at address 0x20000003
char i absolute 0x20000003;

// Variable will occupy 4 bytes at 0x20000000, 0x20000001, 0x20000002, 0x20000003; thus,
// changing i changes jjjj highest byte at the same time, and vice versa
long jjjj absolute 0x20000000;

Directive org

Directive org specifies a starting address of a routine in ROM. Directive org is appended to the function definition. Directives applied to non-defining declarations will be ignored, with an appropriate warning issued by the linker.

Here is a simple example:

void func(int par) org 0x70000 {
// Function will start at address 0x70000
  asm nop;
}

It is possible to use org directive with functions that are defined externally (such as library functions). Simply add org directive to function declaration:

void UART1_Write(char data) org 0x70000;

Directive orgall

If the user wants to place his routines, constants, etc, above a specified address in ROM, #pragma orgall directive should be used:

#pragma orgall 0x70000<

Directive funcorg

You can use the #pragma funcorg directive to specify the starting address of a routine in ROM using routine name only:

#pragma funcorg <func_name>  <starting_address>
Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code