0
votes

In a course I am taking about embedded systems there are certain statements which lack a deep explanation which has left me confused at some points. I would be grateful if someone can offer me clarifications.

  1. I have been told that, if there are initialized variables, their initialization values are stored in the code segment (may be in flash) and are loaded (may be to RAM) by startup routines before running the program. This make sense to me considering global variables as they are allocated to .data section. I presume that global variables have a fixed address for the entire program and the initialization value is loaded to a specific address location(please correct me if I am wrong). Now, how is this done for local variables considering that they don't have a fixed address location on stack? Considering that local variables come to existence only during function execution, how do they get initialized each time the function is invoked?

  2. Also, The instructor says, "The stack is reserved at compile time and the data is allocated at runtime by pre-compiled instructions". Can someone please make me understand the latter half of this statement?

1
The point of a stack is to "stack" function calls so that it's a LIFO structure. Every time you call a function, part of loading this function call to the stack is loading the local variables of this call to the stack. The local variable addresses are not fixed in the sense that they only exist during the lifetime of the specific function call and should only be accessed within that function context (accessed using the stack pointer). The amount of stack space your program has is OS dependent and sometimes can be changed (i.e. Java's stack size setting). - wxz
You can do same step to copy initialization data when you enter a function as well. Same mechanism, different piece of data. Nothing magic about that. If you recursively enter same function again, the same data is copied again ono another area on the stack. - Gerhardh
As for "the data is allocated"...I assume this means local variables are allocated at runtime. When you run a program that's been compiled, all instructions are "pre-compiled instructions" so it seems like an unnecessary clarification for your instructor to add. - wxz
@wxz Thanks for your comment. I was referring from the context of embedded systems, especially bare metal. Stack saves not just local variables but also input parameters passed, return data, register values etc and it all makes sense to me. However, I wasn't clear how are the local variables initialized considering that their initialization values are present in flash. Since fetching data from flash in runtime doesn't sound practical, I wanted to understand how exactly it is done. - Learner

1 Answers

0
votes

Your understanding of static variables the the .data section is correct. You may also want to consider zero-initialized static variables in the .bss section. These are initialized at the same time as those in the .data section, but their initial value does not need to be stored because it is zero.

Automatic variables may be on the stack or may be optimized to only be in processor registers. Either way, code is generated by the compiler to initialize them each time the function using them is called. If they are on the stack then this will include an instruction to adjust the stack pointer to "allocate" space for them when they are needed and "free" them when they go out of context.

The space for the entire stack is usually allocated in the linker script. In an embedded microcontroller system no instructions are necessary to "allocate" it. Depending on the hardware there may be code required to enable access to external memory, but in most cases there is a bank of fast SRAM ready to use as soon as the system powers on, and the first stack will be in this.