15 213 The course that gives CMU its Zip Machine Level Programming I Introduction Sept 10 2002 Topics n Assembly Programmer s Execution Model n Accessing Information l Registers l Memory n class05 ppt Arithmetic operations IA32 Processors Totally Dominate Computer Market Evolutionary Design n Starting in 1978 with 8086 n Added more features as time goes on n Still support old features although obsolete Complex Instruction Set Computer CISC n Many different instructions with many different formats l But only small subset encountered with Linux programs 2 n Hard to match performance of Reduced Instruction Set Computers RISC n But Intel has done just that 15 213 F 02 X86 Evolution Programmer s View Name 8086 Transistors 1978 29K n 16 bit processor Basis for IBM PC DOS n Limited to 1MB address space DOS only gives you 640K 80286 1982 134K n Added elaborate but not very useful addressing scheme n Basis for IBM PC AT and Windows 386 3 Date 1985 275K n Extended to 32 bits Added flat addressing n Capable of running Unix n Linux gcc uses no instructions introduced in later models 15 213 F 02 X86 Evolution Programmer s View Name Date Transistors 486 1989 1 9M Pentium 1993 3 1M Pentium MMX 1997 4 5M n Added special collection of instructions for operating on 64bit vectors of 1 2 or 4 byte integer data PentiumPro 4 1995 6 5M n Added conditional move instructions n Big change in underlying microarchitecture 15 213 F 02 X86 Evolution Programmer s View Name Pentium III Transistors 1999 8 2M n Added streaming SIMD instructions for operating on 128 bit vectors of 1 2 or 4 byte integer or floating point data n Our fish machines Pentium 4 n 5 Date 2001 42M Added 8 byte formats and 144 new instructions for streaming SIMD mode 15 213 F 02 X86 Evolution Clones Clones Advanced Micro Devices AMD n Historically l AMD has followed just behind Intel l A little bit slower a lot cheaper n Recently l Recruited top circuit designers from Digital Equipment Corp l Exploited fact that Intel distracted by IA64 l Now are close competitors to Intel n 6 Developing own extension to 64 bits 15 213 F 02 X86 Evolution Clones Clones Transmeta n Recent start up l Employer of Linus Torvalds n Radically different approach to implementation l Translates x86 code into Very Long Instruction Word VLIW code l High degree of parallelism n 7 Shooting for low power market 15 213 F 02 New Species IA64 Name Date Transistors Itanium 2001 10M n Extends to IA64 a 64 bit architecture n Radically new instruction set designed for high performance n Will be able to run existing IA32 programs l On board x86 engine n Joint project with Hewlett Packard Itanium 2 n 8 2002 221M Big performance boost 15 213 F 02 Assembly Programmer s View CPU Memory Addresses Registers E I P Data Condition Codes Object Code Program Data OS Data Instructions Stack Programmer Visible State n EIP Program Counter l Address of next instruction n Register File l Heavily used program data n Condition Codes l Store status information about 9 most recent arithmetic operation l Used for conditional branching n Memory l Byte addressable array l Code user data some OS data l Includes stack used to support procedures 15 213 F 02 Turning C into into Object Object Code Code n Code in files p1 c p2 c n Compile with command gcc O p1 c p2 c o p l Use optimizations O l Put resulting binary in file p text C program p1 c p2 c Compiler gcc S text Asm program p1 s p2 s Assembler gcc or as binary Object program p1 o p2 o Static libraries a Linker gcc or ld binary 10 Executable program p 15 213 F 02 Compiling Into Assembly Assembly C Code int sum int x int y int t x y return t Generated Assembly sum pushl ebp movl esp ebp movl 12 ebp eax addl 8 ebp eax movl ebp esp popl ebp ret Obtain with command gcc O S code c Produces file code s 11 15 213 F 02 Assembly Characteristics Minimal Data Types n Integer data of 1 2 or 4 bytes l Data values l Addresses untyped pointers n Floating point data of 4 8 or 10 bytes n No aggregate types such as arrays or structures l Just contiguously allocated bytes in memory Primitive Operations n Perform arithmetic function on register or memory data n Transfer data between memory and register l Load data from memory into register l Store register data into memory n Transfer control l Unconditional jumps to from procedures l Conditional branches 12 15 213 F 02 Object Code Code for sum Assembler n Translates s into o n Some libraries are dynamically linked 0x401040 sum n Binary encoding of each instruction 0x55 Total of 13 0x89 n Nearly complete image of executable bytes 0xe5 code Each 0x8b instruction 1 n Missing linkages between code in 0x45 2 or 3 bytes different files 0x0c Starts at 0x03 address Linker 0x45 0x401040 0x08 n Resolves references between files 0x89 n Combines with static run time 0xec libraries 0x5d l E g code for malloc printf 0xc3 l Linking occurs when program begins execution 13 15 213 F 02 Machine Instruction Instruction Example Example C Code int t x y n Add two signed integers Assembly addl 8 ebp eax n Add 2 4 byte integers l Long words in GCC parlance Similar to expression x y l Same instruction whether signed or unsigned n Operands x y t 0x401046 03 45 08 Object Code n n 14 Register eax Memory M ebp 8 Register eax Return function value in eax 3 byte instruction Stored at address 0x401046 15 213 F 02 Disassembling Object Object Code Code Disassembled 00401040 sum 0 55 1 89 e5 3 8b 45 0c 6 03 45 08 9 89 ec b 5d c c3 d 8d 76 00 push mov mov add mov pop ret lea ebp esp ebp 0xc ebp eax 0x8 ebp eax ebp esp ebp 0x0 esi esi Disassembler objdump d p n Useful tool for examining object code n Analyzes bit pattern of series of instructions n Produces approximate rendition of assembly code Can be run on either a out complete executable or o file n 15 15 213 F 02 Alternate Disassembly Disassembled Object 0x401040 0x55 0x89 0xe5 0x8b 0x45 0x0c 0x03 0x45 0x08 0x89 0xec 0x5d 0xc3 0x401040 0x401041 0x401043 0x401046 0x401049 0x40104b 0x40104c 0x40104d sum sum 1 sum 3 sum 6 sum 9 sum 11 sum 12 sum 13 push mov mov add mov pop ret lea ebp esp ebp 0xc ebp eax 0x8 ebp eax ebp esp ebp 0x0 esi esi Within gdb Debugger gdb p disassemble sum Disassemble procedure x 13b sum n n 16 Examine the 13 bytes starting at sum 15 213 F 02 What Can be Disassembled Disassembled objdump d WINWORD EXE WINWORD EXE file format pei i386 No symbols in WINWORD EXE Disassembly of section text 30001000 text 30001000 …
View Full Document