Presentation is loading. Please wait.

Presentation is loading. Please wait.

Machine Level Representation of Programs (III)

Similar presentations


Presentation on theme: "Machine Level Representation of Programs (III)"— Presentation transcript:

1 Machine Level Representation of Programs (III)

2 Aggregate scalar data into larger data Pointers vs. Array
Outline Aggregate scalar data into larger data Pointers vs. Array Array subscript and pointer arithmetic Suggested reading 3.8 2 2

3 Allocate a contiguous region in memory
Array declaration T A[N] ; Allocate a contiguous region in memory The size of the region is sizeof(T) * N bytes 3 3

4 Starting Address of an Array
The starting address of an array A is denoted as XA Identifier A can be used as A pointer to the beginning of the array Its value is XA 4 4

5 Array elements can be accessed
Accessing Array Array elements can be accessed Using an integer index ranging between 0 and N-1 Array element i is stored at address XA + sizeof(T) * i 5 5

6 Array char a[12] ; xa xa+4 xa+8 char *b[5] ; xb xb+4 xb+8 xb+12 xb+16
1 2 3 4 5 6 7 8 9 10 11 xa xa+4 xa+8 char *b[5] ; 4 8 12 16 xb xb+4 xb+8 xb+12 xb+16 6 6

7 Array double c[2] ; xc xc+8 double *d[5] ; xd xd+4 xd+8 xd+12 xd+16 4
4 8 12 16 xd xd+4 xd+8 xd+12 xd+16 7 7

8 Addition and subtraction
Pointer Arithmetic Addition and subtraction p+i , p-i (result is a pointer) p-q (result is an int) Referencing & dereferencing *p, &E Subscription A[i], *(A+i) 8 8

9 Memory referencing instruction
E is an array of int’s Address of E is stored in register %edx Index i is stored in register %ecx The array element E[i] is translated into movl (%edx, %ecx, 4), %eax

10 Pointer Arithmetic Expression Type Value Assembly code E int * xE
movl %edx, %eax E[0] int M[xE] movl (%edx), %eax E[i] M[xE+4i] movl (%edx, %ecx, 4), %eax &E[2] xE+8 leal (%edx,) %eax E+i-1 xE+4i-4 lea (%edx, %ecx, 4), %eax *(&E[i]+i) M[xE+4i+4i] movl (%edx, %ecx, 8), %eax &E[i]-E i movl %ecx, %eax

11 Example 1 int decimal5(int *x) 2 { 3 int i; 4 int val = 0; 5
6 for (i = 0; i < 5; i++) 7 val = (10 * val) + x[i]; 8 9 return val; 10 }

12 Example xorl %edx, %edx # i = 0 xorl %eax, %eax # val = 0 movl 8(%ebp), %ecx # get x L2: leal (%eax,%eax,4), %eax addl %eax, %eax # val *= 10 addl (%ecx,%edx,4), %eax # val += x[i] addl $1, %edx # i++ cmpl $5, %edx # i ? 5 jne L2

13 Nested Array int A[4][3] ; Array A is a two-dimensional array
with four rows and three columns It is referenced as A[0][0] through A[3][2]

14 Row major ordered in memory
Nested Array int A[4][3] ; Array of array typedef int row3_t[3] ; row3_t A[4] ; Array A contains 4 elements, each requiring 12 bytes to store 3 integers The whole size of array A is 48 bytes Row major ordered in memory

15 Nested Array Row Element Address A[0] A[0][0] xA A[0][1] xA+4 A[0][2]

16 D[i][j] is at memory address
Nested Array T D[R][C] ; D[i][j] is at memory address xD + L* ( C * i + j ) L is sizeof(T)

17 Access A[i,j] It is in memory M [xA +j*4+ i * 12 ] %eax contains xA %edx holds i, %ecx holds j sall $2, %ecx j*4 leal (%edx, %edx, 2), %edx i*3 leal (%ecx, %edx, 4), %ecx j*4+ i*12 movl (%eax, %ecx), %eax

18 Fixed-size Arrays 1 #define N 16 2 typedef int fix_matrix[N][N]; 3
4 /* Compute i,k of fixed matrix product */ 5 int fix_prod_ele (fix_matrix A, fix_matrix B, int i, int k) 6 { 7 int j; 8 int result = 0; 9 10 for (j = 0; j < N; j++) 11 result += A[i][j] * B[j][k]; 12 13 return result; 14 }

19 The loop will access the elements of array A
Observations The loop will access the elements of array A as A[i][0], A[i][1], …, A[i][15] in sequence These elements occupy adjacent positions in memory starting with the address of array A[i][0] Use a pointer variable Aptr to access these successive locations.

20 The loop will access the elements of array B
Observations The loop will access the elements of array B as B[0][k], B[1][k], …, B[15][k] in sequence These elements occupy positions in memory starting with the address of array B[0][i] and space 64 bytes apart. Use a pointer variable Bptr to access these locations. Use a simple counter to keep track of the number of iterations required

21 Fixed-size Arrays 1 /* Compute i,k of fixed matrix product */
2 int fix_prod_ele_opt (fix_matrix A, fix_matrix B, int i, int k) 3 { 4 int *Arow=&A[i][0], *Bptr = &B[0][k]; 5 int cnt, result = 0; 6 7 For ( cnt = 0; cnt < 16; cnt++) { 8 result += Arow[cnt] * (*Bptr); 9 Bptr += N; 10 } 11 12 return result; 13 }

22 Fixed-size Arrays Aptr is in %edx, Bptr in %ecx, result in %esi, cnt in %ebx 1 .L6: loop: movl (%ecx),%eax Get *Bptr imull (%edx, %ebx, 4),%eax Multiply by Arow[j] addl %eax,%esi Add to result 5 addl $1, %ebx cnt++ addl $64,%ecx Add 64 to Bptr cmpl $16,%ebx compare cnt : 16 jne .L6 if !=, goto loop

23 Declare an array int A[exp1][exp2]
Variable-Size Arrays 1 int var_ele (int n, int A[n][n], int i, int j) 2 { 3 return A[i][j]; 4 } Declare an array int A[exp1][exp2] either as a local variable or as an argument to a function The dimensions of the array are determined by evaluating the expressions at the time the declaration is encounterd

24 Assembly Code n at %ebp+8, A at %ebp+12, i at %ebp+16, j at %ebp+20
movl 8(%ebp), %eax Get n sall $2, %eax Compute 4*n movl %eax, %edx Copy 4*n imull 16(%ebp), %edx Compute 4*n*i movl 20(%ebp), %eax Get j sall $2, %eax Compute 4*j addl 12(%ebp), %eax Compute xA+ 4 ∗ j movl (%eax,%edx), %eax Read from xA+ 4 ∗ (n ∗ i + j)

25 Optimization 1 /* Compute i,k of variable matrix product */
2 int var_prod_ele (int n, int A[n][n], int B[n][n], int i, int k) { 3 int j; 4 int result = 0; 5 6 for (j = 0; j < n; j++) 7 result += A[i][j] * B[j][k]; 8 9 return result; 10 }

26 Assembly Code n stored at %ebp+8
Registers: Arow in %esi, Bptr in %ecx, j in %edx, result in %ebx, %edi holds 4*n 1 .L30: loop: 2 movl (%ecx), %eax Get *Bptr 3 imull (%esi,%edx,4), %eax Multiply by Arow[j] addl %eax, %ebx Add to result addl $1, %edx Increment j addl %edi, %ecx Add 4*n to Bptr cmpl %edx, 8(%ebp) Compare n:j jg .L If >, goto loop 8(%ebp) Register spill

27 Heterogeneous Data Structures & Alignment

28 Outline Struct Union Alignment

29 Group objects into a single object
Structures Group objects into a single object struct rect { int llx; /* X coordinate of lower-left corner */ int lly; /* Y coordinate of lower-left corner */ int color; /* Coding of color */ int width; /* Width (in pixels) */ int height; /* Height (in pixels) */ };

30 Structure Each object is referenced by name struct rect r; r.llx = r.lly = 0; r.color = 0xFF00FF; r.width = 10; r.height = 20;

31 Structure int area (struct rect *rp) {
return (*rp).width * (*rp).height; } void rotate_left (struct rect *rp) /* Exchange width and height */ int t = rp->height; rp->height = rp->width; rp->width = t;

32 Structures Memory layout
All the components are stored in a contiguous region of memory A pointer to a structure is the address of its first byte

33 Structure struct rec { int i; int j; int a[3]; int *p; } *r; Offset 4
4 8 20 Contents i j a[0] a[1] a[2] p

34 References to structure elements Using offsets as displacements
r->j = r->i (Copy element r->i to element r->j) r is in register %edx. 1 movl (%edx), %eax Get r->i 2 movl %eax, 4(%edx) Store in r->j Offset 4 8 20 Contents i j a[0] a[1] a[2] p

35 Structure &(r->a[i]) r in %eax, i in %edx: Offset 4 8 20 Contents i
1 leal 8(%eax,%edx,4),%ecx Generate &r->a[i] Offset 4 8 20 Contents i j a[0] a[1] a[2] p

36 r->p = &r->a[r->i + r->j];
Structure r->p = &r->a[r->i + r->j]; r in register %edx: 1 movl 4(%edx), %eax Get r->j 2 addl (%edx), %eax Add r->i 3 leal 8(%edx,%eax,4),%eax Compute &r->a[r->i + r->j] 4 movl %eax, 20(%edx) Store in r->p Offset 4 8 20 Contents i j a[0] a[1] a[2] p

37 Unions A single object can be referenced by using different data types The syntax of a union declaration is identical to that for structures, but its semantics are very different Rather than having the different fields reference different blocks of memory, they all reference the same block

38 Unions struct S3 { char c; int i[2]; double v; }; union U3 {
The offsets of the fields, as well as the total size of data types S3 and U3, are: Type c i v size S3 4 12 20 U3 8

39 Unions struct NODE { struct NODE *left; struct NODE *right;
double data; }; union NODE { struct { union NODE *left; union NODE *right; } internal;

40 Unions struct NODE { int is_leaf; union { struct { struct NODE *left;
struct NODE *right; } internal; double data; } info; };

41 Unions 1 unsigned float2bit(float f) 2 { 3 union { 4 float f;
5 unsigned u; 6 } temp; 7 temp.f = f; 8 return temp.u; 9 } 1 movl 8(%ebp), %eax

42 Unions 1 unsigned copy (unsigned u) 2 { 3 return u; 4 }
1 movl 8(%ebp), %eax

43 Alignment restrictions
The address for some type of object must be a multiple of some value k (typically 2, 4, or 8) Simplify the hardware design of the interface between the processor and the memory system

44 Alignment In IA32 hardware will work correctly regardless of the alignment of data Aligned data can improve memory system performance

45 Linux alignment restriction
1-byte data types are able to have any address 2-byte data types must have an address that is multiple of 2 Any larger data types must have an address that is multiple of 4

46 Alignment is enforced by
Making sure that every data type is organized and allocated in such a way that every object within the type satisfies its alignment restrictions. malloc() Returns a generic pointer that is void * Its alignment requirement is 4

47 Overall structure placement
Alignment Structure data type may need to insert gaps in the field allocation may need to add padding to the end of the structure Overall structure placement Each structure has alignment requirement K K = Largest alignment of any element Ini0al address & structure length must be mul0ples of K

48 Simple Example struct xxx { int i; char c; double d; };
struct xxx x[2]; 0x00 0x04 0x08 0x0C 0x10 0x14 &x[0].i &x[0].c &x[0].d &x[1].i

49 Complex Example 0x00 0x04 0x08 0x0C 0x10 0x14 0x18 0x1C &x[0].s struct xxx { short s; char c0; int i; long l; char c1; char a[2]; double d; char c2; }; struct xxx x[2]; &x[0].c0 &x[0].i &x[0].l &x[0].c1 &x[0].a[0] &x[0].a[1] &x[0].d &x[0].c2 &x[1].s

50 Array struct ccc { char c1; char a[3]; char c2; }; struct ccc c[2];
0x00 &c[0].c1 &c[0].a[0] 0x04 &c[0].c2 &c[1].c1 &c[1].a[0] 0x08 &c[1].c2 0x0C 0x10 0x14

51 Array struct ccc { char c1; short a[3]; char c2; }; struct sss s[2];
0x00 &s[0].c1 &s[0].a[0] 0x04 0x08 &s[0].c2 &s[1].c1 0x0C &s[1].a[0] 0x10 &s[1].c2 0x14

52 Array struct iii { char c1; int a[3]; char c2; }; struct iii i[2];
0x00 0x04 0x08 0x0C 0x10 0x14 &s[0].c1 &x[0].i &s[0].c2 &s[1].c1


Download ppt "Machine Level Representation of Programs (III)"

Similar presentations


Ads by Google