Connect with us

RAM address allocation

Discussion in 'Microcontrollers, Programming and IoT' started by electronicsLearner77, Aug 25, 2017.

Scroll to continue with content
  1. electronicsLearner77

    electronicsLearner77

    73
    1
    Jul 2, 2015
    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.
     
  2. Bluejets

    Bluejets

    3,764
    781
    Oct 5, 2014
    Doesn't generate anything. The variables are in non- voitile eeprom.
     
  3. electronicsLearner77

    electronicsLearner77

    73
    1
    Jul 2, 2015
    But it allocates RAM address location for each variable? So the addresses keep changing every time i link it?
     
  4. kellys_eye

    kellys_eye

    4,277
    1,146
    Jun 25, 2010
    Your PROGRAM will define where the data goes - either to a specific register or to an address in RAM.
     
  5. electronicsLearner77

    electronicsLearner77

    73
    1
    Jul 2, 2015
    Now i understood I think for RAM variables i need not worry about their addresses, i should access them by their name.
     
  6. Harald Kapp

    Harald Kapp Moderator Moderator

    9,646
    2,016
    Nov 17, 2011
    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.
     
    electronicsLearner77 likes this.
  7. Harald Kapp

    Harald Kapp Moderator Moderator

    9,646
    2,016
    Nov 17, 2011
    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.
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-