你的位置:CGV中文网 > Blockster中文网 > keil c51中C程序的启动过程
keil c51中C程序的启动过程
- 发布日期:2025-01-04 15:50 点击次数:66 汇编是从org 0000h开始启动,那么keil c51是如何启动main()函数的?keil c51有一个启动程序startup.a51,它总是和c程序一起编译和链接。下面看看它和main()函数是如何编译的;//主函数如下;void main(void){while (1)这是个无条件空循环。{}}把上面的main()函数编译后的汇编程序和反汇编代码整理后对照如下;?C_C51STARTUPSEGMENTCODE?PR?main?TESTMAINSEGMENT CODE?STACKSEGMENTIDATARSEG?STACKDS1CSEGAT0?C_STARTUP:LJMPSTARTUP1C:0x0000020003LJMPSTARTUP1(C:0003)RSEG?C_C51STARTUPSTARTUP1:;该段程序把内存清零;MOVR0,#IDATALEN - 1C:0x0003787FMOVR0,#0x7F;CLRAC:0x0005E4CLRA;MOV@R0,AIDATALOOP:C:0x0006F6MOV@R0,A;DJNZR0,IDATALOOPC:0x0007D8FDDJNZR0,IDATALOOP(C:0006);MOVSP,#?STACK-1;设制CPU的堆栈起始地址C:0x0009758107MOVSP(0x81),#0x07;LJMP?C_STARTC:0x000C02000FLJMPmain(C:000F)RSEG?PR?main?TESTMAINmain:;void main(void)C:0x000F80FESJMPmain(C:000F);main()函数现在分析上面的汇编程序就会明白c51程序是如何启动的。该程序有三个代码段;第一个代码段?C_STARTUP在0x0000地址,是CPU第一条指令的入口,它只有一条长跳转指令,直接跳到第二个代码段.第二个代码段?C_C51STARTUP是可重定位的段,该程序把内存清零,然后再设置CPU的堆栈,最后跳转到main()函数.第三个代码段就是main()函数,在keil c51编译器里main()的段地址名就是?C_START。还有一个IDATA数据段?STACK就是堆栈,?STACK用于设制CPU的堆栈起始地址,这是由keil编译器自动完成的。/*******************************************************************/keil c51函数的返回值是存储在r0-r7中的。多字节变量在存储器里都是低地址存高位,高地址存低位。main()函数的局部变量都是放在存储器里的,不象别的函数先选寄存器r0-r7存放,如果不够用再存入存储器里。看下面的示例;c51程序;unsigned int SumXY(unsigned int X,Y);void main(void){unsigned int a,b,c;a=0x5500;b=0xaa;while (1){c=SumXY(a,b);}}unsigned int SumXY(unsigned int X,Y){unsigned int Z;Z=X+Y;return Z;}编译后的反汇编代码列表;C:0x0000020027LJMPSTARTUP1(C:0027)4: void main(void)5: {unsigned int a,b,c;6:a=0x5500;C:0x0003750855MOV0x08,#0x55;ram地址0x08和0x09存放变量a=0x5500。C:0x0006750900MOV0x09,#0x007:b=0xaa;C:0x0009750A00MOV0x0A,#0x00;ram地址0x0A和0x0B存放变量b=0x00AA。C:0x000C750BAAMOV0x0B,#0xAA8:while (1)9:{10:c=SumXY(a,b);C:0x000FAD0BMOVR5,0x0B;寄存器R4和R5传递变量a的值。C:0x0011AC0AMOVR4,0x0AC:0x0013AF09MOVR7,0x09;寄存器R6和R7传递变量b的值。C:0x0015AE08MOVR6,0x08C:0x0017120020LCALLSumXY(C:0020);调用函数SumXY(a,b)求c=a+bC:0x001A8E0CMOV0x0C,R6;函数SumXY(a,b)返回的整型值存在R6和R7里,C:0x001C8F0DMOV0x0D,R7;把返回值存入变量c,ram地址0x0C和0x0D存放变量c11:}12: }13:C:0x001E80EFSJMPC:000F14: unsigned int SumXY(unsigned int X,Y)15: {unsigned int Z;16:Z=X+Y;C:0x0020EFMOVA,R7;参数变量X放在寄存器R6和R7里C:0x00212DADDA,R5;参数变量Y放在寄存器R4和R5里C:0x0022FFMOVR7,AC:0x0023EEMOVA,R6C:0x00243CADDCA,R4;计算Z=X+Y;C:0x0025FEMOVR6,A;局部变量Z也放在寄存器R6和R7里17:return Z;;由寄存器R6和R7里返回函数的值C:0x002622RET151:MOVSP,#?STACK-1152: ; This code is required if you use L51_BANK.A51 with Banking Mode 4153: ; EXTRN CODE (?B_SWITCH0)154: ;CALL?B_SWITCH0; init bank mechanism to code bank 0C:0x002775810DMOVSP(0x81),#0x0D155:LJMP?C_STARTC:0x002A020003LJMPmain(C:0003)
相关资讯