c - 哪个是初始堆栈指针的正确值?

标签 c gcc initialization stack arm

我正在使用 ARM-GCC 编译器,我在 Internet 上找到了 startup_stm32f10x_cl.c 文件(启动代码)的两个版本。处理器是:STM32F105RC(ARM Cortex M3)。

公共(public)部分:

#define STACK_SIZE       0x100 /*!< The Stack size */
__attribute__ ((section(".co_stack")))
unsigned long pulStack[STACK_SIZE];    

然后,第一个版本像这样启动 vector 表:

void (* const g_pfnVectors[])(void) =
{       
  (void *)&pulStack[STACK_SIZE],     /*!< The initial stack pointer           */
  Reset_Handler,                /*!< Reset Handler                            */
...

第二个版本是这样的:

void (* const g_pfnVectors[])(void) =
{       
  (void *)&pulStack[STACK_SIZE - 1],     /*!< The initial stack pointer           */
  Reset_Handler,                /*!< Reset Handler                            */
    ...

所以,我的问题是:
哪一个是正确的堆栈指针初始化?

最佳答案

来自 M3 内核指令集的 ARM 文档:

PUSH uses the value in the SP register minus four as the highest memory address

On completion, PUSH updates the SP register to point to the location of the lowest stored value

因此,我的解释是 SP 的起点必须是最高地址的 +4,即紧接在 Stack 数组边界之后的地址。 因此

(void *)&pulStack[STACK_SIZE]

看起来不错,因为该地址(虽然不是数组的一部分)永远不会被使用。

关于c - 哪个是初始堆栈指针的正确值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33278646/

相关文章:

c++ - 此类代码中 gcc 和 clang 之间的不同行为

c# - Spring.Net 中的多个上下文

c - 如何将struct作为数组传输?

c - sbrk, malloc - 可以分配的最大内存

C - 为什么只有第二个命令有效?

c - 更多 GCC 链接时间问题 : undefined reference to main

c++ -/usr/bin/ld : build-debug/obj/gpsitem. o: 文件无法识别:文件被截断

连接来自 char 输入的字符串

swift - 如何为字符串扩展添加自定义初始化?

java - 无法访问 apache 模块源代码中系统定义的环境变量