ARM程序示例Word格式文档下载.docx
《ARM程序示例Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《ARM程序示例Word格式文档下载.docx(24页珍藏版)》请在冰豆网上搜索。
![ARM程序示例Word格式文档下载.docx](https://file1.bdocx.com/fileroot1/2022-11/16/160d1f0f-5356-494e-bb8d-7eee2e482568/160d1f0f-5356-494e-bb8d-7eee2e4825681.gif)
returna;
AREAgcd,CODE,READONLYALIGN=3
ENTRY
startCMPR0,R1
BEQstop
BLTless
SUBR0,R0,R1
Bstart
lessSUBR1,R1,R0
stopNOP
END
SUBGTR0,R0,R1
SUBLTR1,R1,R0
BNEstart
EXAMPLE1(AboutLDR、LTORG)
AREALoadcon,CODE,READONLYALIGN=3
ENTRY;
Markfirstinstructiontoexecute
startBLfunc1;
Branchtofirstsubroutine
BLfunc2;
Branchtosecondsubroutine
stopMOVr0,#0x18;
angel_SWIreason_ReportException
LDRr1,=0x20026;
ADP_Stopped_ApplicationExit
SWI0x123456;
ARMsemihostingSWI
func1
LDRr0,=42;
=>
MOVR0,#42
LDRr1,=0x55555555;
LDRR1,[PC,#offsetto
;
LiteralPool1]
LDRr2,=0xFFFFFFFF;
MVNR2,#0
MOVpc,lr
LTORG;
LiteralPool1contains
literalOx55555555
func2
LDRr3,=0x55555555;
LDRR3,[PC,#offsetto
LDRr4,=0x66666666;
Ifthisisuncommentedit
fails,becauseLiteralPool2
isoutofreach
LargeTable
SPACE4200;
Startingatthecurrentlocation,
clearsa4200byteareaofmemory
tozero
END;
LiteralPool2isempty
TheLDRpseudo-instructiongeneratesthemostefficientcodeforaspecificconstant:
iftheconstantcanbeconstructedwithaMOVorMVNinstruction,theassemblergeneratestheappropriateinstruction.
iftheconstantcannotbeconstructedwithaMOVorMVNinstruction,theassembler:
●placesthevalueinaliteralpool(aportionofmemoryembeddedinthecodetoholdconstantvalues)
●generatesanLDRinstructionwithaprogram-relativeaddressthatreadstheconstantfromtheliteralpool.
Forexample:
LDRrn,[pc,#offsettoliteralpool]
loadregisternwithoneword
fromtheaddress[pc+offset]
YoumustensurethatthereisaliteralpoolwithinrangeoftheLDRinstructiongeneratedbytheassembler.RefertoPlacingliteralpoolsformoreinformation.
Theoffsetfromthepctotheconstantmustbe:
lessthan4KBinARMstate,butcanbeineitherdirection
forwardandlessthan1KBinThumbstate.
WhenanLDRRd,=constpseudo-instructionrequirestheconstanttobeplacedinaliteralpool,theassembler:
●checksiftheconstantisavailableandaddressableinanypreviousliteralpools.Ifso,itaddressestheexistingconstant.
●attemptstoplacetheconstantinthenextliteralpoolifitisnotalreadyavailable.
Ifthenextliteralpoolisoutofrange,theassemblergeneratesanerrormessage.InthiscaseyoumustusetheLTORGdirectivetoplaceanadditionalliteralpoolinthecode.PlacetheLTORGdirectiveafterthefailedLDRpseudo-instruction,andwithin4KB(ARM)or1KB(Thumb).RefertoLTORGforadetaileddescription.
EXAMPLE2(ADRADRL)
AREAadrlabel,CODE,READONLY
Start
BLfunc;
Branchtosubroutine
Createaliteralpool
funcADRr0,Start;
SUBr0,PC,#offsettoStart
ADRr1,DataArea;
ADDr1,PC,#offsettoDataArea
ADRr2,DataArea+4300;
Thiswouldfailbecausetheoffset
cannotbeexpressedbyoperand2
ofanADD
ADRLr2,DataArea+4300;
ADDr2,PC,#offset1
ADDr2,r2,#offset2
MOVpc,lr;
Return
DataAreaSPACE8000;
clearsa8000byteareaofmemory
END
TheassemblerconvertsanADRrn,labelpseudo-instructionbygenerating:
●asingleADDorSUBinstructionthatloadstheaddress,ifitisinrange
●anerrormessageiftheaddresscannotbereachedinasingleinstruction.
Theoffsetrangeis±
255bytesforanoffsettoanonword-alignedaddress,and±
1020bytes(255words)foranoffsettoaword-alignedaddress.(ForThumb,theaddressmustbewordaligned,andtheoffsetmustbepositive.)
TheassemblerconvertsanADRLrn,labelpseudo-instructionbygenerating:
●twodata-processinginstructionsthatloadtheaddress,ifitisinrange
●anerrormessageiftheaddresscannotbeconstructedintwoinstructions.
TherangeofanADRLpseudo-instructionis±
64KBforanonword-alignedaddressand±
256KBforaword-alignedaddress.(ThereisnoADRLpseudo-instructionforThumb.)
ADRLassemblestotwoinstructions,ifsuccessful.Theassemblergeneratestwoinstructionseveniftheaddresscouldbeloadedinasingleinstruction.
EXAMPLE3ARMcodejumptable
AREAJump,CODE,READONLY;
Namethisblockofcode
CODE32;
FollowingcodeisARMcode
numEQU2;
Numberofentriesinjumptable
start;
Firstinstructiontocall
MOVr0,#0;
Setupthethreeparameters
MOVr1,#3
MOVr2,#2
BLarithfunc;
Callth