惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Security @ Cisco Blogs
T
Threat Research - Cisco Blogs
TaoSecurity Blog
TaoSecurity Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
腾讯CDC
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Blog of Author Tim Ferriss
Microsoft Azure Blog
Microsoft Azure Blog
罗磊的独立博客
F
Full Disclosure
博客园 - 【当耐特】
C
CERT Recently Published Vulnerability Notes
Engineering at Meta
Engineering at Meta
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Threatpost
I
Intezer
V2EX - 技术
V2EX - 技术
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The Hacker News
The Hacker News
小众软件
小众软件
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
N
News | PayPal Newsroom
MyScale Blog
MyScale Blog
AI
AI
Vercel News
Vercel News
Spread Privacy
Spread Privacy
美团技术团队
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
V
Vulnerabilities – Threatpost
Schneier on Security
Schneier on Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
Help Net Security
Help Net Security
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
L
LINUX DO - 热门话题
U
Unit 42
L
LangChain Blog
Recent Announcements
Recent Announcements

博客园 - xlander

跟实际生活相关的饭否应用 5个跟实际生活相关的Twitter应用 Google Code上两个LPC21XX开源项目 Router牌音箱 基于ARM的网络收音机 博客程序换成了Z-Blog的 音箱?储钱罐? 显式指针转换的教训 使用结构体中成员变量指针的教训 结构体中位段、字节对齐并用的教训 C语言的跨编译器编译 日本某商场里的智能瀑布 救活TrackBack的偏方 近期几个开源的嵌入式产品 几个自制心电图的网络资源 哇哦级的贴片焊接教学视频 自动五线谱识别及音乐合成装置 简易指纹识别授权访问系统 PIC18F2550实现的基于HID的USB示波器
ADS下C语言中局部变量的存储位置分配
xlander · 2008-08-12 · via 博客园 - xlander

做研发很多年了,起初开发的软件都是在台式机上运行的,台式机上丰富的资源从来没让我仔细的思考过有关变量的分配问题。做嵌入式系统的开发是最近两年的事儿,最近系统总是出现莫名其妙的错误,开始关注有关堆栈溢出的问题,特意考察一下ADS下C语言局部变量的分配问题。

按照一般教科书上的说法,C语言中的局部变量应该是分配在"栈"中的。而实际情况,有些出入,看看我下面的调试纪录,肯能更容易理解。

这是我写的一段代码,唯一的用途,就是分配变量。

int func1(void)
{
int father;
int mother;
int boy;
int girl;
father = 30;
mother = boy = girl = father;
return father;
}

int func2(void)
{
int father;
int mother;
int boy;
int girl;
int unnecessary;
father = 30;
mother = boy = girl = father;
unnecessary = 0;
return father;
}

int func3(void)
{
int stone[2];
stone[0] = 30;
return stone[0];
}

int func4(void)
{
int stone[2];
stone[0] = 30;
if (stone[0] == 30)
{
int father;
father = 91;
}
else
{
int mother;
mother = 90;
}
return stone[0];
}

int func5(void)
{
int stone[2];
stone[0] = 30;
if (stone[0] == 30)
{
int boy[2];
boy[0] = 91;
}
else
{
int girl[2];
girl[0] = 90;
}
return stone[0];
}

int func10(int a, int b, int c, int d)
{
return a + b + c + d;
}

int func11(int a, int b, int c, int d)
{
int father = a;
int mother = b;
int boy = c;
int girl = d;
return father + mother + boy + girl;
}

typedef struct Home
{
int father;
int mother;
} THome;
int func12()
{
THome home;
home.father= 12;
home.mother = 12;
return home.father + home.mother;
}

typedef int uint32;
int func13()
{
uint32 home = 2;
home *= 2;
return home;
}

int main(void)
{
func1();
func2();
func3();
func4();
func5();

func10(1,2,3,4);
func11(1,2,3,4);
func12();
func13();
}

通常,ADS编译的代码使用R13作为堆栈指针,也就是SP。

先看看刚进入main()函数的时候,R13=0x08000000。

单步执行一步后,R13=0x07FFFFC。减少了4字节,PC入栈引起。

进入fun1()后,R13=0x07FFFFC。没有变化,说明这几个变量没有入栈,实际上他们分别分配在R0-R3。

进入fun2()后,R13=0x07FFFF8。比0x07FFFFC少4字节,前4个仍然分配在R0-R3,第5个变量入栈。

进入fun3()后,R13=0x07FFFF0。比0x07FFFFC少12字节,除了数组入栈外,还有PC。

进入fun4()后,R13=0x07FFFF0。跟func4()一样,数组和PC入栈,分支中的变量放在R0中。

进入fun5()后,R13=0x07FFFE8。比fun4()少8字节,说明分支中的数组也入栈了。

进入fun10()后,R13=0x07FFFFC。4个函数形参也是分配在R0-R3。

进入fun11()后,R13=0x07FFFEC。比0x07FFFFC少16字节,4个形参仍然分配在R0-R3,另外4个变量入栈。

进入fun12()后,R13=0x07FFFF0。跟func4()一样,结构体变量也是入栈的。

进入fun13()后,R13=0x07FFFFC。没有变化,char、int这些变量即使经过typedef,其处理方法仍然不变。

Copyright © 2008

继续阅读《ADS下C语言中局部变量的存储位置分配》的全文内容...

分类: 软件开发 | Tags: ARM  C  局部变量   | 添加评论(0)

相关文章: