Finer points of variable handling.
Word and SWord type variables have a low byte and a high byte. The high byte may be accessed by simply adding the letter H to the end of the variable's name. For example: -
Dim MyWord as Word
Will produce the assembler code: -
MyWord: .space 2
.def MyWord
.val MyWord
.scl 2
.size 2
.type 016
.endef
.set MyWordH, n
This is only really useful when assembler routines are being implemented, such as: -
Mov.b #1,W0
Mov.b WREG, MyWordH ' Load the high byte of MyWord with 1
Dword, SDWord and Float type variables have a low, mid1, mid2, and high byte. The high byte may be accessed by using Byte0, Byte1, Byte2, or Byte3. For example: -
Dim MyDword as Dword
To access the high byte of variable MyDword, use: -
MyDword.Byte3 = 1
The same is true of all the alias modifiers such as SWord0, Word0 etc...
Casting a variable from signed to unsigned and vice-versa is also possible using the modifiers. For example:
Dim sMyDword as SDword ' Create a 32-bit signed variable
sMyDword.Byte3 = 1 ' Load the unsigned high byte with the value 1
sMyDword.SByte0 = -1 ' Load the signed low byte with the value -1
sMyDword.SWord0 = 128 ' Load signed low and mid1 bytes with 128
Notes.
The final RAM usage will also encompass the microcontroller’s stack size, therefore, even if the BASIC program only declares 4 Byte variables, the final RAM count will be 84. 80 bytes for the default stack size and 4 bytes for variable usage. If handled interrupts are used, the stack size will increase due to context saving and restoring requirements.
RAM locations for variables is allocated automatically within the microcontroller because the PIC24® and dsPIC33® range of devices have specific requirements concerning RAM addressing. Which are:
16-bit variables must be located on a 16-bit RAM address boundary. 32-bit and 64-bit variables must be placed on a 16-bit RAM address boundary, but should be placed on a 32-bit RAM address, if possible, for more efficiency with some mnemonics. 8-bit variables can be located on an 8-bit,16-bit or 32-bit RAM address boundary.
Therefore, the order of variable placements is:
The microcontroller's stack is located before all variables are placed. The compiler's system variables are placed. Word variables are placed. Dword variables are placed. Float variables are placed. Double variables are placed. Byte variables are placed. Pin variables are placed. Word Arrays are placed. Dword Arrays are placed. Float Arrays are placed. Byte Arrays are placed. String variables are placed.
The logic behind the variable placements is because of the microcontroller’s near and far RAM.
The first 8192 bytes of RAM are considered "near" RAM, while space above that is considered "far" RAM. By default, the compiler sets all user variables to near RAM. However, when near RAM space is full, the compiler will place variables in far RAM (above 8192).
The special significance of near versus far to the compiler is that near RAM accesses are encoded in only one mnemonic using direct addressing, while accesses to variables in far RAM require two to three mnemonics using indirect addressing.
Standard variables are used more commonly within a BASIC program, therefore should reside in near RAM for efficiency. Arrays and Strings are generally accessed indirectly anyway, therefore, it is usually of little consequence if they reside in near or far RAM.
The PIC24® and dsPIC33® range of devices have 16 WREG SFRs (Special Function Registers), each 16-bits wide. These are invaluable assets when the fastest possible speed is required by a program’s routines or procedures. However, the compiler needs certain WREG SFRs for its own use, as does the microcontroller itself. Below is a rough list of the compiler's and microcontroller's WREG use.
WREG0, WREG1, WREG2 and WREG3 are used internally by the compiler for its mathematical expressions, as well as comparisons. These WREGs should be considered as volatile and never used for storage of data for more than a brief length of time.
WREG6, WREG7 and WREG8 are sometimes used as parameter storage for the compiler's commands.
WREG12 and WREG13 are used for array indexing.
WREG14 is the microcontroller's Frame Pointer. As such, it must not be accessed directly unless the user is fully aware of the ramifications.
WREG15 is the microcontroller's Stack Pointer. As such, it must not be accessed directly unless the user is fully aware of the ramifications.
A WREG SFR can be used in a BASIC program just the same as any user defined variable. For example:
WREG0 = 12345
Note that the WREG SFRs are each 16-bits wide, but they can be sliced by the .Byte0 and .Byte1 directives, just as a user variable can be:
WREG0.Byte0 = 12
WREG0.Byte1 = 34


