一、目的
本文分析ARMCC编译器编译出来的文件对应的汇编代码,旨在帮助C语言程序员写嵌入式代码时分析堆栈使用问题和优化C语言代码。
二、材料
1、工具
编译器:Keil5 ARMCC
反编译工具:fromelf.exe
2、c语言源码
struct LG_BaseLockFuncs_t;
struct DynMem_Init_t
{
void * pool; //point to start address of dynamic memory
size_t size; //size of dynamic memory
int nMemAlign; //memory align
const struct LG_BaseLockFuncs_t *pLockCBs; //Callback functions to lock the heap for multi-threads.
void *pLock; //parameter which will be passed to 'pLockCBs'.
};
extern uint8_t ImageRW_IRAM1ZILimit[];
static void meminit(void)
{
struct DynMem_Init_t dmInit;
static uint32_t irq_flag;
static const struct LG_BaseLockFuncs_t irq_lockcbs = {IRQ_Disable, IRQ_Enable};
dmInit.nMemAlign = 8;
dmInit.pool = ImageRW_IRAM1ZILimit;
dmInit.size = 0x20000000 + 0x5000 - (int)dmInit.pool;
dmInit.pLock = &irq_flag;
dmInit.pLockCBs = &irq_lockcbs;
DynMem_Init(&dmInit);
}
3、步骤
3.1 用keil对源码进行编译,在输出目录下生成:<输出文件名>.axf
3.2 在keil安装目录下,运行命令行:l\ARM\ARMCC\bin>fromelf.exe -c <输出文件路径>.axf > a.txt
3.3 打开txt,找到要分析的函数名
三、分析汇编伪代码
1、汇编代码如下所示:
i.meminit
meminit
0x08008214: b500 .. PUSH {lr}
0x08008216: b085 .. SUB sp,sp,#0x14
0x08008218: 2008 . MOVS r0,#8
0x0800821a: 9002 .. STR r0,[sp,#8]
0x0800821c: 4807 .H LDR r0,[pc,#28] ; [0x800823c] = 0x20001220
0x0800821e: 9000 .. STR r0,[sp,#0]
0x08008220: 4907 .I LDR r1,[pc,#28] ; [0x8008240] = 0x20005000
0x08008222: 9800 .. LDR r0,[sp,#0]
0x08008224: 1a08 .. SUBS r0,r1,r0
0x08008226: 9001 .. STR r0,[sp,#4]
0x08008228: 4806 .H LDR r0,[pc,#24] ; [0x8008244] = 0x20000018
0x0800822a: 9004 .. STR r0,[sp,#0x10]
0x0800822c: