Home

Placing RAM Variable in an Absolute Address

E-mail Print PDF
( 1 Vote )
Question: How do I place a RAM variable in an absolute RAM address?

Answer: A RAM variable may be placed in an absolute RAM address using three methods.

Option-A: Using the #pragma directive

Let us say, we need to allocate a variable ‘myByte’ at an address 0x78 in RAM and another variable ‘myByte1’ at address 0x104 , then the process would be:

1. Declare a custom area in RAM using the ‘#pragma data’ directive and declare the variable under this area:
#pragma data: MyAbsArea
BYTE myByte;
#pragma data: MyAbsArea1
BYTE myByte1;
‘MyAbsArea’ and ‘MyAbsArea1’ can be any user defined name.

2. Create a file called ‘custom.lkp’ in the project folder with the following lines:

-bMyAbsArea: 0x0078
-bMyAbsArea1: 0x0104

This will place the area MyAbsArea in address 0x0078 and MyAbsArea1 in address 0x0104

Notes:
1. After defining variables in the custom areas, before starting the code, remember to use the “#pragma data: data” instruction before starting the main code.

2. The variable allocation to the absolute addresses may be verified by checking the map file (.mp).  In the map file, the address of these variables are shown as below.
Area                               Addr   Size   Decimal Bytes (Attributes)
--------------------------------   ----   ----   ------- ----- ------------
                       MyAbsArea   0078   0001 =      1. bytes (rel,con,ram)

       Addr  Global Symbol
      -----  --------------------------------
       0078  _myByte
       0079  __MyAbsArea_end

Area                               Addr   Size   Decimal Bytes (Attributes)
--------------------------------   ----   ----   ------- ----- ------------
                      MyAbsArea1   0104   0001 =      1. bytes (rel,con,ram)

       Addr  Global Symbol
      -----  --------------------------------
       0104  _myByte1
       0105  __ramareas_end
       0105  __MyAbsArea1_end
The two custom areas are shown in the end of the .mp file.
User Base Address Definitions

lit = 0x150
data = 0x0
SSCParmBlk:0x00F7.0x00FF
MyAbsArea = 0x78
MyAbsArea1 = 0x104

Option – B: Declaring the Absolute area in a separate .asm file

For the same requirement as in case A, declaring a variable ‘myByte’ at 0x0078  and ‘myByte1’ at 0x0104 , the procedure would be :

1. Create a new assembly file, say ‘variable.asm’ and add the following code:
export _myByte
export _myByte1
AREA MyAbsArea (RAM)
 _myByte:BLK 1

AREA MyAbsArea1(RAM)
 _myByte1:BLK 1
Note that the ‘export’ keyword and the underscore ‘_’  before the variable name makes it accessible from the ‘C’ file . In the ‘C’ file , declare the variable with the extern keyword, without the underscore  :
extern BYTE myByte;
extern BYTE myByte1;

2. Create a custom.lkp file similar as in case A , with the following lines :
-bMyAbsArea:0x0078
-bMyAbsArea1:0x0104

Option – C:  Assembly language

Here , we declare an absolute area in RAM with the attributes - RAM, ABS, CON and this area needs to be originate from the required address. For example, let’s say, if  we need a variable ‘myByte’ at address 00 in RAM, the syntax would be:
AREA myAbsArea( RAM, ABS, CON)
ORG 00h
_myAbsArea_start:
myByte: BLK 1
Notes:
1.The custom.lkp file is not required in this case .
2. After declaring this area in RAM, use the ‘area text (ROM)’ to start writing the program code.
3. The assembly variables can be accessed from C using the same method described in Option-B, ie, by creating the variable with an underscore, exporting the variable to be Global and using the "extern" keyword in the C file to refer the variable.


Options A and B work with the ImageCraft compiler.  In HiTech compiler, a variable may be placed in an absolute location by using the following syntax.
variable_type variable_name @ address
For example to place an BYTE variable named myByte at address 0x78 :
BYTE myByte @0x78;

Comments (0)
Only registered users can write comments!