1 / 29

Machine-Level Programming I: Introduction Apr. 14, 2008

Machine-Level Programming I: Introduction Apr. 14, 2008. EECS 213. Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations. X86 Evolution: Programmer’s view. Programmer-Visible State EIP Program Counter Address of next instruction

jaimin
Download Presentation

Machine-Level Programming I: Introduction Apr. 14, 2008

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Machine-Level Programming I:IntroductionApr. 14, 2008 EECS 213 • Topics • Assembly Programmer’s Execution Model • Accessing Information • Registers • Memory • Arithmetic operations

  2. X86 Evolution: Programmer’s view

  3. Programmer-Visible State EIP Program Counter Address of next instruction Register File Heavily used program data Condition Codes Store status information about most recent arithmetic operation Used for conditional branching Memory Byte addressable array Code, user data, (some) OS data Includes stack used to support procedures Assembly Programmer’s View CPU Memory Addresses Registers E I P Object Code Program Data OS Data Data Condition Codes Instructions Stack

  4. Turning C into Object Code • Code in files p1.c p2.c • Compile with command: gcc -O p1.c p2.c -o p • Use optimizations (-O) • Put resulting binary in file p C program (p1.c p2.c) text Compiler (gcc -S) Asm program (p1.s p2.s) text Assembler (gcc or as) Object program (p1.o p2.o) Static libraries (.a) binary Linker (gcc orld) binary Executable program (p)

  5. Compiling Into Assembly Generated 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 -S code.c • Produces file code.s

  6. Assembly Characteristics • Minimal Data Types • “Integer” data of 1, 2, or 4 bytes • Data values • Addresses • Floating point data of 4, 8, or 10 bytes • No aggregate types such as arrays or structures • Just contiguously allocated bytes in memory • Primitive Operations • Perform arithmetic function on register or memory data • Transfer data between memory and register • Load data from memory into register • Store register data into memory • Transfer control • Unconditional jumps to/from procedures • Conditional branches

  7. Object Code Code for sum • Assembler • Translates .s into .o • Binary encoding of each instruction • Nearly-complete image of executable code • Missing linkages between code in different files • Linker • Resolves references between files • One of the object codes must contain function main(); • Combines with static run-time libraries • E.g., code for malloc, printf • Some libraries are dynamically linked • Linking occurs when program begins execution 0x401040 <sum>: 0x55 0x89 0xe5 0x8b 0x45 0x0c 0x03 0x45 0x08 0x89 0xec 0x5d 0xc3 • Total of 13 bytes • Each instruction 1, 2, or 3 bytes • Starts at address 0x401040

  8. Machine Instruction Example int t = x+y; • C Code • Add two signed integers • Assembly • Add 2 4-byte integers • “Long” words in GCC parlance • Same instruction whether signed or unsigned • Operands: x: Register %eax y: Memory M[%ebp+8] t: Register %eax • Return function value in %eax • Object Code • 3-byte instruction • Stored at address 0x401046 addl 8(%ebp),%eax Similar to expression x += y 0x401046: 03 45 08

  9. Disassembling Object Code Disassembled • Disassembler objdump -d p • Useful tool for examining object code • Analyzes bit pattern of series of instructions • Produces approximate rendition of assembly code • Can be run on either a.out (complete executable) or .o file 00401040 <_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 lea 0x0(%esi),%esi

  10. Data Formats • “word” – 16b data type due to its origins • 32b – double word • 64b – quad words • The overloading of “l” in GAS causes no problems since FP involves different operations & registers

  11. Accessing Information • 8 32bit registers • Six of them mostly for general purpose • Last two point to key data in a process stack • Two low-order bytes of the first 4 can be access directly (low-order 16bit as well) 31 15 8 7 0 %eax %ax %ah %al %ecx %cx %ch %cl %edx %dx %dh %dl %ebx %bx %bh %bl %esi %si %edi %di Stack pointer %esp %sp Frame pointer %ebp %bp

  12. Instruction formats • Most instructions have 1 or 2 operands • operator [source[, destination]] • Operand types: • Immediate – constant, denoted with a “$” in front • Register – either 8 or 16 or 32bit registers • Memory – location given by an effective address • Source: constant or value from register or memory • Destination: register or memory

  13. Operand specifiers • Operand forms • Imm means a number • Ea means a register form, e.g., %eax • s is 1, 2, 4 or 8 (called the scale factor) • Memory form is the most general; subsets also work, e.g., • Absolute: Imm M[Imm] • Base + displacement: Imm(Eb)  M[Imm + R[Eb]] • Operand values • R[Ea] means "value in register" • M[loc] means "value in memory location loc"

  14. Operand quiz

  15. Moving data • Among the most common instructions • IA32 restriction – cannot move from one memory location to another with one instruction • Note the differences between movb, movsbl and movzbl • Last two work with the stack pushl %ebp = subl $4, %esp movl %ebp, (%esp) • Since stack is part of program mem, you can really access all

  16. movl Operand Combinations Source Destination C Analog • Cannot do memory-memory transfers with single instruction Reg movl $0x4,%eax temp = 0x4; Imm Mem movl $-147,(%eax) *p = -147; Reg movl %eax,%edx temp2 = temp1; movl Reg Mem movl %eax,(%edx) *p = temp; Mem Reg movl (%eax),%edx temp = *p;

  17. Using simple addressing modes swap: pushl %ebp movl %esp,%ebp pushl %ebx movl 8(%ebp),%edx movl 12(%ebp),%ecx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret Declares xp as being a pointer to an int Set Up void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } Body Read value stored in location xp and store it in t0 Finish

  18. Address Understanding swap 123 0x124 456 0x120 0x11c void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } 0x118 Offset 0x114 yp 12 0x120 0x110 xp 8 0x124 0x10c 4 Rtn adr 0x108 0 Old %ebp %ebp 0x104 Register Variable %ecx yp %edx xp %eax t1 %ebx t0 -4 Old %ebx 0x100 movl 12(%ebp),%ecx # ecx = yp movl 8(%ebp),%edx # edx = xp movl (%ecx),%eax # eax = *yp (t1) movl (%edx),%ebx # ebx = *xp (t0) movl %eax,(%edx) # *xp = eax movl %ebx,(%ecx) # *yp = ebx

  19. Address %eax %edx %ecx %ebx %esi %edi %esp %ebp 0x104 Understanding swap 123 0x124 456 0x120 0x11c 0x118 Offset 0x114 yp 12 0x120 0x110 xp 8 0x124 0x10c 4 Rtn adr 0x108 0 %ebp 0x104 -4 0x100 movl 12(%ebp),%ecx # ecx = yp movl 8(%ebp),%edx # edx = xp movl (%ecx),%eax # eax = *yp (t1) movl (%edx),%ebx # ebx = *xp (t0) movl %eax,(%edx) # *xp = eax movl %ebx,(%ecx) # *yp = ebx

  20. Address %eax %edx %ecx 0x120 %ebx %esi %edi %esp 0x104 %ebp Understanding swap 123 0x124 456 0x120 0x11c 0x118 Offset 0x114 yp 12 0x120 0x110 xp 8 0x124 0x10c 4 Rtn adr 0x108 0 %ebp 0x104 -4 0x100 movl 12(%ebp),%ecx # ecx = yp movl 8(%ebp),%edx # edx = xp movl (%ecx),%eax # eax = *yp (t1) movl (%edx),%ebx # ebx = *xp (t0) movl %eax,(%edx) # *xp = eax movl %ebx,(%ecx) # *yp = ebx

  21. Address %eax 0x124 %edx %ecx 0x120 %ebx %esi %edi %esp 0x104 %ebp Understanding swap 123 0x124 456 0x120 0x11c 0x118 Offset 0x114 yp 12 0x120 0x110 xp 8 0x124 0x10c 4 Rtn adr 0x108 0 %ebp 0x104 -4 0x100 movl 12(%ebp),%ecx # ecx = yp movl 8(%ebp),%edx # edx = xp movl (%ecx),%eax # eax = *yp (t1) movl (%edx),%ebx # ebx = *xp (t0) movl %eax,(%edx) # *xp = eax movl %ebx,(%ecx) # *yp = ebx

  22. Address 456 %eax 0x124 %edx %ecx 0x120 %ebx %esi %edi %esp 0x104 %ebp Understanding swap 123 0x124 456 0x120 0x11c 0x118 Offset 0x114 yp 12 0x120 0x110 xp 8 0x124 0x10c 4 Rtn adr 0x108 0 %ebp 0x104 -4 0x100 movl 12(%ebp),%ecx # ecx = yp movl 8(%ebp),%edx # edx = xp movl (%ecx),%eax # eax = *yp (t1) movl (%edx),%ebx # ebx = *xp (t0) movl %eax,(%edx) # *xp = eax movl %ebx,(%ecx) # *yp = ebx

  23. Address 456 %eax 0x124 %edx 0x120 %ecx %ebx 123 %esi %edi %esp 0x104 %ebp Understanding swap 123 0x124 456 0x120 0x11c 0x118 Offset 0x114 yp 12 0x120 0x110 xp 8 0x124 0x10c 4 Rtn adr 0x108 0 %ebp 0x104 -4 0x100 movl 12(%ebp),%ecx # ecx = yp movl 8(%ebp),%edx # edx = xp movl (%ecx),%eax # eax = *yp (t1) movl (%edx),%ebx # ebx = *xp (t0) movl %eax,(%edx) # *xp = eax movl %ebx,(%ecx) # *yp = ebx

  24. Address 456 %eax 0x124 %edx 0x120 %ecx %ebx 123 %esi %edi %esp 0x104 %ebp Understanding swap 456 0x124 456 0x120 0x11c 0x118 Offset 0x114 yp 12 0x120 0x110 xp 8 0x124 0x10c 4 Rtn adr 0x108 0 %ebp 0x104 -4 0x100 movl 12(%ebp),%ecx # ecx = yp movl 8(%ebp),%edx # edx = xp movl (%ecx),%eax # eax = *yp (t1) movl (%edx),%ebx # ebx = *xp (t0) movl %eax,(%edx) # *xp = eax movl %ebx,(%ecx) # *yp = ebx

  25. Address 456 %eax 0x124 %edx 0x120 %ecx %ebx 123 %esi %edi %esp 0x104 %ebp Understanding swap 456 0x124 123 0x120 0x11c 0x118 Offset 0x114 yp 12 0x120 0x110 xp 8 0x124 0x10c 4 Rtn adr 0x108 0 %ebp 0x104 -4 0x100 movl 12(%ebp),%ecx # ecx = yp movl 8(%ebp),%edx # edx = xp movl (%ecx),%eax # eax = *yp (t1) movl (%edx),%ebx # ebx = *xp (t0) movl %eax,(%edx) # *xp = eax movl %ebx,(%ecx) # *yp = ebx

  26. Address Computation Instruction • lealSrc,Dest • leal = Load Effective Address • Src is address mode expression • Set Dest to address denoted by expression • Uses • Computing address without doing memory reference • E.g., translation of p = &x[i]; • Computing arithmetic expressions of the form x + k*y • k = 1, 2, 4, or 8. • Leal 7(%edx,%edx,4), %eax • when %edx=x, %eax becomes 5x+7

  27. Address computation quiz

  28. Some arithmetic operations

  29. Arithmetic quiz

More Related