Machine-Level Programming I:IntroductionSept. 10, 2002Machine-Level Programming I:IntroductionSept. 10, 2002TopicsTopicsn Assembly Programmer’sExecution Modeln Accessing Informationl Registersl Memoryn Arithmetic operationsclass05.ppt15-213“The course that gives CMU its Zip!”– 2 –15-213, F’02IA32 ProcessorsIA32 ProcessorsTotally Dominate Computer MarketTotally Dominate Computer MarketEvolutionary DesignEvolutionary Designn Starting in 1978 with 8086n Added more features as time goes onn Still support old features, although obsoleteComplex Instruction Set Computer (CISC)Complex Instruction Set Computer (CISC)n Many different instructions with many different formatsl But, only small subset encountered with Linux programsn Hard to match performance of Reduced Instruction SetComputers (RISC)n But, Intel has done just that!– 3 –15-213, F’02X86 Evolution: Programmer’s ViewX86 Evolution: Programmer’s ViewNameNameDateDateTransistorsTransistors808680861978197829K29Kn 16-bit processor. Basis for IBM PC & DOSn Limited to 1MB address space. DOS only gives you 640K802868028619821982134K134Kn Added elaborate, but not very useful, addressing schemen Basis for IBM PC-AT and Windows38638619851985275K275Kn Extended to 32 bits. Added “flat addressing”n Capable of running Unixn Linux/gcc uses no instructions introduced in later models– 4 –15-213, F’02X86 Evolution: Programmer’s ViewX86 Evolution: Programmer’s ViewNameNameDateDateTransistorsTransistors486486198919891.9M1.9MPentiumPentium199319933.1M3.1MPentium/MMXPentium/MMX199719974.5M4.5Mn Added special collection of instructions for operating on 64-bit vectors of 1, 2, or 4 byte integer dataPentiumProPentiumPro199519956.5M6.5Mn Added conditional move instructionsn Big change in underlying microarchitecture– 5 –15-213, F’02X86 Evolution: Programmer’s ViewX86 Evolution: Programmer’s ViewNameNameDateDateTransistorsTransistorsPentium IIIPentium III199919998.2M8.2Mn Added “streaming SIMD” instructions for operating on 128-bitvectors of 1, 2, or 4 byte integer or floating point datan Our fish machinesPentium 4Pentium 42001200142M42Mn Added 8-byte formats and 144 new instructions for streamingSIMD mode– 6 –15-213, F’02X86 Evolution: ClonesX86 Evolution: ClonesAdvanced Micro Devices (AMD)Advanced Micro Devices (AMD)n HistoricallylAMD has followed just behind IntellA little bit slower, a lot cheapern RecentlylRecruited top circuit designers from Digital Equipment Corp.lExploited fact that Intel distracted by IA64lNow are close competitors to Inteln Developing own extension to 64-bits– 7 –15-213, F’02X86 Evolution: ClonesX86 Evolution: ClonesTransmetaTransmetan Recent start-uplEmployer of Linus Torvaldsn Radically different approach to implementationlTranslates x86 code into “Very Long Instruction Word” (VLIW)codelHigh degree of parallelismn Shooting for low-power market– 8 –15-213, F’02New Species: IA64New Species: IA64NameNameDateDateTransistorsTransistorsItaniumItanium2001200110M10Mn Extends to IA64, a 64-bit architecturen Radically new instruction set designed for high performancen Will be able to run existing IA32 programslOn-board “x86 engine”n Joint project with Hewlett-PackardItanium 2Itanium 220022002221M221Mn Big performance boost– 9 –15-213, F’02Assembly Programmer’s ViewAssembly Programmer’s ViewProgrammer-Visible StateProgrammer-Visible Staten EIP Program Counterl Address of next instructionn Register Filel Heavily used program datan Condition Codesl Store status information aboutmost recent arithmetic operationl Used for conditional branchingEIPRegistersCPU MemoryObject CodeProgram DataOS DataAddressesDataInstructionsStackConditionCodesn Memoryl Byte addressable arrayl Code, user data, (some) OSdatal Includes stack used tosupport procedures– 10 –15-213, F’02texttextbinarybinaryCompiler (gcc -S)Assembler (gcc or as)Linker (gcc or ld)C program (p1.c p2.c)Asm program (p1.s p2.s)Object program (p1.o p2.o)Executable program (p)Static libraries(.a)Turning C into Object CodeTurning C into Object CodenCode in files p1.c p2.cn Compile with command: gcc -O p1.c p2.c -o plUse optimizations (-O)lPut resulting binary in file p– 11 –15-213, F’02Compiling Into AssemblyCompiling Into AssemblyC CodeC Codeint sum(int x, int y){ int t = x+y; return t;}Generated Assembly_sum:pushl %ebpmovl %esp,%ebpmovl 12(%ebp),%eaxaddl 8(%ebp),%eaxmovl %ebp,%esppopl %ebpretObtain with commandgcc -O -S code.cProduces file code.s– 12 –15-213, F’02Assembly CharacteristicsAssembly CharacteristicsMinimal Data TypesMinimal Data Typesn “Integer” data of 1, 2, or 4 bytesl Data valuesl Addresses (untyped pointers)n Floating point data of 4, 8, or 10 bytesn No aggregate types such as arrays or structuresl Just contiguously allocated bytes in memoryPrimitive OperationsPrimitive Operationsn Perform arithmetic function on register or memory datan Transfer data between memory and registerl Load data from memory into registerl Store register data into memoryn Transfer controll Unconditional jumps to/from proceduresl Conditional branches– 13 –15-213, F’02Code for sum0x401040 <sum>:0x550x890xe50x8b0x450x0c0x030x450x080x890xec0x5d0xc3Object CodeObject CodeAssemblerAssemblern Translates .s into .on Binary encoding of each instructionn Nearly-complete image of executablecoden Missing linkages between code indifferent filesLinkerLinkern Resolves references between filesn Combines with static run-timelibrariesl E.g., code for malloc, printfn Some libraries are dynamically linkedl Linking occurs when program beginsexecution• Total of 13bytes• Eachinstruction 1,2, or 3 bytes• Starts ataddress0x401040– 14 –15-213, F’02Machine Instruction ExampleMachine Instruction ExampleC CodeC Coden Add two signed integersAssemblyAssemblyn Add 2 4-byte integersl“Long” words in GCC parlancelSame instruction whethersigned or unsignedn Operands:x: Register %eaxy: Memory M[%ebp+8]t: Register %eax»Return function value in %eaxObject CodeObject Coden 3-byte instructionn Stored at address 0x401046int t = x+y;addl 8(%ebp),%eax0x401046: 03 45 08Similar toexpressionx += y– 15 –15-213, F’02Disassembled00401040 <_sum>: 0: 55 push %ebp 1: 89 e5 mov %esp,%ebp 3: 8b 45 0c mov 0xc(%ebp),%eax 6: 03 45 08 add 0x8(%ebp),%eax 9: 89 ec mov %ebp,%esp b: 5d pop %ebp c: c3 ret d: 8d 76 00
View Full Document