Heterogeneous Data Structures Alignment heterogeneous:不同种类的
1 Heterogeneous Data Structures & Alignment * heterogeneous: 不同种类的
Outline Struct Uu nion Alignment -chqp3.93.10
2 Outline • Struct • Union • Alignment – Chap 3.9, 3.10
Structures P191 Group objects into a single object Each ob ject is referenced by name 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 References to structure elements Using offsets as displacements
3 Structures P191 • Group objects into a single object • Each object is referenced by name • 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 • References to structure elements – Using offsets as displacements
Structure P192 struct rect in七11x; /*x coordinate of 1。wer-1 eft corner*/ in七1 /* y coordinate of 1。wer-1 eft corner★ int color /★ Coding ofco1ox*/ int width; /* width (in pixels)*/ int height / Height (in pixels)*/
4 Structure P192 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) */ };
Structure P192 struct rect ri r.11x=x.11y=0; r color oxFFooFF r width = 10; rheight 20;
5 Structure P192 struct rect r; r.llx = r.lly = 0; r.color = 0xFF00FF; r.width = 10; r.height = 20;
Structure P192 int area (struct rect *rp) return (*rp).width (*rp). height ⅴ。 id rotate1eft( struct rect*rp) /* Exchange width and height * int t rp-heighti rp->height rp->width rp->width s ti
6 Structure P192 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; }
Structure struct rec int ii int j; int a[3] int p i }*r; Ofset 0 nnt仁1jaoa]a2
7 Structure struct rec { int i; int j; int a[3]; int *p; } *r;
Structure P193 Copy element r->i to element r->j r is in register edx movl(edx),eax e七x->i 2 mov l eax 4(号edx) store in Ofset 0 ontents i 8a [0]a[1]a[2]
8 Structure P193 • 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
Structure P193 Generate the pointer &(r->a[1]) 工in各eax 立in各eax 11ea18(号eax,edx,4),.ecx岩ecx=正x a[了 Ofset 0 ontents i 8a [0]a[1]a[2]
9 Structure P193 • Generate the pointer &(r->a[1]) – r in %eax, – i in %edx 1 leal 8(%eax,%edx,4),%ecx %ecx = &r- >a[i]
Structure x->P=&r->a[r->i+x->j]; r in register号edx: 1m。v1 4(edx),各eax Get r->j 2 addl (岩edx),号eax Add r->i 31ea1 8(各edx,eax,4),号eax Compute cx->a[x-xi+x->元7 4 movl gear I 20(号edx) store in Ofset 0 ontents i 8a [0]a[1]a[2]
10 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