Maker Pro
Maker Pro

RAM address allocation

electronicsLearner77

Jul 2, 2015
306
Joined
Jul 2, 2015
Messages
306
Very basic question let us say i have two RAM variables x1, x2 and let us say the micro controller generates address A1 for x1 variable and A2 address for x2 variable when i compiled and linked for the first time. When i link the next time how does it generate the same address A1 for x1 and A2 address for x2? Will it apply any rules while generating addresses for variables? Please help.
 

Bluejets

Oct 5, 2014
6,901
Joined
Oct 5, 2014
Messages
6,901
Doesn't generate anything. The variables are in non- voitile eeprom.
 

kellys_eye

Jun 25, 2010
6,514
Joined
Jun 25, 2010
Messages
6,514
Your PROGRAM will define where the data goes - either to a specific register or to an address in RAM.
 

electronicsLearner77

Jul 2, 2015
306
Joined
Jul 2, 2015
Messages
306
Now i understood I think for RAM variables i need not worry about their addresses, i should access them by their name.
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,700
Joined
Nov 17, 2011
Messages
13,700
Now i understood I think for RAM variables i need not worry about their addresses, i should access them by their name.
Right, that's one point (among others) why we use high level languages.
Even in assembler you'd usually assign a named location and use the name (called label in that case) to ease handling.

There are exceptions to the rule: when you need to address specific memory locations (e.g. to read from an I/O port or to write to a port) you need to use fixed addresses as the locations of these ports, registers or whatever are fixed by the hardware architecture. It is common to have a library (or a set of libraries) that define the fixed addresses for the type of microcontroller you use so you can use symbolic names.
As an example for an AVR microcontroller you might use:
Code:
PORTA=0xFF;
for setting the data in the fixed PORT A. The library that handles the assignment of PORT A (named PORTA as blanks are not allowed within a name) is included into the code via
Code:
#include <avr/io.h>

Other programming languages or compilers will handle this in a similar way, the specific syntax may vary.
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,700
Joined
Nov 17, 2011
Messages
13,700
Your PROGRAM will define where the data goes - either to a specific register or to an address in RAM.
When you code for a microcontroller it is essential that you are aware of the architecture. Data can be stored in many locations:
  • ROM (FLASH)
  • EEPROM
  • RAM
  • Registers
Which storage location you use depends on several factors. Typically variables would be stored in RAM, constants in FLASH. However, commonly data structures are copied from FLASH to RAM at startup so even constants use valuable RAM space. If RAM is scarce, you may be able to keep constants in FLASH. The AVR family of microcontrollers uses the PROGMEM directive to achieve this. You save RAM at the cost of execution speed.

For temporary variables e.g.loop counters it may be advantageous to explicitly assign them to registers instead of RAM as register access is usually faster than RAM access. Another directive REGISTER tells the compiler that the variable should be placed in a register although there is no guarantee that it is really assigned to a register. If all available registers are used by other data, the compiler will use a RAM location instead.

Reading an Writing to EEPROM has the advantage that data is kept save even during power outages. But you cannot simply write to an EEPROm, you need to use an EEPROM programming routine.Again, that is usually taken care of by suitable libraries.
 
Top