您好,欢迎访问三七文档
当前位置:首页 > 建筑/环境 > 工程监理 > KEILC51集成开发环境C和汇编语言的相互调用_严天峰
27 120082JournalofLanzhouJiaotongUniversityVol.27No.1Feb.2008 :1001-4373(2008)01-0131-04KEILC51C*严天峰, 王耀琦(, 730070) :目前C语言已成为开发单片机项目的主要工具,但一些特殊的应用场合仍然需要汇编语言编写程序,如编写时序要求非常严格的接口协议时,这必然涉及到C与汇编的相互调用,即混合编程的问题.详细介绍了KEILC51环境下的C和汇编语言相互调用的方法和原则,并具体说明混合编程的细节和应注意的问题.:KEILC51汇编调用混合编程:TP311.11 :A ,C,,,.KEILWINDOWS51μVision,.C,.,.,,C,;C,C.C,.C,C51,.1 C51C51C51.,C51.,C,C51,.C51,?PR??,,;,,.SMALL,1[1].1 Tab.1 NamingprincipleoflocalsegmentCODE?PR??DATA?DT??BIT?BI?? .,example“func”,”?PR?func?example,func().funcDATABIT,?func?BYTE?func?BIT,.C51,,C,,.2[1].*:2007-09-13:(1970-),,,272 Tab.2 ConversionruleofthefunctionnameCvoidfunc(void)Func,.Func()func,“”.Func(void)reentrant?func,“-?”,. funcC,.//C:void(unsignedchara){bitc;c=0;}(EXAMPLE.SRC):NAMEEXAMPLE;?PR?func?EXAMPLESEGMENTCODE;?DT?func?EXAMPLESEGMENTDATA;?BI?func?EXAMPLESEGMENTBIT;PUBLICfunc;PUBLIC,RSEG?DT?func?EXAMPLE;?func?BYTE:;a?040:DS1;a?040RSEG?BI?func?EXAMPLE;?func?BIT:;c?041:DBIT1;a?041BITvoidfunc(unsignedchara)RSEG?PR?func?EXAMPLE;func:;MOVa?040,R7;R7a?040;{;bitc;;c=0;CLRc?041;}RETEND2 C.,.#pragmaNOREG-PARMS,.3[2].3 Tab.3 PassingrulesofregisterparametersCharIntLong,float1R7R7,R6R4~R7R1,R2,R32R5R5,R4R4~R7R1,R2,R33R3R3,R2R1,R2,R3 func(*p,unsignedchara,un-signedcharb),“p”R1,R2,R3,R1,R2.“a”R7,“b”R5.EXAMPLE.SRC,R7“a”a?040.C,4[2].4 Tab.4 ParameterpassingoffunctionreturnedvalueBitCyCyUnsignedcharR7R7UnsignedintR7~R6R6,R7()LongintR4~R7R4,R5,R6,R7()FloatR4~R7IEEER1~R3R3,R2,R11321:KEILC51C3 C513.1 C51C51ADC0809C51.C,”extern”;“PUBLIC”.C51:#includereg51.h#includestdio.h#defineucharunsignedcharexternucharad0809(uchar*p);//ad0809,//externdelasm(uchartime);//delasmmain(){uchardataadcdata[8];//ADC08098adcdatawhile(1)ad0809(adcdata);//ad0809,delasm(2);//delasm}AD0809A/D:EOCEQUP1.4NAMEADC0809;ADC0809?PR?AD0809?ADC0809SEGMENTCODE;,AD0809,““PUBLICAD0809;“PUBLIC”RSEG?PR?AD0809?ADC0809;AD0809AD0809:MOVA,R1;R1adcdata[8]MOVR0,AMOVR4,#00HMOVR7,#8MOVDPTR,#0FDF8HSAM:MOVA,R4MOVX@DPTR,AJBEOC,MOVXA,@DPTRMOV@R0,AINCDPTRINCR0INCR4DJNZR7,SAMRETEND,C,AD0809,externucharad0809(uchar*p).ad0809(adcdata),ad0809,adcdata(),R1.AD0809,R1R0,R08ADC0809,,adcdata[8]A/D.ad0809(uchar*p)p,R1、R2、R3;uchardata*p,pdata,3,R7,.3.2 CC.AFUNC.ASM60H63Hint,count.c2,4,://:#includestdio.hexternvoidafunc(void);afuncvoidmain(void){afunc();};AFUNC.ASM:NAMEAFUNC;?PR?afunc?AFUNCSEGMENTCODE;EXTRNCODE(count);count.cPUBLICafuncRSEG?PR?afunc?AFUNC;afunc:USING0MOV60h,#23h;60H63H,60H13327MOV61h,#34hMOV62h,#34h;62HMOV63h,#62hLCALLcount;count.cMOV60h,r7;R4-R7,R7MOV61h,r6MOV62h,r5MOV63h,r4AJMP;END//Ccount.c#includereg51.h#defineuintunsignedint#defineulongunsignedlonguintdatavalue[2]at0x60//value[2]60Hulongcount(uinta1,uinta2){uintb1,b2;ulongresult,result1;b1=result[0];b2=result[1];result=(ulong)a1*(ulong)a2;//result1=(ulong)b1*(ulong)b2;return(result);}//3.3 CC,,C.#pragmaasm#pragmaendasm,:“projectoptionsforgroupsourcegroup”“generateassem-blerSRCfile”“AssembleSRCfile”.,,“SMALL”,KEILC51LIBC51S.LIB,.#includereg51.hvoidmain(){unsignedi;#pragmaasmMOVA,#01hMOVP1,ARLAMOVP1,A#pragmaendasmi=20;},#pragmaasmC,C.KEILC516.20.4 ,,C,C、,,.:[1] Cx51compileruser'sguide:optimizingCcompilerandlibraryreferenceforclassicandextended8051micro-controllers[Z].KeilSoftware,Inc.2000.[2] .C[M].,2003.[3] .C[M].:,2003.CallinganAssemblyRoutinefromCPrograminKEILC51CompilerYANTian-feng, WANGYao-qi(SchoolofElectronicsandInformationEngineering,LanzhouJiaotongUniversity,Lanzhou730070,China)Abstract:Inrecentyears,Clanguagehasbecometodevelopthemaintoolofdevelopmentmicrocontrollers,butinsomespecialapplicationstheassemblylanguageprogrammingisstillneeded.Forexample,Writingsomeinterfaceprotocolsthatrequestverystricttimesequence.ItmustinvolvecallingassemblyroutinefromCprogram.Thatismixedprogrammingproblems.ThemethodsandprinciplesofCandassemblylan-guagecallingeachotherinKEILC51compilerareintroducedindetail.Furthermore,thespecificationofmixedprogrammingandtheissueswhichshouldbepaidmoreattentionareelaboratedthroughthreespe-cificexamples.Keywords:KEILC51;assembly;calling;mixedprogramming134
本文标题:KEILC51集成开发环境C和汇编语言的相互调用_严天峰
链接地址:https://www.777doc.com/doc-4419236 .html