嵌入式系统.docx
- 文档编号:24539717
- 上传时间:2023-05-28
- 格式:DOCX
- 页数:26
- 大小:22.81KB
嵌入式系统.docx
《嵌入式系统.docx》由会员分享,可在线阅读,更多相关《嵌入式系统.docx(26页珍藏版)》请在冰豆网上搜索。
嵌入式系统
EXAMPLE1(AboutLDR、LTORG)
AREALoadcon,CODE,READONLY
ENTRY
startBLfunc1
BLfunc2
stopMOVr0,#0x18
LDRr1,=0x20026
SWI0x123456
func1
LDRr0,=42
LDRr1,=0x55555555
LDRr2,=0xFFFFFFFF
MOVpc,lr
LTORG
func2
LDRr3,=0x55555555
;LDRr4,=0x66666666
MOVpc,lr
LargeTable
SPACE4200
END
EXAMPLE2(ADRADRL)
AREAadrlabel,CODE,READONLY
ENTRY
Start
BLfunc
stopMOVr0,#0x18
LDRr1,=0x20026
SWI0x123456
LTORG
funcADRr0,Start
ADRr1,DataArea
;ADRr2,DataArea+4300
ADRLr2,DataArea+4300
MOVpc,lr
DataAreaSPACE8000
END
EXAMPLE3ARMcodejumptable
AREAJump,CODE,READONLY
CODE32
numEQU2
ENTRY
start
MOVr0,#0
MOVr1,#3
MOVr2,#2
BLarithfunc
stopMOVr0,#0x18
LDRr1,=0x20026
SWI0x123456
arithfunc
CMPr0,#num
MOVHSpc,lr
ADRr3,JumpTable
LDRpc,[r3,r0,LSL#2]
JumpTable
DCDDoAdd
DCDDoSub
DoAddADDr0,r1,r2
MOVpc,lr
DoSubSUBr0,r1,r2
MOVpc,lr
END
Example4Stringcopy
AREAStrCopy,CODE,READONLY
ENTRY
startLDRr1,=srcstr
LDRr0,=dststr
BLstrcopy
stopMOVr0,#0x18
LDRr1,=0x20026
SWI0x123456
strcopy
LDRBr2,[r1],#1
STRBr2,[r0],#1
CMPr2,#0
BNEstrcopy
MOVpc,lr
;PACE4800
AREAStrings,DATA,READWRITE
srcstrDCB"Firststring-source",0
dststrDCB"Secondstring-destination",0
END
Example5Blockcopy
AREAWord,CODE,READONLY
numEQU20
ENTRY
start
LDRr0,=src
LDRr1,=dst
MOVr2,#num
wordcopyLDRr3,[r0],#4
STRr3,[r1],#4
SUBSr2,r2,#1
BNEwordcopy
stopMOVr0,#0x18
LDRr1,=0x20026
SWI0x123456
AREABlockData,DATA,READWRITE
srcDCD1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,1,2,3,4
dstDCD0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
END
AREABlock,CODE,READONLY
numEQU20
ENTRY
start
LDRr0,=src
LDRr1,=dst
MOVr2,#num
MOVsp,#0x400
blockcopyMOVSr3,r2,LSR#3
BEQcopywords
STMFDsp!
{r4-r11}
octcopyLDMIAr0!
{r4-r11}
STMIAr1!
{r4-r11}
SUBSr3,r3,#1
BNEoctcopy
LDMFDsp!
{r4-r11}
copywordsANDSr2,r2,#7
BEQstop
wordcopyLDRr3,[r0],#4
STRr3,[r1],#4
SUBSr2,r2,#1
BNEwordcopy
stopMOVr0,#0x18
LDRr1,=0x20026
SWI0x123456
AREABlockData,DATA,READWRITE
srcDCD1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,1,2,3,4
dstDCD0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
END
MACRO
$labelTestAndBranch$dest,$reg,$cc
$labelCMP$reg,#0
B$cc$dest
MEND
testTestAndBranchNonZero,r0,NE
...
...
NonZero
Aftersubstitutionthisbecomes:
testCMPr0,#0
BNENonZero
...
...
NonZero
EXAMPLE6MAP&FIELD
typedefstructPoint
{
floatx,y,z;
}Point;
Pointorigin,oldloc,newloc;
Thefollowingassemblylanguagecodeisequivalenttothetypedefstatementabove:
PointBaseRNr11
MAP0,PointBase
Point_xFIELD4
Point_yFIELD4
Point_zFIELD4
Thefollowingassemblylanguagecodeallocatesspaceinmemory.ThisisequivalenttothelastlineofCcode:
originSPACE12
oldlocSPACE12
newlocSPACE12
Youmustloadthebaseaddressofthedatastructureintothebaseregisterbeforeyoucanusethelabelsdefinedinthemap.Forexample:
LDRPointBase,=origin
MOVr0,#0
STRr0,Point_x
MOVr0,#2
STRr0,Point_y
MOVr0,#3
STRr0,Point_z
isequivalenttotheCcode:
origin.x=0;
origin.y=2;
origin.z=3;
Examples7
;straightforwardsubstitution
GBLSadd4ff
;
add4ffSETS"ADDr4,r4,#0xFF";setupadd4ff
$add4ff.00;invokeadd4ff
;thisproduces
ADDr4,r4,#0xFF00
;elaboratesubstitution
GBLSs1
GBLSs2
GBLSfixup
GBLAcount
;
countSETA14
s1SETS"a$$b$count";s1nowhasvaluea$b0000000E
s2SETS"abc"
fixupSETS"|xy$s2.z|";fixupnowhasvalue|xyabcz|
|C$$code|MOVr4,#16;butthelabelhereisC$$code
C与汇编的混合编程
TheARMC++compilerssupporttheasmsyntaxproposedintheANSIC++Standard,withtherestrictionthatthestringliteralmustbeasinglestring.Forexample:
asm("instruction[;instruction]");
Theinlineassemblerisinvokedwiththeassemblerspecifier.Thespecifierisfollowedbyalistofassemblerinstructionsinsidebraces.Forexample:
__asm
{
instruction[;instruction]
...
[instruction]
}
ATPCSregisters,r0-r3,r12,lr,andPSR,arecorrupted.(不可靠)
Example4-1Stringcopy
#include
voidmy_strcpy(constchar*src,char*dst)
{
intch;
__asm
{
loop:
#ifndef__thumb
//ARMversion
LDRBch,[src],#1
STRBch,[dst],#1
#else
//Thumbversion
LDRBch,[src]
ADDsrc,#1
STRBch,[dst]
ADDdst,#1
#endif
CMPch,#0
BNEloop
}
}
intmain(void)
{
constchar*a="Helloworld!
";
charb[20];
my_strcpy(a,b);
printf("Originalstring:
'%s'\n",a);
printf("Copiedstring:
'%s'\n",b);
return0;
}
Labels
CandC++labelscanbeusedininlineassemblerstatements.CandC++labelscanbebranchedtobybranchinstructionsonlyintheform:
B{cond}label
YoucannotbranchtoCorC++labelsusingBL.
Storagedeclarations
AllstoragecanbedeclaredinCorC++andpassedtotheinlineassemblerusingvariables.Therefore,thestoragedeclarationsthataresupportedbyarmasmarenotimplemented.
SWIandBLinstructions
SWIandBLinstructionsmustspecifyexactlythecallingstandardused.Threeoptionalregisterlistsfollowthenormalinstructionfields.Theregisterlistsspecify:
●Theregistersthataretheinputparameters
●Theregistersthatareoutputparametersafterreturn
●Theregistersthatarecorruptedbythecalledfunction.
Forexample:
SWI{cond}swi_num,{input_regs},{output_regs},{corrupted_regs}
BL{cond}function,{input_regs},{output_regs},{corrupted_regs}
Usage
Thefollowingpointsapplytousinginlineassemblylanguage:
Dommaisusedasaseparatorinassemblylanguage,soCexpressionswiththecommaoperatormustbeenclosedinparenthesestodistinguishthem:
__asm{ADDx,y,(f(),z)}
Ifyouareusingphysicalregisters,youmustensurethatthecompilerdoesnotcorruptthemwhenevaluatingexpressions.Forexample:
__asm
{
MOVr0,x
ADDy,r0,x/y//(x/y)overwritesr0withtheresult.
}
Becausethecompilerusesafunctioncalltoevaluatex/y,it:
●corruptsr2,r3,ip,andlr
●updatestheNZCVflagsintheCPSR
●altersr0andr1withthedividendandmodulo.
Thevalueinr0islost.YoucanworkaroundthisbyusingaCvariableinsteadofr0:
movvar,x
addy,var,x/y
Thecompilercandetectthecorruptioninmanycases,forexamplewhenitrequiresatemporaryregisterandtheregisterisalreadyinuse:
__asm
{
MOVip,#3
ADDSx,x,#0x12345678//thisinstructionisexpanded
ORRx,x,ip
}
ThecompilerusesipasatemporaryregisterwhenitexpandstheADDinstruction,andcorruptsthevalue3inip.Anerrormessageisissued.
稤onotusephysicalregisterstoaddressvariables,evenwhenitseemsobviousthataspecificvariableismappedontoaspecificregister.Ifthecompilerdetectsthisiteithergeneratesanerrormessageorputsthevariableintoanotherregistertoavoidconflicts:
intbad_f(intx)//xinr0
{
__asm
{
ADDr0,r0,#1//wronglyassertsthatxisstillinr0
}
returnx;//xinr0
}
Thiscodereturnsxunaltered.Thecompilerassumesthatxandr0aretwodifferentvariables,despitethefactthatxisallocatedtor0onbothfunctionentryandfunctionexit.Astheassemblylanguagecodedoesnotdoanythinguseful,itisoptimizedaway.Theinstructionshouldbewrittenas:
ADDx,x,#1
Donotsaveandrestorephysicalregistersthatareusedbyaninlineassembler.Thecompilerwilldothisforyou.IfphysicalregistersotherthanCPSRandSPSRarereadwithoutbeingwrittento,anerrormessageisissued.Forexample:
intf(intx)
{
__asm
{
STMFDsp!
{r0}//saver0-illegal:
readbeforewrite
ADDr0,x,1
EORx,r0,x
LDMFDsp!
{r0}//restorer0-notneeded.
}
returnx;
}
4.2AccessingCglobalvariablesfromassemblycode
Globalvariablescanonlybeaccessedindirectly,throughtheiraddress.Toaccessaglobalvariable,usetheIMPORTdirectivetoimporttheglobalandthenloadtheaddressintoaregister.Youcanaccessthevariablewithloadandstoreinstructions,dependingonitstype.
Forunsignedvariablesuse:
●LDRB/STRBforchar
●LDRH/STRHforshort(UsetwoLDRB/STRBinstructionsforArchitecture3)
●LDR/STRforint.
Forsignedvariables,usetheequivalentsignedinstruction,suchasLDRSBandLDRSH.
SmallstructuresoflessthaneightwordscanbeaccessedasawholeusingtheLDMandSTMinstructions.Individualmembersofstructurescanbeaccessedbyaloadorstoreinstructionoftheappropriatetype.Youmustknowtheoffsetofamemberfromthestartofthestructureinordertoaccessit.
Example4-6loadstheaddressoftheintegerglobalglobvarintor1,loadsthevaluecontainedinthataddressintor0,adds2toit,thenstoresthenewvaluebackintoglobvar.
Example4-6Addressofglobal
AREAglobals,CODE,READONLY
EXPORTasmsubroutine
IMPORTglobvar
asmsubroutine
LDRr1,=globvar;readaddressofglobvarinto
;r1fromliteralpool
LDRr0,[r1]
ADDr0,r0,#2
STRr0,[r1]
MOVpc,lr
END
CallingassemblylanguagefromC
Example4-9andExample4-10showaCprogramthatusesacalltoanassemblylanguagesubroutinetocopyonestringoverthetopofanotherstring.
Example4-9CallingassemblylanguagefromC
#include
externvoidstrcopy(char*d,constchar*s);
intmain()
{constchar*srcstr="Firststring-source";
chardststr[]="Secondstring-destination";
/*dststrisanarraysincewe'regoingtochangeit*/
printf("Beforecopy
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 嵌入式 系统
![提示](https://static.bdocx.com/images/bang_tan.gif)