Unformatted text preview:

Machine Level Programming IV x86 64 Procedures Data 1 Today Procedures x86 64 Arrays One dimensional Multi dimensional nested Multi level Structures Allocation Access 2 x86 64 Integer Registers Usage Conventions rax Return value r8 Argument 5 rbx Callee saved r9 Argument 6 rcx Argument 4 r10 Caller saved rdx Argument 3 r11 Caller Saved rsi Argument 2 r12 Callee saved rdi Argument 1 r13 Callee saved rsp Stack pointer r14 Callee saved rbp Callee saved r15 Callee saved 4 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 callee saved 2 caller saved 1 return value also usable as caller saved 1 special stack pointer 5 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 rsp unused 16 loc 1 24 loc 0 7 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 12 Today Procedures x86 64 Arrays One dimensional Multi dimensional nested Multi level Structures Allocation Access 14 Structure Allocation struct rec int a 3 int i struct rec n Memory Layout a 0 i n 12 16 20 Concept Contiguously allocated region of memory Refer to members within structure by names Members may be of different types 15 Structure Access struct rec int a 3 int i struct rec n r a 0 r 12 i n 12 16 20 Accessing Structure Member Pointer indicates first byte of structure Access elements with offsets void set i struct rec r int val r i val IA32 Assembly edx val eax r movl edx 12 eax Mem r 12 val 16 Generating Pointer to Structure Member r struct rec int a 3 int i struct rec n Generating Pointer to Array Element Offset of each structure member determined at compile time Arguments Mem ebp 8 r Mem ebp 12 idx a 0 r idx 4 i n 12 16 20 int get ap struct rec r int idx return r a idx movl sall addl 12 ebp eax 2 eax 8 ebp eax Get idx idx 4 r idx 4 17 Following Linked List C Code void set val struct rec r int val while r int i r i r a i val r r n L17 movl movl movl testl jne struct rec int a 3 int i struct rec n a i n 12 16 20 0 Element i Register Value edx r ecx val 12 edx eax ecx edx eax 4 16 edx edx edx edx L17 loop r i r a i val r r n Test r If 0 goto loop 18 Today Procedures x86 64 Arrays One dimensional Multi dimensional nested Multi level Structures 19 Basic Data Types Integral Stored operated on in general integer registers Signed vs unsigned depends on instructions used Intel ASMBytes 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 ASMBytes C Single s 4 float Double l 8 double Extended t 10 12 16 long double 20 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 x x 8 char p 3 x 16 x 24 IA32 x x 4 x 8 x 12 x86 64 x x 8 x 16 x 24 21 Array Access Basic Principle T A L Array of data type T and length L Identifier A can be used as a pointer to array element 0 Type T 1 int val 5 x Reference Type 5 x 4 2 x 8 1 x 12 3 x 16 x 20 Value val 4 int 3 val int x val 1 int x 4 val 2 int x 8 val 5 int val 1 int 5 val i int x 4 i 22 Array Example define ZLEN 5 typedef int zip dig ZLEN zip dig ut 7 8 7 1 2 zip dig mit 0 2 1 3 9 zip dig ucb 9 4 7 2 0 zip dig 7 ut 16 20 0 zip dig mit 36 56 7 24 2 40 9 zip dig ucb 8 28 1 44 4 60 1 32 3 48 7 64 2 9 52 2 68 36 56 0 72 76 Declaration zip dig ut equivalent to int ut 5 Example arrays were allocated in successive 20 byte blocks Not guaranteed to happen in general 23 Array Accessing Example zip dig 7 ut 16 8 20 7 24 1 28 int get digit zip dig z int dig return z dig 32 IA32 edx z eax dig movl edx eax 4 eax 2 z dig 36 Register edx contains starting address of array Register eax contains array index Desired digit at 4 eax edx Use memory reference edx eax 4 24 Array Loop Example IA32 void zincr zip dig z int i for i 0 i ZLEN i z i edx movl L4 addl addl cmpl jne z 0 eax eax i loop 1 edx eax 4 z i 1 eax i 5 eax i 5 L4 if goto loop 25 Pointer Loop Example IA32 void zincr p zip dig z int zend z ZLEN do z z while z zend edx movl L8 addl addl cmpl jne z vz 0 eax 1 edx eax 4 eax 20 eax L8 void zincr v zip dig z void vz z int i 0 do int vz i i ISIZE while i ISIZE ZLEN i 0 loop Increment vz i i 4 Compare i 20 if goto loop 26 Nested Array Example define PCOUNT 4 zip dig pgh PCOUNT 1 5 2 0 6 1 5 2 1 3 1 5 2 1 7 1 5 2 2 1 zip dig pgh 4 1 5 2 0 6 1 5 2 1 3 1 5 2 1 7 1 5 2 2 1 76 96 116 136 156 zip dig pgh 4 equivalent to int pgh 4 5 Variable pgh array of 4 elements allocated contiguously Each element is an array of 5 int s allocated contiguously Row Major ordering of all elements guaranteed 27 Multidimensional Nested Arrays …


View Full Document

UT CS 429H - Machine-Level Programming IV - x86-64 Procedures, Data

Loading Unlocking...
Login

Join to view Machine-Level Programming IV - x86-64 Procedures, Data and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Machine-Level Programming IV - x86-64 Procedures, Data and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?