C51: CALCULATING STACK SIZE

本文介绍了如何确定C51程序的最大栈大小,包括使用μVision模拟器获取最大栈指针值的方法,以及在不具备模拟器的情况下手动计算栈大小的技术方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C51: CALCULATING STACK SIZE


QUESTION


How can I determine the maximum stack size of my program?


ANSWER


There is no automatic way the tools can tell you the maximum stack depth or stack utilization. Because of asynchronous events and interrupts, the automation is too difficult.


However, you can use the μVision Simulator which provides the sp_max value in the register window. When you execute all features of your application, this gives you the maximum sp value that has been reached by your application. Make sure that your assumptions about the available stack are correct. Some 8051 variants have just 128 bytes on-chip RAM and therefore the stack space ends at 0x7F.


You should have more bytes available for executed interrupts. As a guideline, there should be enough space to execute the most complex interrupt of your application.


If you cannot use the μVision Simulator, use the following method, which works in any debugging tool. You may use the Monitor, the ISD51 In-system debugger, or any in-circuit emulator. The results should be similar.


Locate the beginning of the stack. If you use the C51 compiler, look for the ?STACK symbol. that is listed in the linker map file (*.M51 or *.MAP)


Fill the stack area with a known value or string. Something like “STACK—”, since it’s 8 characters long, displays nicely in a memory dump.


Run your program and make sure you execute every line of code (or as much as makes sense). Make sure you generate interrupts so that they are included in the profile. If your emulator has a code coverage feature, use it to make sure that every path is taken. You can do this with dScope’s code coverage feature.


After your program runs a while, stop it and look at the stack area. You should see garbage for the part of the stack that has been used and the “STACK—” strings in the remainder of the stack. Count the number of complete strings, multiply by 8 (since “STACK—” is 8 bytes long), and you have the number of bytes of remaining stack space.


通过C51: CALCULATING STACK SIZE.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值