启动代码有如下的宏定义:
MACRO
$HandlerLabel HANDLER $HandleLabel
$HandlerLabel
-
;下面一句一句分析一下,为了便于分析,假设sp = 0x33ff8000, $HandleLabel = 0x33ffff00,
- ;[0x33ffff00]=0x10000000,r0 =0x56001234:
- ;$HandlerLabel HANDLER $HandleLabel
- ;宏的名字叫HANDLER ,有两个参数
- ;$HandlerLabel 定义一个标号
- ; sub sp,sp,#4 ;decrement sp(to store jump address)
- ;把栈顶指针减4,留出一个字的空间(用于保存跳转地址的值),sp=0x33ff7ffc
- ; stmfd sp!,{r0} ;PUSH the work register to stack(lr does not push because it return to original address)
- ;首先把sp减4 (sp=0x33ff7ff8),然后把将要使用的r0寄存器入栈,此时[0x33ff7ff8]=0x56001234
- ; ldr r0,=$HandleLabel;load the address of HandleXXX to r0
- ;给寄存器r0赋值,r0=0x33ffff00
- ; ldr r0,[r0] ;load the contents(service routine start address) of HandleXXX
- ;给寄存器r0赋值,r0=0x10000000
- ; str r0,[sp,#4] ;store the contents(ISR) of HandleXXX to stack
- ;把寄存器r0保存到0x33ff7ffc (0x33ff7ff8+4),sp没有改变sp=0x33ff7ff8,如果
- ;str r0,[sp,#4]!sp 改变
- ;此时
- ;[0x33ff7ffc] = 0x10000000
- ; ldmfd sp!,{r0,pc} ;POP the work register and pc(jump to ISR)
- ;把栈顶的两个字弹出,分别保存到r0、pc,此时sp=0x33ff8000,r0=0x56001234,pc=0x10000000,
- ;通过比较不难发现,sp和r0在执行前后都没有变化,程序就跳转到0x10000000处执行
- ; MEND
- ;所以,通过上面的分析可以看出,$HandlerLabel HANDLER $HandleLabel是让PC跳转到$HandleLabel中存放的地址执行。
- ;==================================================================================================================