X86-Basics-Handout.Pdf

X86-Basics-Handout.Pdf

CSAPP book is very useful and well-aligned with class for the remainder of the course. Turning C into Machine Code C Code void sumstore(long x, long y, long *dest) { long t = x + y; *dest = t; Generated x86 Assembly Code C to Machine Code and x86 Basics } Human-readable language close to machine code. sum.c sum: ISA context and x86 history addq %rdi,%rsi movq %rsi,(%rdx) Translation tools: C --> assembly <--> machine code compiler (CS 301) retq sum.s x86 Basics: gcc -Og -S sum.c assembler Registers Data movement instructions Object Code linker Memory addressing modes 01010101100010011110010110 00101101000101000011000000 Arithmetic instructions Executable: sum 00110100010100001000100010 Resolve references between object files, 01111011000101110111000011 sum.o 2 libraries, (re)locate data 3 01010101100010011110010110 00101101000101000011000000 sub-registers 00110100010100001000100010 Disassembling Object Code 01111011000101110111000011 x86-64 registers 1985: 32-bit extended register %eax ... %rax Return Value 1978: 16-bit register %ax Disassembled by objdump -d sum %rbx %ah %al 0000000000400536 <sumstore>: %rax %eax %rcx Argument 4 %ax 400536: 48 01 fe add %rdi,%rsi Disassembler 400539: 48 89 32 mov %rsi,(%rdx) %rdx Argument 3 high and low bytes 40053c: c3 retq %rsi Argument 2 of %ax %rdi Argument 1 %rsi %esi %si %rsp Special Purpose: Stack Pointer %rbp Object Disassembled by GDB Low 32 bits of %rsi Low 16 bits of %rsi %r8 Argument 5 0x00400536: 0x0000000000400536 <+0>: add %rdi,%rsi %r9 Argument 6 0x48 0x0000000000400539 <+3>: mov %rsi,(%rdx) historical artifacts 0x01 0x000000000040053c <+6>: retq %r10 0xfe $ gdb sum %r11 0x48 %r8 %r8d %r12 0x89 (gdb) disassemble sumstore %r13 32-bit sub-register to match 0x32 (disassemble function) 0xc3 %r14 (gdb) x/7b sum %r15 (examine the 13 bytes starting at sum) 5 64-bits / 8 bytes Some have special uses for particular instructions x86: Three Basic Kinds of Instructions Data movement instructions 1. Data movement between memory and register mov_ Source, Dest Load data from memory into register data size _ is one of {b, w, l, q} %reg ß Mem[address] movq: move 8-byte “quad word” Memory is an Store register data into memory movl: move 4-byte “long word” array[] of bytes! Mem[address] ß %reg movw: move 2-byte “word” Historical terms based on the 16-bit days, movb: move 1-byte “byte” not the current machine word size (64 bits) 2. Arithmetic/logic on register or memory data Source/Dest operand types: c = a + b; z = x << y; i = h & g; Immediate: Literal integer data Examples: $0x400 $-533 3. Comparisons and Control flow to choose next instruction Register: One of 16 registers Examples: %rax %rdx Unconditional jumps to/from procedures Memory: consecutive bytes in memory, at address held by register Conditional branches Direct addressing: (%rax) With displacement/offset: 8(%rsp) 12 13 Memory Addressing Modes Pointers and Memory Addressing Indirect (R) Mem[Reg[R]] void swap(long* xp, long* yp){ swap: Register R specifies memory address: movq (%rcx),%rax long t0 = *xp; movq (%rdi),%rax long t1 = *yp; movq (%rsi),%rdx *xp = t1; movq %rdx,(%rdi) Displacement D(R) Mem[Reg[R]+D] *yp = t0; movq %rax,(%rsi) } retq Register R specifies base memory address (e.g. base of an object) Displacement D specifies literal offset (e.g. a field in the object) movq %rdx,8(%rsp) Registers Memory Register Variable Address %rdi 0x120 0x120 %rdi xp General Form: D(Rb,Ri,S) Mem[Reg[Rb] + S*Reg[Ri] + D] %rsi 0x108 0x118 %rsi yp D: Literal “displacement” value represented in 1, 2, or 4 bytes 0x110 %rax t0 %rax Rb: Base register: Any register 0x108 %rdx t1 %rdx Ri: Index register: Any except %rsp 0x100 S: Scale: 1, 2, 4, or 8 15 16 Compute address given by Address Computation Examples ex this addressing mode expression Load effective address and store it here. General Addressing Modes leaq Src, Dest D(Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri] + D] Register contents DOES NOT ACCESS MEMORY !!! Special Cases: Implicitly: %rdx 0xf000 (Rb,Ri) Mem[Reg[Rb]+Reg[Ri]] (S=1,D=0) Uses: "address of" "Lovely Efficient Arithmetic" %rcx 0x100 D(Rb,Ri) Mem[Reg[Rb]+Reg[Ri]+D] (S=1) (Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]] (D=0) p = &x[i]; x + k*I, where k = 1, 2, 4, or 8 Address Expression Address Computation Address leaq vs. movq 0x8(%rdx) Registers Memory Address Assembly Code %rax 0x400 0x120 leaq (%rdx,%rcx,4), %rax (%rdx,%rcx) %rbx 0xf 0x118 movq (%rdx,%rcx,4), %rbx (%rdx,%rcx,4) leaq (%rdx), %rdi %rcx 0x4 0x8 0x110 movq (%rdx), %rsi %rdx 0x100 0x80(,%rdx,2) 0x10 0x108 %rdi 0x1 0x100 %rsi 17 18 Call Stack Call Stack: Push, Pop Stack “Bottom” Stack “Bottom” higher addresses Memory region for temporary storage pushq Src managed with stack discipline. 1. Fetch value from Src higher 2. Decrement %rsp by 8 (why 8?) Stack Pointer: %rsp addresses -8 %rsp holds lowest stack address 3. Store value at new address (address of "top" element) given by %rsp lower addresses Stack “Top” stack grows Stack “Bottom” toward popq Dest higher addresses lower addresses Stack Pointer: %rsp 1. Load value from address %rsp 2. Write value to Dest 3. Increment %rsp by 8 Stack Pointer: %rsp Stack “Top” +8 Those bits are still there; lower addresses Stack “Top” we’re just not using them. 20 21 Procedure Preview (more soon) Arithmetic Operations call, ret, push, pop Two-operand instructions: Format Computation Procedure arguments passed in 6 registers: … addq Src,Dest Dest = Dest + Src %rax Return Value %r8 Argument 5 %rbx %r9 Argument 6 Caller subq Src,Dest Dest = Dest – Src argument order %rcx Argument 4 %r10 Frame imulq Src,Dest Dest = Dest * Src %rdx Argument 3 %r11 Extra Arguments shlq Src,Dest Dest = Dest << Src a.k.a salq %rsi Argument 2 %r12 to callee %rdi Argument 1 %r13 sarq Src,Dest Dest = Dest >> Src Arithmetic Return Address %rsp Stack pointer %r14 shrq Src,Dest Dest = Dest >> Src Logical %rbp %r15 xorq Src,Dest Dest = Dest ^ Src Return value in %rax. andq Src,Dest Dest = Dest & Src orq Src,Dest Dest = Dest | Src Saved Registers Callee One-operand (unary) instructions Allocate/push new stack frame for each + Frame procedure call. Local Variables incq Dest Dest = Dest + 1 increment Stack pointer decq Dest Dest = Dest – 1 decrement Some local variables, %rsp negq Dest Dest = -Dest negate saved register values, extra arguments notq Dest Dest = ~Dest bitwise complement Deallocate/pop frame before return. 22 See CSAPP 3.5.5 for: mulq, cqto, idivq, divq 23 leaq for arithmetic Another example long arith(long x, long y, Register Use(s) Register Use(s) long z){ %rdi Argument x long logical(long x, long y){ %rdi Argument x long t1 = x+y; %rsi Argument y long t1 = x^y; %rsi Argument y long t2 = z+t1; long t2 = t1 >> 17; long t3 = x+4; %rdx Argument z long mask = (1<<13) - 7; %rax long t4 = y * 48; %rax long rval = t2 & mask; long t5 = t3 + t4; return rval; %rcx long rval = t2 * t5; } return rval; } arith: § Instructions in different leaq (%rdi,%rsi), %rax order from C code logical: addq %rdx, %rax § Some expressions require movq %rdi,%rax leaq (%rsi,%rsi,2), %rdx multiple instructions xorq %rsi,%rax salq $4, %rdx § Some instructions cover sarq $17,%rax leaq 4(%rdi,%rdx), %rcx multiple expressions andq $8185,%rax imulq %rcx, %rax retq ret § Same x86 code by compiling: (x+y+z)*(x+4+48*y) 24 25.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    4 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us