This section discusses the allocation of variables to RAM (GPR, SRAM, or other TLA).
Variables in GCBASIC can be bits, bytes, words, integers, longs, arrays, or reals. This section will NOT address reals, as these are developmental variables only.
Variables can also be defined as aliases - this is discussed later in this section.
Basic variable allocation
Variables of type byte, word, integer, and long are placed in RAM using the following simple rules.
- A RAM memory location is automatically assigned, starting at the first available memory location.
- The first memory location is the first RAM location as defined in the chip datasheet.
- Once a variable is allocated, the RAM location is marked as used, and this specific location can be reviewed in the ASM source.
- Bytes use a single RAM location, words use two RAM locations, and integers and longs use four RAM locations.
- Subsequent variables of type byte, word, integer, or long are placed in RAM at the next available RAM location.
Variables of array and string type are placed in RAM using the following simple rules.
- A RAM memory location is automatically assigned from the end of RAM, less the size of the array plus 1 byte.
- The last memory location is the last RAM location as defined in the chip datasheet.
- Once an array is allocated, the RAM location is marked as used, and the start of the array’s RAM location can be reviewed in the ASM source.
- Subsequent variables of array type are placed in RAM at the next available location, working backwards from the start of the previous array, minus the size of this next array.
Variables of bit type are placed in RAM using the following simple rules.
- A bit’s memory location is automatically assigned to the first bit of a BYTE variable, created at a RAM memory location that is automatically assigned starting at the first available memory location. This byte can hold 8 bits.
- Once a bit is allocated, the byte is marked as used, and this specific location can be reviewed in the ASM source.
- Subsequent bits are allocated either to an existing byte variable, or, when 8 bits have already been allocated to an existing byte variable, another byte variable will be created.
Addressing Variables
Addressing a variable’s memory address can be achieved by using the @ prefix. This will return the address of the variable (@ applies to table data and any data block).
The following example shows registers DMAnSSAU, DMAnSSAH, DMAnSSAL being loaded with the address of the array WaveArray.
' Source start address
Dim addressdummy as byte
Dim DMAnSS as long ALIAS addressdummy, DMAnSSAU, DMAnSSAH, DMAnSSAL
DMAnSS = @WaveArray ' <<< the @ address-of operatorKey line: DMAnSS = @WaveArray — writes the RAM address of WaveArray into DMAnSS, an alias over registers DMAnSSAU/DMAnSSAH/DMAnSSAL; this is how a DMA peripheral’s source-address registers are loaded with a variable’s real memory location without hand-splitting
the address into bytes.
AT allocation
The Dim variable command can be used to instruct GCBASIC to allocate variables at a specific memory location, using the parameter AT.
The compiler will inspect the provided AT memory location, and if the memory location is already used (by an existing variable), is lower than the minimum memory location, or is greater than the maximum memory location, an error will be issued.
Variable Aliases
Alias creates a variable that shares the same memory location as another variable. These are useful for joining predefined byte variables together to form a word/long variable. When reading or writing to this variable, it is effectively the aliased variable that is being read or written to. Aliases are used to refer to existing memory locations, whether SFR or RAM, and aliases can be used to construct other variables. Constructed variables can be a mix (or not) of SFR or RAM.
Aliases are not like pointers in many languages - they must always refer to an existing variable or register.
When setting a register/variable bit (i.e. my_variable.my_bit_address_variable) and using an alias for the variable, then you must ensure the bytes that construct the variable are consecutive.
Alias and At cannot be used together on the same declaration line. Alias does not support Bit variables. For bit-level aliasing,
use constants or #Define macros.
Aliases are always shown in the ASM source, in the ;ALIAS VARIABLES section.
The coding approach should be to DIMension the variable (word, integer, or long) first, then create the byte aliases:
Example 1:
Dim my_variable as LONG
Dim ByteOne as Byte alias my_variable_E
Dim ByteTwo as Byte alias my_variable_U
Dim ByteThree as Byte alias my_variable_H
Dim ByteFour as Byte alias my_variable
Dim my_bit_address_variable as Byte
my_bit_address_variable = 23
'set the bit in the variable
my_variable.my_bit_address_variable = 1 ' <<< setting a single bit of the aliased Long by index
'then, use the four byte variables as you need to.Key line: my_variable.my_bit_address_variable = 1 — sets bit 23 of the 32-bit my_variable directly by variable index; because ByteOne-ByteFour are aliases over the same memory, this bit also becomes visible immediately through ByteThree (which covers bits 16-23).
To set a series of registers that are not consecutive, it is recommended to use a mask variable and then apply it to the registers:
Example 2:
Dim my_variable as LONG
Dim my_bit_address_variable as Byte
my_bit_address_variable = 23
'set the bit in the variable
my_variable.my_bit_address_variable = 1
porta = my_variable_E
portb = my_variable_E
portc = my_variable_E
portd = my_variable_EExample 3:
Dim MyADResult As Word Alias ADRESH, ADRESL
//MyADResult reads the A/D values stored in registers ADRESH, ADRESLExample 4:
dim Myhibyte, Mylobyte as Byte
dim Myvariable As Word Alias Myhibyte, Mylobyte
Myvariable=294
//294 is stored here: Myhibyte=1 Mylobyte=38.Memory Specification
All memory specifics, like RAM size and the lower and upper RAM addresses, are specified in the chip-specific DAT file.
The DAT file details should be reviewed in the PICINFO application. See the PICINFO/CHIPDATA tab for RAM and MaxAddress, etc.
A simple calculation is MaxAddress - RAM + 1 = the first memory address, and first memory address + RAM - 1 = the last memory address.
This can be confirmed by reviewing the DAT file. See the [FreeRAM] section for the start and end of RAM.
The DAT file also has a [NoBankRAM] section. NoBankRAM is somewhat misnamed - it is used for the definition of (any) access bank locations. If a memory location
is defined in both NoBankRAM and FreeRAM, then the compiler knows that it is access bank RAM. If an SFR location is in one
of the NoBankRAM ranges, then the compiler knows not to do any bank selection when accessing that register.
The [NoBankRAM] section includes two ranges: one for access bank RAM, and one for access bank SFRs.
The first range MUST be the ACCESS RAM range.
The second range is the FAST SFR range.
If there are no ranges defined in NoBankRAM, the compiler will try to guess them. On 18Fs, it will guess based on where the lowest SFR is, and on the total RAM on the chip. If there is only one range defined in the NoBankRAM locations, the compiler will assume that is the range for the RAM, and will then guess where the range for the access bank SFRs is.
'GCBASIC/GCGB Chip Data File
'Chip: 18F27Q43
[ChipData]
.... many other data rows
'This constant is exposed as ChipRAM
RAM=8192 'Dec values
.... many other data rows
'This constant is exposed as ChipMaxAddress
MaxAddress=9471 'Dec values
.... many other data rows
[FreeRAM]
500:24FF 'Hex value
[NoBankRAM]
500:55F 'Hex value
460:4FF 'Hex value
.... many other data rowsIn the example shown above, the following can be extracted.
- RAM size: RAM = 8192d
- Minimum RAM address: FREERAM = 0x500
- Maximum RAM address: FREERAM = 0x24FF
- Maximum RAM address: MAXADDRESS=9471d or 0x24FF
- ACCESS RAM: NOBANKRAM = 0x500-0x55F
- BANKED SFR: NOBANKRAM = 0x460-0x4FF
See Also:
- Dim — the Dim statement, including the AT and Alias parameters used throughout this page
- Advanced VariableTypes — more on the
_H/_U/_Ebyte-suffix aliasing convention - Variable Types — category overview

