MachineLevel Programming 1 Introduction Topics n n Assembly
Machine-Level Programming 1 Introduction Topics n n Assembly Programmer’s Execution Model Accessing Information l Registers l Memory n Arithmetic operations
IA 32 Processors Totally Dominate Computer Market Evolutionary Design n Starting in 1978 with 8086 Added more features as time goes on 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 n n – 2– Hard to match performance of Reduced Instruction Set Computers (RISC) But, Intel has done just that! CMSC 313, F’ 09
x 86 Evolution: Programmer’s View (Abbreviated) Name Date Transistors 8086 1978 29 K 16 -bit processor. Basis for IBM PC & DOS n Limited to 1 MB address space. n DOS only gives you 640 K n 386 1985 275 K Extended to 32 bits. Added “flat addressing” n Capable of running Unix n Referred to as “IA 32” n 32 -bit Linux/gcc uses no instructions introduced in later models n – 3– CMSC 313, F’ 09
x 86 Evolution: Programmer’s View Machine Evolution 1989 1. 9 M Pentium 1993 n Pentium/MMX n Pentium. Pro n Pentium III n Pentium 4 2001 n Core Duo 2006 3. 1 M 1997 1995 1999 42 M 291 M n 486 n 4. 5 M 6. 5 M 8. 2 M Added Features n Instructions to support multimedia operations l Parallel operations on 1, 2, and 4 -byte data, both integer & FP n Instructions to enable more efficient conditional operations Linux/GCC Evolution – 4– n None! CMSC 313, F’ 09
X 86 Evolution: 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. and other downward trending companies l Exploited fact that Intel distracted by IA 64 l Now are close competitors to Intel n Developed x 86 -64, its own extension to 64 bits l Started eating into Intel’s high-end server market – 5– CMSC 313, F’ 09
Assembly Programmer’s View CPU Memory Addresses Registers P C Data Condition Codes Instructions Object Code Program Data OS Data Programmer-Visible State n Stack PC Program Counter l Address of next instruction l Called “EIP” (IA 32) or “RIP” (x 86 - 64) n Register File l Heavily used program data n Condition Codes l Store status information about – 6– 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 CMSC 313, F’ 09
Turning C into Object Code in files p 1. c p 2. c n Compile with command: gcc –O 2 p 1. c p 2. c -o p n l Use optimizations (-O 2) l Put resulting binary in file p text C program (p 1. c p 2. c) Compiler (gcc -S) text Asm program (p 1. s p 2. s) Assembler (gcc or as) binary Object program (p 1. o p 2. o) Static libraries (. a) Linker (gcc or ld) binary – 7– Executable program (p) CMSC 313, F’ 09
Compiling Into Assembly Generated IA 32 Assembly C Code sum: pushl %ebp movl %esp, %ebp movl 12(%ebp), %eax addl 8(%ebp), %eax movl %ebp, %esp popl %ebp ret int sum(int x, int y) { int t = x + y; return t; } Obtain with command gcc –O 2 -S code. c Produces file code. s Some compilers use single instruction “leave” Gnu ASssembler (GAS) format – 8– CMSC 313, F’ 09
Assembly Characteristics Minimal Data Types n “Integer” data of 1, 2, or 4 bytes l Data values l Addresses (untyped pointers) n n Floating point data of 4, 8, or 10 bytes No aggregate types such as arrays or structures l Just contiguously allocated bytes in memory Primitive Operations n n Perform arithmetic function on register or memory data 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 – 9– CMSC 313, F’ 09
Object Code for sum Assembler n Translates. s into. o 0 x 401040 <sum>: n Binary encoding of each instruction 0 x 55 • Total of 13 0 x 89 n Nearly-complete image of executable bytes 0 xe 5 code • Each 0 x 8 b instruction 1, n Missing linkages between code in 0 x 45 2, or 3 bytes different files 0 x 0 c • Starts at 0 x 03 address Linker 0 x 45 0 x 401040 0 x 08 n Resolves references between files 0 x 89 n Combines with static run-time libraries 0 xec l E. g. , code for malloc, printf 0 x 5 d 0 xc 3 n Some libraries are dynamically linked l Linking occurs when program begins execution – 10 – CMSC 313, F’ 09
Machine Instruction Example int t = x + y; C Code n Add two signed integers Assembly addl 8(%ebp), %eax n l “Long” words in GCC parlance Similar to expression: l Same instruction whether signed or unsigned x += y Or n int *ebp; eax += ebp[2] 03 45 08 Operands: x: Register %eax y: Memory M[%ebp+8] t: Register %eax » Return function value in %eax int eax; 0 x 401046: Add 2 4 -byte integers Object Code 3 -byte instruction n Stored at address 0 x 401046 n – 11 – CMSC 313, F’ 09
Disassembling Object Code Disassembled 00401040 <_sum>: 0: 55 1: 89 e 5 3: 8 b 45 0 c 6: 03 45 08 9: 89 ec b: 5 d c: c 3 d: 8 d 76 00 push mov add mov pop ret lea %ebp %esp, %ebp 0 xc(%ebp), %eax 0 x 8(%ebp), %eax %ebp, %esp %ebp 0 x 0(%esi), %esi Disassembler: objdump -d p n n n – 12 – Useful tool for examining object code Analyzes bit pattern of series of instructions Produces approximate rendition of assembly code Slightly different than GAS format in. s file Can be run on either a. out (complete executable) or. o file CMSC 313, F’ 09
Alternate Disassembly Disassembled Object 0 x 401040: 0 x 55 0 x 89 0 xe 5 0 x 8 b 0 x 45 0 x 0 c 0 x 03 0 x 45 0 x 08 0 x 89 0 xec 0 x 5 d 0 xc 3 0 x 401040 0 x 401041 0 x 401043 0 x 401046 0 x 401049 0 x 40104 b 0 x 40104 c 0 x 40104 d <sum>: <sum+1>: <sum+3>: <sum+6>: <sum+9>: <sum+11>: <sum+12>: <sum+13>: push mov add mov pop ret lea %ebp %esp, %ebp 0 xc(%ebp), %eax 0 x 8(%ebp), %eax %ebp, %esp %ebp 0 x 0(%esi), %esi Within gdb Debugger unix> gdb p (gdb) disassemble sum Disassemble procedure (gdb) x/13 b sum n Examine the 13 bytes starting at sum n – 13 – CMSC 313, F’ 09
What Can be Disassembled? % objdump -d WINWORD. EXE: file format pei-i 386 No symbols in "WINWORD. EXE". Disassembly of section. text: 30001000 <. text>: 30001000: 55 30001001: 8 b ec 30001003: 6 a ff 30001005: 68 90 10 00 30 3000100 a: 68 91 dc 4 c 30 n n – 14 – push mov push %ebp %esp, %ebp $0 xffff $0 x 30001090 $0 x 304 cdc 91 Anything that can be interpreted as executable code Disassembler examines bytes and reconstructs assembly source CMSC 313, F’ 09
Moving Data: IA 32 %eax %edx Moving Data movl Source, Dest: n n Move 4 -byte (“long”) word Lots of these in typical code Operand Types n Immediate: Constant integer data l Like C constant, but prefixed with ‘$’ %ecx %ebx %esi %edi %esp %ebp l E. g. , $0 x 400, $-533 l Encoded with 1, 2, or 4 bytes n Register: One of 8 integer registers l But %esp and %ebp reserved for special use l Others have special uses for particular instructions n Memory: 4 consecutive bytes of memory l Various “address modes” – 15 – CMSC 313, F’ 09
movl Operand Combinations Source movl Dest Src, Dest C Analog Imm Reg movl $0 x 4, %eax Mem movl $-147, (%eax) temp = 0 x 4; Reg movl %eax, %edx Mem movl %eax, (%edx) temp 2 = temp 1; Mem Reg temp = *p; movl (%eax), %edx *p = -147; *p = temp; Cannot do memory-memory transfer with a single instruction – 16 – CMSC 313, F’ 09
Simple Addressing Modes Normal (R) Mem[Reg[R]] n Register R specifies memory address movl (%ecx), %eax Displacement D(R) Mem[Reg[R]+D] n Register R specifies start of memory region n Constant displacement D specifies offset movl 8(%ebp), %edx – 17 – CMSC 313, F’ 09
Using Simple Addressing Modes void swap(int *xp, int *yp) { int t 0 = *xp; int t 1 = *yp; *xp = t 1; *yp = t 0; } swap: pushl %ebp movl %esp, %ebp pushl %ebx movl movl 12(%ebp), %ecx 8(%ebp), %edx (%ecx), %eax (%edx), %ebx %eax, (%edx) %ebx, (%ecx) movl -4(%ebp), %ebx movl %ebp, %esp popl %ebp ret – 18 – Set Up Body Finish CMSC 313, F’ 09
Using Simple Addressing Modes void swap(int *xp, int *yp) { int t 0 = *xp; int t 1 = *yp; *xp = t 1; *yp = t 0; } swap: pushl %ebp movl %esp, %ebp pushl %ebx movl movl 12(%ebp), %ecx 8(%ebp), %edx (%ecx), %eax (%edx), %ebx %eax, (%edx) %ebx, (%ecx) movl -4(%ebp), %ebx movl %ebp, %esp popl %ebp ret – 19 – Set Up Body Finish CMSC 313, F’ 09
Understanding Swap void swap(int *xp, int *yp) { int t 0 = *xp; int t 1 = *yp; *xp = t 1; *yp = t 0; } • • • Offset Stack 12 yp 8 xp 4 Rtn addr 0 Old %ebp Register %ecx %edx %eax %ebx – 20 – Variable yp xp t 1 t 0 %ebp -4 Old %ebx movl movl 12(%ebp), %ecx 8(%ebp), %edx (%ecx), %eax (%edx), %ebx %eax, (%edx) %ebx, (%ecx) # # # ecx edx eax ebx *xp *yp = = = yp xp *yp (t 1) *xp (t 0) eax ebx CMSC 313, F’ 09
Address Understanding Swap 123 0 x 124 456 0 x 120 0 x 11 c %eax 0 x 118 Offset %edx %ecx %ebx %esi – 21 – 12 0 x 120 0 x 110 xp 8 0 x 124 0 x 10 c 4 Rtn addr 0 x 108 0 -4 %esp %ebp yp %ebp %edi 0 x 104 movl movl 0 x 114 12(%ebp), %ecx 8(%ebp), %edx # (%ecx), %eax # (%edx), %ebx # %eax, (%edx) # %ebx, (%ecx) # 0 x 104 0 x 100 # ecx = yp edx = xp eax = *yp (t 1) ebx = *xp (t 0) *xp = eax *yp = ebx CMSC 313, F’ 09
Address Understanding Swap 123 0 x 124 456 0 x 120 0 x 11 c %eax 0 x 118 Offset %edx %ecx 0 x 120 %ebx %esi – 22 – 12 0 x 120 0 x 110 xp 8 0 x 124 0 x 10 c 4 Rtn addr 0 x 108 0 -4 %esp %ebp yp %ebp %edi 0 x 104 movl movl 0 x 114 12(%ebp), %ecx 8(%ebp), %edx # (%ecx), %eax # (%edx), %ebx # %eax, (%edx) # %ebx, (%ecx) # 0 x 104 0 x 100 # ecx = yp edx = xp eax = *yp (t 1) ebx = *xp (t 0) *xp = eax *yp = ebx CMSC 313, F’ 09
Address Understanding Swap 123 0 x 124 456 0 x 120 0 x 11 c %eax 0 x 118 %edx 0 x 124 %ecx 0 x 120 Offset %ebx %esi – 23 – 12 0 x 120 0 x 110 xp 8 0 x 124 0 x 10 c 4 Rtn addr 0 x 108 0 -4 %esp %ebp yp %ebp %edi 0 x 104 movl movl 0 x 114 12(%ebp), %ecx 8(%ebp), %edx # (%ecx), %eax # (%edx), %ebx # %eax, (%edx) # %ebx, (%ecx) # 0 x 104 0 x 100 # ecx = yp edx = xp eax = *yp (t 1) ebx = *xp (t 0) *xp = eax *yp = ebx CMSC 313, F’ 09
Address Understanding Swap 123 0 x 124 456 0 x 120 0 x 11 c %eax 456 %edx 0 x 124 %ecx 0 x 120 0 x 118 Offset %ebx %esi – 24 – 12 0 x 120 0 x 110 xp 8 0 x 124 0 x 10 c 4 Rtn addr 0 x 108 0 -4 %esp %ebp yp %ebp %edi 0 x 104 movl movl 0 x 114 12(%ebp), %ecx 8(%ebp), %edx # (%ecx), %eax # (%edx), %ebx # %eax, (%edx) # %ebx, (%ecx) # 0 x 104 0 x 100 # ecx = yp edx = xp eax = *yp (t 1) ebx = *xp (t 0) *xp = eax *yp = ebx CMSC 313, F’ 09
Address Understanding Swap 123 0 x 124 456 0 x 120 0 x 11 c %eax 456 %edx 0 x 124 %ecx 0 x 120 %ebx 0 x 118 Offset 123 %esi – 25 – 12 0 x 120 0 x 110 xp 8 0 x 124 0 x 10 c 4 Rtn addr 0 x 108 0 -4 %esp %ebp yp %ebp %edi 0 x 104 movl movl 0 x 114 12(%ebp), %ecx 8(%ebp), %edx # (%ecx), %eax # (%edx), %ebx # %eax, (%edx) # %ebx, (%ecx) # 0 x 104 0 x 100 # ecx = yp edx = xp eax = *yp (t 1) ebx = *xp (t 0) *xp = eax *yp = ebx CMSC 313, F’ 09
Address Understanding Swap 456 0 x 124 456 0 x 120 0 x 11 c %eax 456 %edx 0 x 124 %ecx 0 x 120 %ebx 0 x 118 Offset 123 %esi – 26 – 12 0 x 120 0 x 110 xp 8 0 x 124 0 x 10 c 4 Rtn addr 0 x 108 0 -4 %esp %ebp yp %ebp %edi 0 x 104 movl movl 0 x 114 12(%ebp), %ecx 8(%ebp), %edx # (%ecx), %eax # (%edx), %ebx # %eax, (%edx) # %ebx, (%ecx) # 0 x 104 0 x 100 # ecx = yp edx = xp eax = *yp (t 1) ebx = *xp (t 0) *xp = eax *yp = ebx CMSC 313, F’ 09
Address Understanding Swap 456 0 x 124 123 0 x 120 0 x 11 c %eax 456 %edx 0 x 124 %ecx 0 x 120 %ebx 0 x 118 Offset 123 %esi – 27 – 12 0 x 120 0 x 110 xp 8 0 x 124 0 x 10 c 4 Rtn addr 0 x 108 0 -4 %esp %ebp yp %ebp %edi 0 x 104 movl movl 0 x 114 12(%ebp), %ecx 8(%ebp), %edx # (%ecx), %eax # (%edx), %ebx # %eax, (%edx) # %ebx, (%ecx) # 0 x 104 0 x 100 # ecx = yp edx = xp eax = *yp (t 1) ebx = *xp (t 0) *xp = eax *yp = ebx CMSC 313, F’ 09
Indexed Addressing Modes Most General Form D(Rb, Ri, S) Mem[Reg[Rb]+S*Reg[Ri]+ D] D: Constant “displacement” 1, 2, or 4 bytes n Rb: Base register: Any of 8 integer registers n Ri: Index register: Any, except for %esp l. Unlikely you’d use %ebp, either n n S: Scale: 1, 2, 4, or 8 Special Cases – 28 – (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]] CMSC 313, F’ 09
Address Computation Examples – 29 – %edx 0 xf 000 %ecx 0 x 100 Expression Computation Address 0 x 8(%edx) 0 xf 000 + 0 x 8 0 xf 008 (%edx, %ecx) 0 xf 000 + 0 x 100 0 xf 100 (%edx, %ecx, 4) 0 xf 000 + 4*0 x 100 0 xf 400 0 x 80(, %edx, 2) 2*0 xf 000 + 0 x 80 0 x 1 e 080 CMSC 313, F’ 09
Address Computation Instruction leal Src, Dest Src is address mode expression n Set Dest to address denoted by expression n Uses n Computing addresses without a memory reference l E. g. , translation of p = &x[i]; n Computing arithmetic expressions of the form x + k*y l k = 1, 2, 4, or 8. – 30 – CMSC 313, F’ 09
Some Arithmetic Operations Format Computation Two Operand Instructions addl Src, Dest = Dest + Src subl Src, Dest = Dest - Src imull Src, Dest = Dest * Src sall Src, Dest = Dest << Src Also called shll sarl Src, Dest = Dest >> Src Arithmetic shrl Src, Dest = Dest >> Src Logical xorl Src, Dest = Dest ^ Src andl Src, Dest = Dest & Src, Dest = Dest | Src orl – 31 – CMSC 313, F’ 09
Some Arithmetic Operations Format Computation One Operand Instructions – 32 – incl Dest = Dest + 1 decl Dest = Dest - 1 negl Dest = - Dest notl Dest = ~ Dest CMSC 313, F’ 09
Using leal for Arithmetic Expressions int arith (int x, int y, int z) { int t 1 = x+y; int t 2 = z+t 1; int t 3 = x+4; int t 4 = y * 48; int t 5 = t 3 + t 4; int rval = t 2 * t 5; return rval; } – 33 – arith: pushl %ebp movl %esp, %ebp movl 8(%ebp), %eax movl 12(%ebp), %edx leal (%edx, %eax), %ecx leal (%edx, 2), %edx sall $4, %edx addl 16(%ebp), %ecx leal 4(%edx, %eax), %eax imull %ecx, %eax movl %ebp, %esp popl %ebp ret Set Up Body Finish CMSC 313, F’ 09
Understanding arith int arith (int x, int y, int z) { int t 1 = x+y; int t 2 = z+t 1; int t 3 = x+4; int t 4 = y * 48; int t 5 = t 3 + t 4; int rval = t 2 * t 5; return rval; } movl 8(%ebp), %eax movl 12(%ebp), %edx leal (%edx, %eax), %ecx leal (%edx, 2), %edx sall $4, %edx addl 16(%ebp), %ecx leal 4(%edx, %eax), %eax imull %ecx, %eax – 34 – # # # # eax edx ecx eax Offset • • • 16 z 12 y 8 x 4 Rtn addr 0 Old %ebp = = = = Stack %ebp x y x+y (t 1) 3*y 48*y (t 4) z+t 1 (t 2) 4+t 4+x (t 5) t 5*t 2 (rval) CMSC 313, F’ 09
Understanding arith int arith (int x, int y, int z) { int t 1 = x+y; int t 2 = z+t 1; int t 3 = x+4; int t 4 = y * 48; int t 5 = t 3 + t 4; int rval = t 2 * t 5; return rval; } movl 8(%ebp), %eax movl 12(%ebp), %edx leal (%edx, %eax), %ecx leal (%edx, 2), %edx sall $4, %edx addl 16(%ebp), %ecx leal 4(%edx, %eax), %eax imull %ecx, %eax – 35 – Offset • • • 16 z 12 y 8 x 4 Rtn addr 0 Old %ebp # # # # eax edx ecx eax = = = = Stack %ebp x y x+y (t 1) 3*y 48*y (t 4) z+t 1 (t 2) 4+t 4+x (t 5) t 5*t 2 (rval) CMSC 313, F’ 09
Understanding arith int arith (int x, int y, int z) { int t 1 = x+y; int t 2 = z+t 1; int t 3 = x+4; int t 4 = y * 48; int t 5 = t 3 + t 4; int rval = t 2 * t 5; return rval; } movl 8(%ebp), %eax movl 12(%ebp), %edx leal (%edx, %eax), %ecx leal (%edx, 2), %edx sall $4, %edx addl 16(%ebp), %ecx leal 4(%edx, %eax), %eax imull %ecx, %eax – 36 – Offset • • • 16 z 12 y 8 x 4 Rtn addr 0 Old %ebp # # # # eax edx ecx eax = = = = Stack %ebp x y x+y (t 1) 3*y 48*y (t 4) z+t 1 (t 2) 4+t 4+x (t 5) t 5*t 2 (rval) CMSC 313, F’ 09
Understanding arith int arith (int x, int y, int z) { int t 1 = x+y; int t 2 = z+t 1; int t 3 = x+4; int t 4 = y * 48; int t 5 = t 3 + t 4; int rval = t 2 * t 5; return rval; } movl 8(%ebp), %eax movl 12(%ebp), %edx leal (%edx, %eax), %ecx leal (%edx, 2), %edx sall $4, %edx addl 16(%ebp), %ecx leal 4(%edx, %eax), %eax imull %ecx, %eax – 37 – Offset • • • 16 z 12 y 8 x 4 Rtn addr 0 Old %ebp # # # # eax edx ecx eax = = = = Stack %ebp x y x+y (t 1) 3*y 48*y (t 4) z+t 1 (t 2) 4+t 4+x (t 5) t 5*t 2 (rval) CMSC 313, F’ 09
Understanding arith int arith (int x, int y, int z) { int t 1 = x+y; int t 2 = z+t 1; int t 3 = x+4; int t 4 = y * 48; int t 5 = t 3 + t 4; int rval = t 2 * t 5; return rval; } movl 8(%ebp), %eax movl 12(%ebp), %edx leal (%edx, %eax), %ecx leal (%edx, 2), %edx sall $4, %edx addl 16(%ebp), %ecx leal 4(%edx, %eax), %eax imull %ecx, %eax – 38 – Offset • • • 16 z 12 y 8 x 4 Rtn addr 0 Old %ebp # # # # eax edx ecx eax = = = = Stack %ebp x y x+y (t 1) 3*y 48*y (t 4) z+t 1 (t 2) 4+t 4+x (t 5) t 5*t 2 (rval) CMSC 313, F’ 09
Understanding arith int arith (int x, int y, int z) { int t 1 = x+y; int t 2 = z+t 1; int t 3 = x+4; int t 4 = y * 48; int t 5 = t 3 + t 4; int rval = t 2 * t 5; return rval; } movl 8(%ebp), %eax movl 12(%ebp), %edx leal (%edx, %eax), %ecx leal (%edx, 2), %edx sall $4, %edx addl 16(%ebp), %ecx leal 4(%edx, %eax), %eax imull %ecx, %eax – 39 – Offset • • • 16 z 12 y 8 x 4 Rtn addr 0 Old %ebp # # # # eax edx ecx eax = = = = Stack %ebp x y x+y (t 1) 3*y 48*y (t 4) z+t 1 (t 2) 4+t 4+x (t 5) t 5*t 2 (rval) CMSC 313, F’ 09
Another Example int logical(int x, int y) { int t 1 = x^y; int t 2 = t 1 >> 17; int mask = (1<<13) - 7; int rval = t 2 & mask; return rval; } movl xorl sarl andl – 40 – 8(%ebp), %eax 12(%ebp), %eax $17, %eax $8185, %eax logical: pushl %ebp movl %esp, %ebp movl xorl sarl andl 8(%ebp), %eax 12(%ebp), %eax $17, %eax $8185, %eax movl %ebp, %esp popl %ebp ret eax eax = = Set Up Body Finish x x^y t 1>>17 t 2 & 8185 CMSC 313, F’ 09
Another Example int logical(int x, int y) { int t 1 = x^y; int t 2 = t 1 >> 17; int mask = (1<<13) - 7; int rval = t 2 & mask; return rval; } movl xorl sarl andl – 41 – 8(%ebp), %eax 12(%ebp), %eax $17, %eax $8185, %eax logical: pushl %ebp movl %esp, %ebp movl xorl sarl andl 8(%ebp), %eax 12(%ebp), %eax $17, %eax $8185, %eax movl %ebp, %esp popl %ebp ret eax eax = = Set Up Body Finish x x^y (t 1) t 1>>17 (t 2) t 2 & 8185 CMSC 313, F’ 09
Another Example int logical(int x, int y) { int t 1 = x^y; int t 2 = t 1 >> 17; int mask = (1<<13) - 7; int rval = t 2 & mask; return rval; } movl xorl sarl andl – 42 – 8(%ebp), %eax 12(%ebp), %eax $17, %eax $8185, %eax logical: pushl %ebp movl %esp, %ebp movl xorl sarl andl 8(%ebp), %eax 12(%ebp), %eax $17, %eax $8185, %eax movl %ebp, %esp popl %ebp ret eax eax = = Set Up Body Finish x x^y (t 1) t 1>>17 (t 2) t 2 & 8185 CMSC 313, F’ 09
Another Example int logical(int x, int y) { int t 1 = x^y; int t 2 = t 1 >> 17; int mask = (1<<13) - 7; int rval = t 2 & mask; return rval; } logical: pushl %ebp movl %esp, %ebp movl xorl sarl andl movl %ebp, %esp popl %ebp ret 213 = 8192, 213 – 7 = 8185 movl xorl sarl andl – 43 – 8(%ebp), %eax 12(%ebp), %eax $17, %eax $8185, %eax eax eax = = Set Up Body Finish x x^y (t 1) t 1>>17 (t 2) t 2 & 8185 (rval) CMSC 313, F’ 09
Summary Machine Level Programming n Assembly code is textual form of binary object code n Low-level representation of program l Explicit manipulation of registers l Simple and explicit instructions l Minimal concept of data types l Many C control constructs must be implemented with multiple instructions – 44 – CMSC 313, F’ 09
- Slides: 44