data structures in memory
play

Data Structures in Memory! Arrays One dimensional Multi - PDF document

University of Washington Data Structures in Memory! Arrays One dimensional Multi dimensional (nested) M l i di i l ( d) Multi level Structs Alignment Unions 23 April 2012 Data Structures 1 University of


  1. University of Washington Data Structures in Memory! � Arrays � One ‐ dimensional � Multi ‐ dimensional (nested) M l i di i l ( d) � Multi ‐ level � Structs � Alignment � Unions 23 April 2012 Data Structures 1 University of Washington Data Structures in Assembly… � Arrays? � Strings? � Structs? 23 April 2012 Data Structures 2

  2. University of Washington Array Allocation � Basic Principle � T A[N]; � Array of data type T and length N � Contiguously allocated region of N * sizeof(T) bytes C ti l ll t d i f N * i f(T) b t 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 x + 16 x + 24 char *p[3]; IA32 x x + 4 x + 8 x + 12 x86 ‐ 64 x x + 8 x + 16 x + 24 23 April 2012 Data Structures 3 University of Washington Array Access � Basic Principle � T A[N]; � Array of data type T and length N � Identifier A can be used as a pointer to array element 0: Type T* Id tifi A b d i t t l t 0 T T* 9 8 1 9 5 int val[5]; x x + 4 x + 8 x + 12 x + 16 x + 20 � Reference Type Value � val[4] int � val int * � val+1 int * � &val[2] int * � val[5] int � *(val+1) int � val + i int * 23 April 2012 Data Structures 4

  3. University of Washington Array Access � Basic Principle � T A[N]; � Array of data type T and length N � Identifier A can be used as a pointer to array element 0: Type T* Id tifi A b d i t t l t 0 T T* 9 8 1 9 5 int val[5]; x x + 4 x + 8 x + 12 x + 16 x + 20 � Reference Type Value � val[4] int 5 � val int * x � val+1 int * x + 4 � &val[2] int * x + 8 � val[5] int ?? � *(val+1) int 8 � val + i int * x + 4 i 23 April 2012 Data Structures 5 University of Washington Array Example typedef int zip_dig[5]; zip_dig cmu = { 1, 5, 2, 1, 3 }; zip dig uw zip_dig uw = { 9, 8, 1, 9, 5 }; { 9, 8, 1, 9, 5 }; zip_dig ucb = { 9, 4, 7, 2, 0 }; 23 April 2012 Data Structures 6

  4. University of Washington Array Example typedef int zip_dig[5]; zip_dig cmu = { 1, 5, 2, 1, 3 }; zip dig uw zip_dig uw = { 9, 8, 1, 9, 5 }; { 9, 8, 1, 9, 5 }; zip_dig ucb = { 9, 4, 7, 2, 0 }; 1 5 2 1 3 zip_dig cmu; 16 20 24 28 32 36 9 8 1 9 5 zip_dig uw ; 36 40 44 48 52 56 9 4 7 2 0 zip_dig ucb; 56 60 64 68 72 76 Declaration “ zip_dig uw ” equivalent to “ int uw[5] ” � Example arrays were allocated in successive 20 byte blocks � � Not guaranteed to happen in general 23 April 2012 Data Structures 7 University of Washington Array Accessing Example 9 8 1 9 5 zip_dig uw; 36 40 44 48 52 56 int get_digit (zip_dig z, int dig) { return z[dig]; � Register %edx contains } starting address of array IA32 IA32 � Register %eax contains array index # %edx = z # %eax = dig � Desired digit at movl (%edx,%eax,4),%eax # z[dig] 4*%eax + %edx � Use memory reference (%edx,%eax,4) 23 April 2012 8

  5. University of Washington Referencing Examples 1 5 2 1 3 zip_dig cmu; 16 20 24 28 32 36 9 8 1 9 5 zip_dig uw ; 36 40 44 48 52 56 9 4 7 2 0 zip_dig ucb; 56 60 64 68 72 76 � Reference Address Value Guaranteed? uw[3] 36 + 4* 3 = 48 9 uw[6] [6] 36 + 4* 6 36 + 4* 6 = 60 60 4 4 What are these values? uw[-1] 36 + 4*-1 = 32 3 cmu[15] 16 + 4*15 = 76 ?? 23 April 2012 Data Structures 9 University of Washington Referencing Examples 1 5 2 1 3 zip_dig cmu; 16 20 24 28 32 36 9 8 1 9 5 zip_dig uw ; 36 40 44 48 52 56 9 4 7 2 0 zip_dig ucb; 56 60 64 68 72 76 � Reference Address Value Guaranteed? uw[3] 36 + 4* 3 = 48 9 uw[6] [6] 36 + 4* 6 36 + 4* 6 = 60 60 4 4 uw[-1] 36 + 4*-1 = 32 3 cmu[15] 16 + 4*15 = 76 ?? � No bound checking � Out ‐ of ‐ range behavior implementation ‐ dependent � No guaranteed relative allocation of different arrays 23 April 2012 Data Structures 10

  6. University of Washington Referencing Examples 1 5 2 1 3 zip_dig cmu; 16 20 24 28 32 36 9 8 1 9 5 zip_dig uw ; 36 40 44 48 52 56 9 4 7 2 0 zip_dig ucb; 56 60 64 68 72 76 � Reference Address Value Guaranteed? uw[3] 36 + 4* 3 = 48 9 Yes No No uw[6] [6] 36 + 4* 6 = 60 36 + 4* 6 60 4 4 No uw[-1] 36 + 4*-1 = 32 3 No cmu[15] 16 + 4*15 = 76 ?? � No bound checking � Out ‐ of ‐ range behavior implementation ‐ dependent � No guaranteed relative allocation of different arrays 23 April 2012 Data Structures 11 University of Washington Array Loop Example int zd2int(zip_dig z) { int i; int zi = 0; for (i = 0; i < 5; i++) { for (i = 0; i < 5; i++) { zi = 10 * zi + z[i]; } return zi; } 23 April 2012 Data Structures 12

  7. University of Washington Array Loop Example int zd2int(zip_dig z) { � Original int i; int zi = 0; for (i = 0; i < 5; i++) { for (i = 0; i < 5; i++) { zi = 10 * zi + z[i]; } return zi; } � Transformed int zd2int(zip_dig z) � Eliminate loop variable i { int zi = 0; � Convert array code to � Convert array code to int *zend = z + 4; pointer code do { � Express in do ‐ while form zi = 10 * zi + *z; (no test at entrance) z++; } while (z <= zend); return zi; } 23 April 2012 Data Structures 13 University of Washington Array Loop Implementation (IA32) int zd2int(zip_dig z) { int zi = 0; int *zend = z + 4; do { do { zi = 10 * zi + *z; z++; } while(z <= zend); return zi; } # %ecx = z xorl %eax,%eax # zi = 0 leal 16(%ecx),%ebx l l 16(% ) % b # # zend d = z+4 +4 .L59: leal (%eax,%eax,4),%edx # 5*zi Translation? movl (%ecx),%eax # *z addl $4,%ecx # z++ leal (%eax,%edx,2),%eax # zi = *z + 2*(5*zi) cmpl %ebx,%ecx # z : zend jle .L59 # if <= goto loop 23 April 2012 Data Structures 14

  8. University of Washington Array Loop Implementation (IA32) int zd2int(zip_dig z) int zd2int(zip_dig z) int zd2int(zip_dig z) int zd2int(zip_dig z) int zd2int(zip_dig z) � Registers { { { { { %ecx z int zi = 0; int zi = 0; int zi = 0; int zi = 0; int zi = 0; %eax zi int *zend = z + 4; int *zend = z + 4; int *zend = z + 4; int *zend = z + 4; int *zend = z + 4; %ebx zend do { do { do { do { do { do { do { do { do { do { zi = 10 * zi + *z; zi = 10 * zi + *z; zi = 10 * zi + *z; zi = 10 * zi + *z; zi = 10 * zi + *z; � Computations z++; z++; z++; z++; z++; � 10*zi + *z implemented as } while(z <= zend); } while(z <= zend); } while(z <= zend); } while(z <= zend); } while(z <= zend); *z + 2*(zi+4*zi) return zi; return zi; return zi; return zi; return zi; � z++ increments by 4 } } } } } # %ecx = z # %ecx = z # %ecx = z # %ecx = z # %ecx = z xorl %eax,%eax xorl %eax,%eax xorl %eax,%eax xorl %eax,%eax xorl %eax,%eax # zi = 0 # zi = 0 # zi = 0 # zi = 0 # zi = 0 leal 16(%ecx),%ebx leal 16(%ecx),%ebx l l leal 16(%ecx),%ebx leal 16(%ecx),%ebx l leal 16(%ecx),%ebx l l l 16(% l 16(% l 16(% l 16(% l 16(% ) % b ) % b ) % b ) % b ) % b # zend = z+4 # # # zend = z+4 # zend = z+4 # # # # zend # zend = z+4 d d d d d = z+4 +4 +4 +4 +4 +4 .L59: .L59: .L59: .L59: .L59: leal (%eax,%eax,4),%edx # 5*zi leal (%eax,%eax,4),%edx # 5*zi leal (%eax,%eax,4),%edx # 5*zi leal (%eax,%eax,4),%edx # 5*zi leal (%eax,%eax,4),%edx # 5*zi movl (%ecx),%eax movl (%ecx),%eax movl (%ecx),%eax movl (%ecx),%eax movl (%ecx),%eax # *z # *z # *z # *z # *z addl $4,%ecx addl $4,%ecx addl $4,%ecx addl $4,%ecx addl $4,%ecx # z++ # z++ # z++ # z++ # z++ leal (%eax,%edx,2),%eax # zi = *z + 2*(5*zi) leal (%eax,%edx,2),%eax # zi = *z + 2*(5*zi) leal (%eax,%edx,2),%eax # zi = *z + 2*(5*zi) leal (%eax,%edx,2),%eax # zi = *z + 2*(5*zi) leal (%eax,%edx,2),%eax # zi = *z + 2*(5*zi) cmpl %ebx,%ecx cmpl %ebx,%ecx cmpl %ebx,%ecx cmpl %ebx,%ecx cmpl %ebx,%ecx # z : zend # z : zend # z : zend # z : zend # z : zend jle .L59 jle .L59 jle .L59 jle .L59 jle .L59 # if <= goto loop # if <= goto loop # if <= goto loop # if <= goto loop # if <= goto loop 23 April 2012 Data Structures 15 University of Washington Nested Array Example #define PCOUNT 4 zip_dig sea[PCOUNT] = {{ 9, 8, 1, 9, 5 }, { 9, 8, 1, 0, 5 }, { 9 8 1 0 5 } { 9, 8, 1, 0, 3 }, { 9, 8, 1, 1, 5 }}; 23 April 2012 Data Structures 16

  9. University of Washington Nested Array Example #define PCOUNT 4 zip_dig sea[PCOUNT] = {{ 9, 8, 1, 9, 5 }, { 9, 8, 1, 0, 5 }, { 9 8 1 0 5 } { 9, 8, 1, 0, 3 }, { 9, 8, 1, 1, 5 }}; 9 8 1 9 5 9 8 1 0 5 9 8 1 0 3 9 8 1 1 5 76 76 96 96 116 116 136 136 156 156 23 April 2012 Data Structures 17 University of Washington Nested Array Example #define PCOUNT 4 zip_dig sea[PCOUNT] = {{ 9, 8, 1, 9, 5 }, { 9 { 9, 8, 1, 0, 5 }, 8 1 0 5 } &sea[3][2]; { 9, 8, 1, 0, 3 }, { 9, 8, 1, 1, 5 }}; 9 8 1 9 5 9 8 1 0 5 9 8 1 0 3 9 8 1 1 5 76 76 96 96 116 116 136 136 156 156 � “row ‐ major” ordering of all elements � Guaranteed? 23 April 2012 Data Structures 18

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend