ARM程序示例.docx
- 文档编号:11320087
- 上传时间:2023-02-26
- 格式:DOCX
- 页数:27
- 大小:62.04KB
ARM程序示例.docx
《ARM程序示例.docx》由会员分享,可在线阅读,更多相关《ARM程序示例.docx(27页珍藏版)》请在冰豆网上搜索。
ARM程序示例
ARM程序示例
EXAMPLE1(AboutLDR、LTORG)
AREALoadcon,CODE,READONLY
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
;LiteralPool1]
;LDRr4,=0x66666666;Ifthisisuncommentedit
;fails,becauseLiteralPool2
;isoutofreach
MOVpc,lr
LargeTable
SPACE4200;Startingatthecurrentlocation,
;
oneword
;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
ENTRY;Markfirstinstructiontoexecute
Start
BLfunc;Branchtosubroutine
stopMOVr0,#0x18;angel_SWIreason_ReportException
LDRr1,=0x20026;ADP_Stopped_ApplicationExit
SWI0x123456;ARMsemihostingSWI
LTORG;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;Startingatthecurrentlocation,
;clearsa8000byteareaofmemory
;tozero
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
ENTRY;Markfirstinstructiontoexecute
start;Firstinstructiontocall
MOVr0,#0;Setupthethreeparameters
MOVr1,#3
MOVr2,#2
BLarithfunc;Callthefunction
stopMOVr0,#0x18;angel_SWIreason_ReportException
LDRr1,=0x20026;ADP_Stopped_ApplicationExit
SWI0x123456;ARMsemihostingSWI
arithfunc;Labelthefunction
CMPr0,#num;Treatfunctioncodeasunsignedinteger
MOVHSpc,lr;Ifcodeis>=numthensimplyreturn
ADRr3,JumpTable;Loadaddressofjumptable
LDRpc,[r3,r0,LSL#2];Jumptotheappropriateroutine
JumpTable
DCDDoAdd
DCDDoSub
DoAddADDr0,r1,r2;Operation0
MOVpc,lr;Return
DoSubSUBr0,r1,r2;Operation1
MOVpc,lr;Return
END;Marktheendofthisfile
Example4Stringcopy
AREAStrCopy,CODE,READONLY
ENTRY;Markfirstinstructiontoexecute
startLDRr1,=srcstr;Pointertofirststring
LDRr0,=dststr;Pointertosecondstring
BLstrcopy;Callsubroutinetodocopy
stopMOVr0,#0x18;angel_SWIreason_ReportException
LDRr1,=0x20026;ADP_Stopped_ApplicationExit
SWI0x123456;ARMsemihostingSWI
strcopy
LDRBr2,[r1],#1;Loadbyteandupdateaddress
STRBr2,[r0],#1;Storebyteandupdateaddress
CMPr2,#0;Checkforzeroterminator
BNEstrcopy;Keepgoingifnot
MOVpc,lr;Return
AREAStrings,DATA,READWRITE
srcstrDCB"Firststring-source",0
dststrDCB"Secondstring-destination",0
END
Example5Blockcopy
AREAWord,CODE,READONLY;namethisblockofcode
numEQU20;setnumberofwordstobecopied
ENTRY;markthefirstinstructiontocall
start
LDRr0,=src;r0=pointertosourceblock
LDRr1,=dst;r1=pointertodestinationblock
MOVr2,#num;r2=numberofwordstocopy
wordcopyLDRr3,[r0],#4;loadawordfromthesourceand
STRr3,[r1],#4;storeittothedestination
SUBSr2,r2,#1;decrementthecounter
BNEwordcopy;...copymore
stopMOVr0,#0x18;angel_SWIreason_ReportException
LDRr1,=0x20026;ADP_Stopped_ApplicationExit
SWI0x123456;ARMsemihostingSWI
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;namethisblockofcode
numEQU20;setnumberofwordstobecopied
ENTRY;markthefirstinstructiontocall
start
LDRr0,=src;r0=pointertosourceblock
LDRr1,=dst;r1=pointertodestinationblock
MOVr2,#num;r2=numberofwordstocopy
MOVsp,#0x400;Setupstackpointer(r13)
blockcopyMOVSr3,r2,LSR#3;Numberofeightwordmultiples
BEQcopywords;Lessthaneightwordstomove?
STMFDsp!
{r4-r11};Savesomeworkingregisters
octcopyLDMIAr0!
{r4-r11};Load8wordsfromthesource
STMIAr1!
{r4-r11};andputthematthedestination
SUBSr3,r3,#1;Decrementthecounter
BNEoctcopy;...copymore
LDMFDsp!
{r4-r11};Don'tneedthesenow-restore
;originals
copywordsANDSr2,r2,#7;Numberofoddwordstocopy
BEQstop;Nowordslefttocopy?
wordcopyLDRr3,[r0],#4;Loadawordfromthesourceand
STRr3,[r1],#4;storeittothedestination
SUBSr2,r2,#1;Decrementthecounter
BNEwordcopy;...copymore
stopMOVr0,#0x18;angel_SWIreason_ReportException
LDRr1,=0x20026;ADP_Stopped_ApplicationExit
SWI0x123456;ARMsemihostingSWI
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
Test-and-branchmacroexample
Atest-and-branchoperationrequirestwoARMinstructionstoimplement.
Youcandefineamacrodefinitionsuchasthis:
MACRO
$labelTestAndBranch$dest,$reg,$cc
$labelCMP$reg,#0
B$cc$dest
MEND
ThelineaftertheMACROdirectiveisthemacroprototypestatement.Themacroprototypestatementdefinesthename(TestAndBranch)youusetoinvokethemacro.Italsodefinesparameters($label,$dest,$reg,and$cc).Youmustgivevaluestotheparameterswhenyouinvokethemacro.Theassemblersubstitutesthevaluesyougiveintothecode.
Thismacrocanbeinvokedasfollows:
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
//Thumb
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- ARM 程序 示例
![提示](https://static.bdocx.com/images/bang_tan.gif)