SRAM
Every variable uses memory. Variables are stored in memory. This memory is also called SRAM. (static ram).
The available memory depends on the chip. When you double click on the chip pinout, you can view the parameters of the used chip.
A special kind of memory are the registers in the AVR. Registers 0-31 have addresses 0-31.
Almost all registers are used by the compiler or might be used in the future.
Which registers are used depends on the program statements you use.
This brings us back to the SRAM.
No SRAM is used by the compiler other than the space needed for the software stack and frame.
Some statements might use some SRAM. When this is the case it is mentioned in the help topic of that statement.
For example, CONFIG CLOCK in user mode requires variables to hold the time.
Each 8 bits used occupy one byte. When you dimension 1 bit, you will also use 1 byte.
Each byte variable occupies one byte.
Each integer/word variable occupies two bytes.
Each Long, Dword or Single variable occupies four bytes.
Each double variable occupies 8 bytes.
Each string variable occupies at least 2 bytes.
A string with a length of 10 occupies 11 bytes. The extra byte is needed to indicate the end of the string.
Use bits or byte variables wherever you can to save memory. (not allowed for negative values)
The software stack is used to store the addresses of LOCAL variables and for variables that are passed to SUB routines.
Each LOCAL variable and passed variable to a SUB/FUNCTION, requires two bytes to store the address. So when you have a SUB routine in your program that passes 10 variables, you need 10 * 2 = 20 bytes. When you use 2 LOCAL variables in the SUB program that receives the 10 variables, you need additional 2 * 2 = 4 bytes.
The software stack size can be calculated by taking the maximum number of parameters in a SUB routine, adding the number of LOCAL variables and multiplying the result by 2. To be safe, add 4 more bytes for internally used LOCAL variables.
LOCAL variables are stored in a place that is named the Frame.
When you have a LOCAL STRING with a size of 40 bytes, and a LOCAL LONG, you need 41 + 4 bytes = 45 bytes of frame space.
When you use conversion routines such as STR(), VAL(), HEX(), INPUT etc. that convert from numeric to string and vice versa, you also need a frame. In fact, the compiler creates a buffer of 24 bytes that serves as scratchpad for temporare;y variables, and conversion buffer space. So the frame space should be 24 at minimum.
Note that the use of the INPUT statement with a numeric variable, or the use of the PRINT or LCD statement with a numeric variable, will also force you to reserve 24 bytes of frame space. This because these routines use the internal numeric<>string conversion routines.
XRAM
Some processors have an external memory interface. For example the atmegs128 has such an interface.
The additional memory is named XRAM memory(extended or external memory)
When you add 32 KB RAM, the first address will be 0.
But because the XRAM can only start after the internal SRAM, the lower memory locations of the XRAM will not be available for use. The processor will automatically use the SRAM if an address is accessed that is in range of the SRAM memory.
Thus adding 32KB of XRAM, will result in a total of 32 KB RAM.
ERAM
Most AVR chips have internal EEPROM on board.
This EEPROM can be used to store and retrieve data.
In BASCOM, this data space is called ERAM.
An important difference is that an ERAM variable can only be written to a maximum of 100.000 times. So only assign an ERAM variable when it is required, and never use it in a loop or the ERAM will become unusable.
Always use the Brown out detection of the processor to prevent EEPROM corruption.
Constant code usage
Constants are stored in a constant table.
Each used constant in your program will end up in the constant table.
For example:
Print "ABCD"
Print "ABCD"
This example will only store one constant (ABCD).
Print "ABCD"
Print "ABC"
In this example, two constants will be stored because the strings differ.