計算機組成與結(jié)構(gòu):第二部分-程序和數(shù)據(jù)的機器級表達 05-程序的機器級表示:基本_第1頁
計算機組成與結(jié)構(gòu):第二部分-程序和數(shù)據(jù)的機器級表達 05-程序的機器級表示:基本_第2頁
計算機組成與結(jié)構(gòu):第二部分-程序和數(shù)據(jù)的機器級表達 05-程序的機器級表示:基本_第3頁
計算機組成與結(jié)構(gòu):第二部分-程序和數(shù)據(jù)的機器級表達 05-程序的機器級表示:基本_第4頁
計算機組成與結(jié)構(gòu):第二部分-程序和數(shù)據(jù)的機器級表達 05-程序的機器級表示:基本_第5頁
已閱讀5頁,還剩48頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

第3章程序的機器級表示

——

基本

計算機組成與結(jié)構(gòu)

2016年4月主講教師Today:MachineProgrammingI:BasicsHistoryofIntelprocessorsandarchitecturesC,assembly,machinecodeAssemblyBasics:Registers,operands,moveaddressingmode,addresscomputation*ArithmeticoperationsIntrotox86-64Intelx86Evolution:MilestonesName Date Transistors MHz8086 1978 29K 5-10First16-bitprocessor.BasisforIBMPC&DOS1MBaddressspace386 1985 275K 16-33First32bitprocessor,referredtoasIA32Added“flataddressing”,CapableofrunningUnixPentium4F 2004 125M 2800-3800First64-bitprocessor,referredtoasx86-64Core22006291M1060-3500Firstmulti-coreIntelprocessorCorei7 2008 731M 1700-3900Fourcores(oursharkmachines)Haswell20131.4B1900-3700On-chipGPUOurCoverageIA32Thetraditionalx86x86-64/EM64TTheemergingstandardPresentationBookpresentsIA32inSections3.1—3.12Coversx86-64in3.13WewillcoverbothsimultaneouslySomelabswillbebasedonx86-64,othersonIA32Today:MachineProgrammingI:BasicsHistoryofIntelprocessorsandarchitecturesC,assembly,machinecodeAssemblyBasics:Registers,operands,moveaddressingmode,addresscomputationArithmeticoperationsIntrotox86-64CPUAssemblyProgrammer’sViewProgrammer-VisibleStatePC:ProgramcounterAddressofnextinstructionCalled“EIP”(IA32)or“RIP”(x86-64)RegisterfileHeavilyusedprogramdataConditioncodesStorestatusinformationaboutmostrecentarithmeticoperationUsedforconditionalbranchingPCRegistersMemoryCodeDataStackAddressesDataInstructionsConditionCodesMemoryByteaddressablearrayCodeanduserdataStacktosupportprocedurestexttextbinarybinaryCompiler(gcc-S)Assembler(gccoras)Linker(gccorld)Cprogram(p1.cp2.c)Asmprogram(p1.sp2.s)Objectprogram(p1.op2.o)Executableprogram(p)Staticlibraries(.a)TurningCintoObjectCodeCodeinfilesp1.cp2.cCompilewithcommand:gcc–O1p1.cp2.c-opUsebasicoptimizations(-O1)PutresultingbinaryinfilepGCC的基本用法gcc[options][filenames]其中options就是編譯器所需要的參數(shù),filenames給出相關(guān)的文件名稱-c,只編譯,不鏈接為可執(zhí)行。-o,確定輸出文件的名稱。如果不給出這個選項,gcc給出預(yù)設(shè)的可執(zhí)行文件a.out。-s,產(chǎn)生匯編代碼。-O1,對程序進行第一級優(yōu)化編譯、鏈接。-O2,比-O1更好的優(yōu)化編譯、鏈接,當(dāng)然整個編譯、鏈接過程會更慢。等等參考:百度百科GCC閱讀:課程網(wǎng)站推薦閱讀資料“GCC”專題CompilingIntoAssemblyCCodeintsum(intx,inty){intt=x+y;returnt;}GeneratedIA32Assemblysum: pushl%ebp movl%esp,%ebp movl12(%ebp),%eax addl8(%ebp),%eax popl%ebp retObtainwithcommandgcc–O1-Scode.cProducesfilecode.sSomecompilersuseinstruction“l(fā)eave”MachineInstructionExampleCCodeAddtwosignedintegersAssemblyAdd24-byteintegers“Long”wordsinGCCparlanceSameinstructionwhethersignedorunsignedOperands:x: Register %eaxy: Memory M[%ebp+8]t: Register %eaxReturnfunctionvaluein%eaxObjectCode3-byteinstructionStoredataddress0x80483caintt=x+y;Addl8(%ebp),%eax0x80483ca:034508Similartoexpression:x+=yMoreprecisely:inteax;int*ebp;eax+=ebp[2]Codeforsum0x401040<sum>:0x550x890xe50x8b0x450x0c0x030x450x080x5d0xc3ObjectCodeAssemblerTranslates.sinto.oBinaryencodingofeachinstructionNearly-completeimageofexecutablecodeMissinglinkagesbetweencodeindifferentfilesLinkerResolvesreferencesbetweenfilesCombineswithstaticrun-timelibrariesE.g.,codeformalloc,printfSomelibrariesaredynamicallylinkedLinkingoccurswhenprogrambeginsexecutionTotalof11bytesEachinstruction1,2,or3bytesStartsataddress0x0401040Obtainwithcommandgcc–O1-ccode.cProducesfilecode.o目標(biāo)代碼code.oDisassembledDisassemblingObjectCodeDisassemblerobjdump–dcode.oUsefultoolforexaminingobjectcodeAnalyzesbitpatternofseriesofinstructionsProducesapproximaterenditionofassemblycodeCanberunoneithera.out(completeexecutable)or.ofile0x401040<sum>:0x401040:55push%ebp0x401041:89e5mov%esp,%ebp0x401043:8b450cmov0xc(%ebp),%eax0x401046:034508add0x8(%ebp),%eax0x401049:5dpop%ebp0x40104a:c3retDisassembledDumpofassemblercodeforfunctionsum:0x080483c4<sum+0>:push%ebp0x080483c5<sum+1>:mov%esp,%ebp0x080483c7<sum+3>:mov0xc(%ebp),%eax0x080483ca<sum+6>:add0x8(%ebp),%eax0x080483cd<sum+9>:pop%ebp0x080483ce<sum+10>:retAlternateDisassemblyWithingdbDebuggergdbPdisassemblesumDisassembleprocedurex/11xbsumExaminethe11bytesstartingatsumObject0x401040:0x550x890xe50x8b0x450x0c0x030x450x080x5d0xc3GCC使用舉例將C源程序文件test.c,用GCC直接生成最終的可執(zhí)行文件testgcc-O1test1.ctest2.c-otest選項-O1表示一級優(yōu)化,-O2為二級優(yōu)化,選項-o指出輸出文件名目標(biāo)文件可用“objdump-dtest.o”反匯編為匯編語言程序add:pushl %ebpmovl %esp,%ebpsubl $16,%espmovl 12(%ebp),%eaxmovl 8(%ebp),%edxleal (%edx,%eax),%eaxmovl %eax,-4(%ebp)movl -4(%ebp),%eaxleaveret00000000<add>:0:55 push%ebp1:89e5 mov%esp,%ebp3:83ec10sub$0x10,%esp6:8b450cmov0xc(%ebp),%eax9:8b5508mov0x8(%ebp),%edxc:8d0402lea(%edx,%eax,1),%eaxf:8945fcmov%eax,-0x4(%ebp)12:8b45fcmov-0x4(%ebp),%eax15:c9leave16:c3retgcc-Etest.c-otest.igcc-Stest.i-otest.s

gcc–Stest.c–otest.s

test.s位移量機器指令匯編指令編譯得到的與反匯編得到的匯編指令形式稍有差異

兩種目標(biāo)文件“objdump-dtest”結(jié)果00000000<add>:0:55 push%ebp1:89e5 mov%esp,%ebp3:83ec10sub$0x10,%esp6:8b450cmov0xc(%ebp),%eax9:8b5508mov0x8(%ebp),%edxc:8d0402lea(%edx,%eax,1),%eaxf:8945fcmov%eax,-0x4(%ebp)12:8b45fcmov-0x4(%ebp),%eax15:c9leave16:c3rettest.o中的代碼從地址0開始,test中的代碼從80483d4開始!080483d4<add>:80483d4:55push...80483d5:89e5…80483d7:83ec10…80483da:8b450c…80483dd:8b5508…80483e0:8d0402…80483e3:8945fc…80483e6:8b45fc…80483e9:c9…80483ea:c3

ret

“objdump-dtest.o”結(jié)果test.o:可重定位目標(biāo)文件test:可執(zhí)行目標(biāo)文件Today:MachineProgrammingI:BasicsHistoryofIntelprocessorsandarchitecturesC,assembly,machinecodeAssemblyBasics:Registers,operands,moveaddressingmode,addresscomputation*ArithmeticoperationsIntrotox86-64AssemblyCharacteristics:DataTypes“Integer”dataof1,2,or4bytesDatavaluesAddresses(untypedpointers)Floatingpointdataof4,8,or10bytesNoaggregatetypessuchasarraysorstructuresJustcontiguouslyallocatedbytesinmemoryAssemblyCharacteristics:OperationsPerformarithmeticfunctiononregisterormemorydataTransferdatabetweenmemoryandregisterLoaddatafrommemoryintoregisterStoreregisterdataintomemoryTransfercontrolUnconditionaljumpsto/fromproceduresConditionalbranchesIntegerRegisters(IA32)%eax%ecx%edx%ebx%esi%edi%esp%ebp%ax%cx%dx%bx%si%di%sp%bp%ah%ch%dh%bh%al%cl%dl%bl16-bitvirtualregisters(backwardscompatibility)generalpurposeaccumulatecounterdatabasesourceindexdestinationindexstackpointerbasepointerOrigin(mostlyobsolete)MovingData:IA32MovingDatamovlSource,Dest:OperandTypesImmediate:

ConstantintegerdataExample:$0x400,$-533LikeCconstant,butprefixedwith‘$’Encodedwith1,2,or4bytesRegister:Oneof8integerregistersExample:%eax,%edxBut%espand%ebpreservedforspecialuseOthershavespecialusesforparticularinstructionsMemory:

4consecutivebytesofmemoryataddressgivenbyregisterSimplestexample:(%eax)Variousother“addressmodes”%eax%ecx%edx%ebx%esi%edi%esp%ebpmovlOperandCombinationsCannotdomemory-memorytransferwithasingleinstructionmovlImmRegMemRegMemRegMemRegSourceDestCAnalogmovl$0x4,%eaxtemp=0x4;movl$-147,(%eax)*p=-147;movl%eax,%edxtemp2=temp1;movl%eax,(%edx)*p=temp;movl(%eax),%edxtemp=*p;Src,DestToday:MachineProgrammingI:BasicsHistoryofIntelprocessorsandarchitecturesC,assembly,machinecodeAssemblyBasics:Registers,operands,moveaddressingmode,addresscomputation*ArithmeticoperationsIntrotox86-64SimpleMemoryAddressingModesNormal (R) Mem[Reg[R]]RegisterRspecifiesmemoryaddress

movl(%ecx),%eaxDisplacement D(R) Mem[Reg[R]+D]RegisterRspecifiesstartofmemoryregionConstantdisplacementDspecifiesoffset

movl8(%ebp),%edxUsingSimpleAddressingModesvoidswap(int*xp,int*yp){intt0=*xp;intt1=*yp;*xp=t1;*yp=t0;}BodySetUpFinishswap:pushl%ebpmovl%esp,%ebppushl%ebxmovl8(%ebp),%edxmovl12(%ebp),%ecxmovl(%edx),%ebxmovl(%ecx),%eaxmovl%eax,(%edx)movl%ebx,(%ecx)popl%ebxpopl%ebpretUsingSimpleAddressingModesvoidswap(int*xp,int*yp){intt0=*xp;intt1=*yp;*xp=t1;*yp=t0;}swap:pushl%ebpmovl%esp,%ebppushl%ebxmovl 8(%ebp),%edxmovl 12(%ebp),%ecxmovl (%edx),%ebxmovl (%ecx),%eaxmovl %eax,(%edx)movl %ebx,(%ecx)popl %ebxpopl %ebpretBodySetUpFinish插播:函數(shù)調(diào)用——IA32棧結(jié)構(gòu)示例C程序:intsum(intx,inty){ intt=x+y; returnt;}intmain(){ returnsum(1,3);}UnderstandingSwapvoidswap(int*xp,int*yp){intt0=*xp;intt1=*yp;*xp=t1;*yp=t0;}Stack(inmemory)Register Value%edxxp%ecxyp%ebxt0%eaxt1ypxpRtnadrOld%ebp%ebp04812Offset???Old%ebx-4%espmovl8(%ebp),%edx#edx=xpmovl12(%ebp),%ecx#ecx=ypmovl(%edx),%ebx#ebx=*xp(t0)movl(%ecx),%eax#eax=*yp(t1)movl%eax,(%edx)#*xp=t1movl%ebx,(%ecx)#*yp=t0UnderstandingSwap0x1200x124Rtnadr%ebp04812Offset-4123456Address0x1240x1200x11c0x1180x1140x1100x10c0x1080x1040x100ypxp%eax%edx%ecx%ebx%esi%edi%esp%ebp0x104movl8(%ebp),%edx #edx=xpmovl12(%ebp),%ecx #ecx=ypmovl(%edx),%ebx #ebx=*xp(t0)movl(%ecx),%eax #eax=*yp(t1)movl%eax,(%edx) #*xp=t1movl%ebx,(%ecx) #*yp=t0UnderstandingSwap0x1200x124Rtnadr%ebp04812Offset-4123456Address0x1240x1200x11c0x1180x1140x1100x10c0x1080x1040x100ypxp%eax%edx%ecx%ebx%esi%edi%esp%ebp0x1240x1040x120movl8(%ebp),%edx #edx=xpmovl12(%ebp),%ecx #ecx=ypmovl(%edx),%ebx #ebx=*xp(t0)movl(%ecx),%eax #eax=*yp(t1)movl%eax,(%edx) #*xp=t1movl%ebx,(%ecx) #*yp=t0UnderstandingSwap0x1200x124Rtnadr%ebp04812Offset-4123456Address0x1240x1200x11c0x1180x1140x1100x10c0x1080x1040x100ypxp%eax%edx%ecx%ebx%esi%edi%esp%ebp0x1200x1040x1240x124movl8(%ebp),%edx #edx=xpmovl12(%ebp),%ecx #ecx=ypmovl(%edx),%ebx #ebx=*xp(t0)movl(%ecx),%eax #eax=*yp(t1)movl%eax,(%edx) #*xp=t1movl%ebx,(%ecx) #*yp=t0456UnderstandingSwap0x1200x124Rtnadr%ebp04812Offset-4123456Address0x1240x1200x11c0x1180x1140x1100x10c0x1080x1040x100ypxp%eax%edx%ecx%ebx%esi%edi%esp%ebp0x1240x1201230x104movl8(%ebp),%edx#edx=xpmovl12(%ebp),%ecx #ecx=ypmovl(%edx),%ebx #ebx=*xp(t0)movl(%ecx),%eax #eax=*yp(t1)movl%eax,(%edx) #*xp=t1movl%ebx,(%ecx) #*yp=t0UnderstandingSwap0x1200x124Rtnadr%ebp04812Offset-4123456Address0x1240x1200x11c0x1180x1140x1100x10c0x1080x1040x100ypxp%eax%edx%ecx%ebx%esi%edi%esp%ebp4560x1240x1200x104123123movl8(%ebp),%edx #edx=xpmovl12(%ebp),%ecx #ecx=ypmovl(%edx),%ebx #ebx=*xp(t0)movl(%ecx),%eax #eax=*yp(t1)movl%eax,(%edx) #*xp=t1movl%ebx,(%ecx) #*yp=t0456456UnderstandingSwap0x1200x124Rtnadr%ebp04812Offset-4Address0x1240x1200x11c0x1180x1140x1100x10c0x1080x1040x100ypxp%eax%edx%ecx%ebx%esi%edi%esp%ebp4564560x1240x1201230x104123movl8(%ebp),%edx #edx=xpmovl12(%ebp),%ecx #ecx=ypmovl(%edx),%ebx #ebx=*xp(t0)movl(%ecx),%eax #eax=*yp(t1)movl%eax,(%edx) #*xp=t1movl%ebx,(%ecx) #*yp=t0UnderstandingSwap0x1200x124Rtnadr%ebp04812Offset-4456123Address0x1240x1200x11c0x1180x1140x1100x10c0x1080x1040x100ypxp%eax%edx%ecx%ebx%esi%edi%esp%ebp4560x1240x1200x104123movl8(%ebp),%edx #edx=xpmovl12(%ebp),%ecx #ecx=ypmovl(%edx),%ebx #ebx=*xp(t0)movl(%ecx),%eax #eax=*yp(t1)movl%eax,(%edx) #*xp=t1movl%ebx,(%ecx) #*yp=t0CompleteMemoryAddressingModesMostGeneralFormD(Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]+D]D: Constant“displacement”1,2,or4bytesRb:Baseregister:Anyof8integerregistersRi: Indexregister:Any,exceptfor%espUnlikelyyou’duse%ebp,eitherS: Scale:1,2,4,or8(whythesenumbers?)SpecialCases(Rb,Ri) Mem[Reg[Rb]+Reg[Ri]]D(Rb,Ri) Mem[Reg[Rb]+Reg[Ri]+D](Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]]AddressComputationExamplesExpressionAddressComputationAddress0x8(%edx)0xf000+0x80xf008(%edx,%ecx)0xf000+0x1000xf100(%edx,%ecx,4)0xf000+4*0x1000xf4000x80(,%edx,2)2*0xf000+0x800x1e080%edx0xf000%ecx0x0100ExpressionAddressComputationAddress0x8(%edx)0xf000+0x80xf008(%edx,%ecx)0xf000+0x1000xf100(%edx,%ecx,4)0xf000+4*0x1000xf4000x80(,%edx,2)2*0xf000+0x800x1e080AddressComputationInstructionLealSrc,DestSrcisaddressmodeexpressionSetDesttoaddressdenotedbyexpressionUsesComputingaddresseswithoutamemoryreferenceE.g.,translationofp=&x[i];Computingarithmeticexpressionsoftheformx+k*yk=1,2,4,or8Exampleintmul12(intx){returnx*12;}leal(%eax,%eax,2),%eax;t<-x+x*2sall$2,%eax;returnt<<2ConvertedtoASMbycompiler:Today:MachineProgrammingI:BasicsHistoryofIntelprocessorsandarchitecturesC,assembly,machinecodeAssemblyBasics:Registers,operands,moveaddressingmode,addresscomputation*ArithmeticoperationsIntrotox86-64SomeArithmeticOperationsTwoOperandInstructions:FormatComputationaddl

Src,Dest ;Dest=Dest+Srcsubl

Src,Dest ;Dest=DestSrcimull

Src,Dest ;Dest=Dest*Srcsall

Src,Dest ;Dest=Dest<<Src(Alsocalledshll)sarl

Src,Dest ;Dest=Dest>>Src(Arithmetic)shrl

Src,Dest ;Dest=Dest>>Src(Logical)xorl

Src,Dest ;Dest=Dest^Srcandl

Src,Dest ;Dest=Dest&Srcorl

Src,Dest ;Dest=Dest|SrcWatchoutforargumentorder!Nodistinctionbetweensignedandunsignedint(why?)SomeArithmeticOperationsOneOperandInstructionsincl

Dest Dest=Dest+1decl

Dest Dest=Dest1negl

Dest Dest=Destnotl

Dest Dest=~DestSeebookformoreinstructionsArithmeticExpressionExampleintarith(intx,inty,intz){intt1=x+y;intt2=z+t1;intt3=x+4;intt4=y*48;intt5=t3+t4;intrval=t2*t5;returnrval;}arith:pushl %ebpmovl %esp,%ebpmovl 8(%ebp),%ecxmovl 12(%ebp),%edxleal (%edx,%edx,2),%eaxsall $4,%eaxleal 4(%ecx,%eax),%eaxaddl %ecx,%edxaddl 16(%ebp),%edximull %edx,%eaxpopl %ebpretBodySetUpFinish???16z12y8x4Rtn

Addr0Old%ebpUnderstandingarithmovl 8(%ebp),%ecxmovl 12(%ebp),%edxleal (%edx,%edx,2),%eaxsall $4,%eaxleal 4(%ecx,%eax),%eaxaddl %ecx,%edxaddl 16(%ebp),%edximull %edx,%eax%ebpOffsetintarith(intx,inty,intz){intt1=x+y;intt2=z+t1;intt3=x+4;intt4=y*48;intt5=t3+t4;intrval=t2*t5;returnrval;}???16z12y8x4Rtn

Addr0Old%ebpUnderstandingarith%ebpOffsetStackintarith(intx,inty,intz){intt1=x+y;intt2=z+t1;intt3=x+4;intt4=y*48;intt5=t3+t4;intrval=t2*t5;returnrval;}movl 8(%ebp),%ecx #ecx=xmovl 12(%ebp),%edx #edx=yleal (%edx,%edx,2),%eax #eax=y*3sall $4,%eax #eax*=16(t4)leal 4(%ecx,%eax),%eax #eax=t4+x+4(t5)addl %ecx,%edx #edx=x+y(t1)addl 16(%ebp),%edx #edx+=z(t2)imull %edx,%eax #eax=t2*t5(rval)ObservationsaboutarithInstructionsindifferentorderfromCcodeSomeexpressionsrequiremultipleinstructionsSomeinstructionscovermultipleexpressionsGetexactsamecodewhencompile:(x+y+z)*(x+4+48*y)movl 8(%ebp),%ecx #ecx=xmovl 12(%ebp),%edx #edx=yleal (%edx,%edx,2),%eax #eax=y*3sall $4,%eax #eax*=16(t4)leal 4(%ecx,%eax),%eax #eax=t4+x+4(t5)addl %ecx,%edx #edx=x+y(t1)addl 16(%ebp),%edx #edx+=z(t2)imull %edx,%eax #eax=t2*t5(rval)intarith(intx,inty,intz){intt1=x+y;intt2=z+t1;intt3=x+4;intt4=y*48;intt5=t3+t4;intrval=t2*t5;returnrval;}SpecialArithmeticOperations指令效果描述imullS%edx:%eax←S×%eax有符號全64位乘法mullS%edx

:%eax←S×%eax無符號全64位乘法cltd%edx:%eax←SignExtend(%eax)轉(zhuǎn)為四字idivlS%edx←%edx:%eaxmodS;%eax←%edx:%eax÷S有符號除法divlS%edx←%edx

:%eaxmodS;%eax←%edx:%eax÷S無符號除法Today:MachineProgrammingI:BasicsHistoryofIntelprocessorsandarchitecturesC,assembly,machinecodeAssemblyBasics:Regi

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論