[英]ARM Assembly - Monitor Stack Pointer
I am trying to monitor the stack pointer in C on an embedded ARM MCU. 我试图在嵌入式ARM MCU上监视C中的堆栈指针。 As the project grows, I'd like to know how much space is left and map more if necessary.
随着项目的发展,我想知道剩下多少空间,并在必要时绘制更多空间。
I already know the start and top address of the stack from the map file generated when compiled. 我已经从编译时生成的映射文件中知道了堆栈的起始和顶部地址。
I would like to get the address the sp is pointing to so I can do some basic math and monitor the stack on a percentage used basis, but I am new to ARM and don't understand why my assembly is not working. 我想得到sp指向的地址,所以我可以做一些基本的数学运算并在百分比使用的基础上监控堆栈,但我是ARM的新手并且不明白为什么我的程序集不起作用。
This is what I am doing: 这就是我在做的事情:
int stackptr;
asm
{
LDR r0, =stackptr // put address of C variable stackptr in r0
MOV r1, sp // move value of sp to r1
STR r1, [r0] // put value of r1 in address contained in r0 (stackptr)
}
// math using stackptr...
If I look at the address stored in stackptr, its right at the start of the stack and its not changing (I am calling this every 100ms). 如果我查看存储在stackptr中的地址,它就在堆栈的开头并且没有改变(我每100ms调用一次)。 I expected this to be bouncing around somewhere in the middle of my stack.
我预计这会在我的堆栈中间的某个地方弹跳。
Also, if I try to get the address of the stack base and limit (using the same method but with sb and sl), I just get 0's. 此外,如果我尝试获取堆栈基地的地址并限制(使用相同的方法,但使用sb和sl),我只得到0。 I am not as concerned about this since from my research it seems they are not always used.
我并不关心这一点,因为从我的研究来看,它似乎并不总是被使用。
Thanks for the help 谢谢您的帮助
Try making stackptr
volatile
, since compiler can think it is not getting updated and use stale copy in a register or completely optimize away access to it. 尝试使
stackptr
volatile
,因为编译器可以认为它没有得到更新并在寄存器中使用过时副本或完全优化对它的访问。
Another approach to your problem might be to ask compiler to calculate possible stack usage and create a report. 解决问题的另一种方法可能是要求编译器计算可能的堆栈使用情况并创建报告。 For example with GCC you can use -fstack-usage and -Wstack-usage flags.
例如,使用GCC,您可以使用-fstack-usage和-Wstack-usage标志。
-fstack-usage
creates a .su
file with the same name, reporting stack usage of each function. -fstack-usage
创建一个具有相同名称的.su
文件,报告每个函数的堆栈使用情况。 -Wstack-usage=X
warns if a function requires more stack than what's passed in. Of course this is static analysis so compiler can't handle all the cases (but generate warnings if so). -Wstack-usage=X
警告函数是否需要更多的堆栈而不是传入的内容。当然这是静态分析,因此编译器无法处理所有情况(但如果是这样则会生成警告)。 I would also expect any commercial compiler to have such functionality if you are not using GCC. 如果你不使用GCC,我也希望任何商业编译器都有这样的功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.