This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

General MCU Query - about Memory

WHen we burn hex file in program, what & how allocation is done in memory. Like for particular code- hex ile is generated with ram used=200 bytes & rom used = 14 Kbytes


1. Like hex file which is burned went in rom as it contain our code.
2. What exactly ram have- initial variables, what else.
3. Stack is executed during run time. Only its starting address is defined in MCU

  • Aamir Ali said:
    how allocation is done in memory

    It is done by the Linker - so see your Linker manual for details.

    Aamir Ali said:
    What exactly ram have- initial variables, what else

    If you are programming in 'C', the Compiler inserts code to do initialisation - see your Compiler manual for details

    Aamir Ali said:
    Stack is executed during run time. Only its starting address is defined in MCU

    Not sure what you mean by that?

    Different tools have different ways to allocate the stack - see your particular tool manuals for details.

  • Aamir Ali said:
    Stack is executed during run time. Only its starting address is defined in MCU

    The stack pointer, which is pointing at the 'current top of stack' is initialized by the C startup code (usually to the address right after end of memory, so the first element written to stack appears at top of memory). ifyou program in assembly language, you're responsible for setting the stack pointer to a suitable start address.

    However, except for very weird code, stack is never executed. Whenever something is pushed on the stack, the CPU executes something like
    memory[--stackpointer]=value;
    And when it is fetched back, something like
    value = memory[stackpointer++] is done.
    where 'memory' points to 0x0000, the start of the addressing range, and 'stackpointer' is the SP register of the CPU. And hopefully, memory[stackpointer] addresses something that is in the memory range occupied by ram - and not used for variables already (stack overflow).

    About how the memory is allocated, well, usually, variables are allocated on ram area from bottom up, while the stack is initialized to top of ram and grows down. Until the two meet (which never should happen).

    IF you have pre-initialized global variables in your C code, the init values are allocated in flash memory and copied to the proper locations in ram by the C startup code.
    initialized local variables are initialized programmatically (the function code itself sets them at start of the function)

**Attention** This is a public forum