简体   繁体   English

嵌入式系统中的堆栈 memory 管理

[英]stack memory management in embedded systems

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.有人告诉我,如果有初始化变量,它们的初始化值将存储在代码段中(可能在闪存中)并在运行程序之前由启动例程加载(可能是到 RAM)。 This make sense to me considering global variables as they are allocated to.data section.考虑全局变量,因为它们被分配到 .data 部分,这对我来说是有意义的。 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?考虑到局部变量仅在 function 执行期间才存在,那么每次调用 function 时如何初始化它们?

  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?有人可以让我理解这句话的后半部分吗?

Your understanding of static variables the the .data section is correct.您对.data部分的 static 变量的理解是正确的。 You may also want to consider zero-initialized static variables in the .bss section.您可能还需要考虑.bss部分中的零初始化 static 变量。 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.它们与.data部分中的初始化同时进行,但它们的初始值不需要存储,因为它是零。

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.无论哪种方式,每次调用使用它们的 function 时,编译器都会生成代码来初始化它们。 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.如果它们在堆栈上,那么这将包括一条指令来调整堆栈指针,以便在需要它们时为它们“分配”空间,并在它们 go 脱离上下文时“释放”它们。

The space for the entire stack is usually allocated in the linker script.整个堆栈的空间通常在 linker 脚本中分配。 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.根据硬件的不同,可能需要代码来启用对外部 memory 的访问,但在大多数情况下,一旦系统上电,就有一组快速 SRAM 可供使用,第一个堆栈将在其中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM