Memory usage

Top  Previous  Next

SRAM

Every variable uses memory. This memory is also called SRAM.

 

The available memory depends on the 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.

 

 

Each 8 bits used occupy one byte.

Each byte variable occupies one byte.

Each integer/word variable occupies two bytes.

Each Long or Single variable occupies four bytes.

Each double variable occupies 8 bytes.

Each string variable occupies at least 2 byes.

A string with a length of 10. occupies 11 byes. 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, uses 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() etc. that convert from numeric to string and vice versa, you also need a frame. It should be 16 bytes in this case.

Add additional space for the local data.

 

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 16 bytes of frame space. This because these routines use the internal numeric<>string conversion routines.

 

 

 

XRAM

You can easy add external memory to an 8515. Then XRAM (extended memory)

will become available.  When you add a 32 KB RAM, the first address will be 0.

But because the XRAM can only start after the internal SRAM, which is &H0260 for the 8515, the lower memory locations of the XRAM will not be available for use.

 

 

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 needed, and never use it in a loop or the ERAM will become unusable.

 

 

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.