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 14 Basic Data Types Integral Stored operated on in general integer registers Signed vs unsigned depends on instructions used Intel byte word double word quad word ASM b w l q Bytes 1 2 4 8 C unsigned unsigned unsigned unsigned char short int long int x86 64 Floating Point Stored operated on in floating point registers Intel Single Double Extended ASM s l t Bytes 4 8 10 12 16 C float double long double 15 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 16 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 int val 5 1 x Reference val 4 val val 1 val 2 val 5 val 1 val i 5 x 4 2 x 8 Type Value int int int int int int int 3 x x 4 x 8 5 x 4i 1 x 12 3 x 16 x 20 17 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 ut 7 16 zip dig mit 20 0 36 zip dig ucb 7 24 2 40 9 56 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 cmu equivalent to int ut 5 Example arrays were allocated in successive 20 byte blocks Not guaranteed to happen in general 18 Array Accessing Example zip dig ut 7 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 19 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 20 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 21 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 22 Multidimensional Nested Arrays Declaration T A R C 2D array of data type T R rows C columns Type T element requires K bytes Array Size A 0 0 A 0 C 1 A R 1 0 A R 1 C 1 R C K bytes Arrangement Row Major Ordering int A R C A 0 0 A A 0 1 C 1 0 A 1 C 1 A A R 1 R 1 0 C 1 4 R C Bytes 23 Nested Array Row Access Row Vectors A i is array of C elements Each element of type T requires K bytes Starting address A i C K int A R C A 0 A 0 0 A A i A 0 C 1 A i 0 A i C 4 A R 1 A i C 1 A R 1 0 A R 1 C 1 A R 1 C 4 24 Nested Array Row Access Code int get pgh zip int index return pgh index 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 eax index leal eax eax 4 eax 5 index leal pgh eax 4 eax pgh 20 index Row Vector pgh index is array of 5 int s Starting address pgh 20 index IA32 Code Computes and returns address Compute as pgh 4 index 4 index 25 Nested Array Row Access Array Elements A i j is element of type T which requires K bytes Address A i C K j K A i C j K int A R C A 0 A 0 0 A A i A 0 C 1 …


View Full Document

UT CS 429H - Machine-Level Programming IV

Loading Unlocking...
Login

Join to view Machine-Level Programming IV 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 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?