Carnegie Mellon Introduction to Computer Systems 15 213 18 243 spring 2009 8th Lecture Feb 5th Instructors Gregory Kesden and Markus P schel Carnegie Mellon Last Time For loops for loop while loop do while loop goto version for loop while loop goto jump to middle version Switch statements Jump tables jmp L62 edx 4 Decision trees not shown Jump table section rodata align 4 L62 long L61 long L56 long L57 long L58 long L61 long L60 long L60 x x x x x x x 0 1 2 3 4 5 6 Carnegie Mellon Last Time Procedures IA32 call return esp ebp local variables recursive functions Caller Frame Arguments eax Caller Save ebp edx Saved Registers Local Variables ecx ebx Callee Save esi edi Special esp ebp Return Addr Old ebp esp Argument Build Carnegie Mellon Today Procedures x86 64 Arrays One dimensional Multi dimensional nested Multi level Structures Carnegie Mellon x86 64 Integer Registers rax eax r8 r8d rbx ebx r9 r9d rcx ecx r10 r10d rdx edx r11 r11d rsi esi r12 r12d rdi edi r13 r13d rsp esp r14 r14d rbp ebp r15 r15d Twice the number of registers Accessible as 8 16 32 64 bits Carnegie Mellon x86 64 Integer Registers rax Return value r8 Argument 5 rbx Callee saved r9 Argument 6 rcx Argument 4 r10 Callee saved rdx Argument 3 r11 Used for linking rsi Argument 2 r12 C Callee saved rdi Argument 1 r13 Callee saved rsp Stack pointer r14 Callee saved rbp Callee saved r15 Callee saved Carnegie Mellon x86 64 Registers Arguments passed to functions via registers If more than 6 integral parameters then pass rest on stack These registers can be used as caller saved as well All references to stack frame via stack pointer Eliminates need to update ebp rbp Other Registers 6 1 callee saved 2 or 3 have special uses Carnegie Mellon x86 64 Long Swap void swap long xp long yp long t0 xp long t1 yp xp t1 yp t0 swap movq movq movq movq ret Operands passed in registers First xp in rdi second yp in rsi 64 bit pointers No stack operations required except ret Avoiding stack Can hold all local information in registers rdi rdx rsi rax rax rdi rdx rsi Carnegie Mellon x86 64 Locals in the Red Zone Swap using local array void swap a long xp long yp volatile long loc 2 loc 0 xp loc 1 yp xp loc 1 yp loc 0 swap a movq movq movq movq movq movq movq movq ret Avoiding Stack Pointer Change Can hold all information within small window beyond stack pointer rdi rax rax 24 rsp rsi rax rax 16 rsp 16 rsp rax rax rdi 24 rsp rax rax rsi rtn Ptr 8 unused 16 loc 1 24 loc 0 rsp Carnegie Mellon x86 64 NonLeaf without Stack Frame long scount 0 Swap a i a i 1 void swap ele se long a int i swap a i a i 1 scount No values held while swap being invoked No callee save registers needed swap ele se movslq esi rsi Sign extend i leaq rdi rsi 8 rdi a i leaq 8 rdi rsi a i 1 call swap swap incq scount rip scount ret Carnegie Mellon x86 64 Call using Jump long scount 0 Swap a i a i 1 void swap ele long a int i swap a i a i 1 swap ele movslq esi rsi Sign extend i disappear leaq rdi rsi 8 rdi Will a i leaq 8 rdi rsi a i 1 Blackboard jmp swap swap Carnegie Mellon x86 64 Call using Jump long scount 0 Swap a i a i 1 void swap ele long a int i swap a i a i 1 When swap executes ret it will return from swap ele Possible since swap is a tail call no instructions afterwards swap ele movslq esi rsi Sign extend i leaq rdi rsi 8 rdi a i leaq 8 rdi rsi a i 1 jmp swap swap Carnegie Mellon x86 64 Stack Frame Example long sum 0 Swap a i a i 1 void swap ele su long a int i swap a i a i 1 sum a i Keeps values of a and i in callee save registers Must set up stack frame to save these registers swap ele su movq rbx 16 rsp movslq esi rbx movq r12 8 rsp movq rdi r12 leaq rdi rbx 8 rdi subq 16 rsp leaq 8 rdi rsi call swap movq r12 rbx 8 rax addq rax sum rip movq rsp rbx movq 8 rsp r12 addq 16 rsp ret Blackboard Carnegie Mellon Understanding x86 64 Stack Frame swap ele su movq rbx 16 rsp movslq esi rbx movq r12 8 rsp movq rdi r12 leaq rdi rbx 8 rdi subq 16 rsp leaq 8 rdi rsi call swap movq r12 rbx 8 rax addq rax sum rip movq rsp rbx movq 8 rsp r12 addq 16 rsp ret Save rbx Extend save i Save r12 Save a a i Allocate stack frame a i 1 swap a i sum a i Restore rbx Restore r12 Deallocate stack frame Carnegie Mellon Understanding x86 64 Stack Frame swap ele su movq rbx 16 rsp movslq esi rbx movq r12 8 rsp movq rdi r12 leaq rdi rbx 8 rdi subq 16 rsp leaq 8 rdi rsi call swap movq r12 rbx 8 rax addq rax sum rip movq rsp rbx movq 8 rsp r12 addq 16 rsp ret Save rbx rsp rtn addr Extend save i 8 r12 Save r12 16 rbx Save a a i Allocate stack frame a i 1 rtn addr swap 8 r12 a i sum a i rsp rbx Restore rbx Restore r12 Deallocate stack frame Carnegie Mellon Interesting Features of Stack Frame Allocate entire frame at once All stack accesses can be relative to rsp Do by decrementing stack pointer Can delay allocation since safe to temporarily use red zone Simple deallocation Increment stack pointer No base frame pointer needed Carnegie Mellon x86 64 Procedure Summary Heavy use of registers Parameter passing More temporaries since more registers Minimal use of stack Sometimes none Allocate deallocate entire block Many tricky optimizations What kind of stack frame to use Calling with jump Various allocation techniques Carnegie Mellon Today Procedures x86 64 Arrays One dimensional Multi dimensional nested Multi level Structures Carnegie Mellon Basic Data Types Integral Stored operated on in general integer registers Signed vs unsigned depends on instructions used Intel GAS Bytes C byte b 1 unsigned char word w 2 unsigned short double word l 4 unsigned int quad word q 8 unsigned long int x86 64 Floating Point Stored operated on in floating point registers Intel GAS Single s Double Extended Bytes C 4 float l 8 double t 10 12 16 long double Carnegie Mellon Array Allocation Basic Principle T A L Array of data type T and length L Contiguously allocated region of L sizeof T bytes char string 12 x x 12 int val 5 x x 4 x 8 x 12 x 16 x 20 double a 3 …
View Full Document