




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第3章程序的机器级表示
——
基本
计算机组成与结构
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就是编译器所需要的参数,filenames给出相关的文件名称-c,只编译,不链接为可执行。-o,确定输出文件的名称。如果不给出这个选项,gcc给出预设的可执行文件a.out。-s,产生汇编代码。-O1,对程序进行第一级优化编译、链接。-O2,比-O1更好的优化编译、链接,当然整个编译、链接过程会更慢。等等参考:百度百科GCC阅读:课程网站推荐阅读资料“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“leave”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目标代码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直接生成最终的可执行文件testgcc-O1test1.ctest2.c-otest选项-O1表示一级优化,-O2为二级优化,选项-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位移量机器指令汇编指令编译得到的与反汇编得到的汇编指令形式稍有差异
两种目标文件“objdump-dtest”结果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”结果test.o:可重定位目标文件test:可执行目标文件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插播:函数调用——IA32栈结构示例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)转为四字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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年特许金融分析师考试经典试题及答案
- 全方位了解特许金融分析师考试难题及答案
- 全球银行业发展趋势研究试题及答案
- 2025年特许金融分析师重要主题试题及答案
- 2025年特许金融分析师复习策略建议试题及答案
- 小语种证书考试查缺补漏试题及答案
- 特许金融分析师考试社会影响力提升试题及答案
- 2025年银行从业资格全球市场试题及答案
- 畜牧师职称考试工作技巧试题及答案
- 畜牧师岗位技能的试题及答案深挖
- MT-T 1199-2023 煤矿用防爆柴油机无轨胶轮运输车辆安全技术条件
- 睡眠呼吸暂停低通气综合征课件
- Scratch电子学会等级考试四级模拟题
- 2024年中考数学模拟考试试卷-带答案(北师大版)
- 含油污水处理操作规程
- 基于STM32的停车场智能管理系统
- 中药饮片处方审核培训课件
- 客户回访表完整版本
- 2024年天猫运营月度计划
- 毒蛇咬伤事故专项应急预案
- 岩溶地区建筑地基基础技术规范
评论
0/150
提交评论