21
University of Washington Data Structures II: Structs and Unions The Hardware/Software Interface CSE351 Winter 2013

The Hardware/Software Interface CSE351 Winter 2013

  • Upload
    bena

  • View
    22

  • Download
    0

Embed Size (px)

DESCRIPTION

The Hardware/Software Interface CSE351 Winter 2013. Data Structures II: Structs and Unions. Data Structures in Assembly. Arrays One-dimensional Multi-dimensional (nested) Multi-level Structs Alignment Unions. Structures. struct rec { int i; int a[3]; int *p; };. Structures. - PowerPoint PPT Presentation

Citation preview

Page 1: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

Data Structures II: Structs and Unions

The Hardware/Software InterfaceCSE351 Winter 2013

Page 2: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

2Data Structures II

Data Structures in Assembly Arrays

One-dimensional Multi-dimensional (nested) Multi-level

Structs Alignment

Unions

Winter 2013

Page 3: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

3Data Structures II

struct rec { int i; int a[3]; int *p;};

Structures

Winter 2013

Page 4: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

4Data Structures II

struct rec { int i; int a[3]; int *p;};

Structures

Characteristics Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types

Winter 2013

Memory Layouti a p

0 4 16 20

Page 5: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

5Data Structures II

struct rec { int i; int a[3]; int *p;};

IA32 Assembly# %eax = val# %edx = rmovl %eax,(%edx) # Mem[r] = val

voidset_i(struct rec *r, int val){ r->i = val; }

Structures

Winter 2013

Accessing Structure Member Given an instance of the struct, we can use

the . operator, just like Java: struct rec r1; r1.i = val;

What if we have a pointer to a struct: struct rec *r = &r1; Using * and . operators: Or, use -> operator for short:

Pointer indicates first byte of structure; access members with offsets

(*r).i = val; r->i = val;

Page 6: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

6Data Structures II

# %ecx = idx# %edx = rleal 0(,%ecx,4),%eax # 4*idxleal 4(%eax,%edx),%eax # r+4*idx+4

int *find_a (struct rec *r, int idx){ return &r->a[idx];}

Generating Pointer to Structure Member

Generating Pointer to Array Element Offset of each structure

member determined at compile time

Winter 2013

struct rec { int i; int a[3]; int *p;};

i a p

0 4 16 20

r+4+4*idxr

Page 7: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

7Data Structures II

Structures & Alignment Unaligned Data

Aligned Data Primitive data type requires K bytes Address must be multiple of K

c i[0] i[1] v3 bytes 4 bytes

p+0 p+4 p+8 p+16 p+24

Multiple of 4 Multiple of 8

Multiple of 8

Multiple of 8

c i[0] i[1] v

p p+1 p+5 p+9 p+17

struct S1 { char c; int i[2]; double v;} *p;

struct S1 { char c; int i[2]; double v;} *p;

Winter 2013

Page 8: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

8Data Structures II

Alignment Principles Aligned Data

Primitive data type requires K bytes Address must be multiple of K

Aligned data is required on some machines; it is advisedon IA32 Treated differently by IA32 Linux, x86-64 Linux, and Windows!

What is the motivation for alignment?

Winter 2013

Page 9: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

9Data Structures II

Alignment Principles Aligned Data

Primitive data type requires K bytes Address must be multiple of K

Aligned data is required on some machines; it is advisedon IA32 Treated differently by IA32 Linux, x86-64 Linux, and Windows!

Motivation for Aligning Data Physical memory is accessed by aligned chunks of 4 or 8 bytes (system-

dependent) Inefficient to load or store datum that spans quad word boundaries

Also, virtual memory is very tricky when datum spans two pages (later…) Compiler

Inserts padding in structure to ensure correct alignment of fields sizeof() should be used to get true size of structs

Winter 2013

Page 10: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

10Data Structures II

Specific Cases of Alignment (IA32) 1 byte: char, …

no restrictions on address 2 bytes: short, …

lowest 1 bit of address must be 02

4 bytes: int, float, char *, … lowest 2 bits of address must be 002

8 bytes: double, … Windows (and most other OSs & instruction sets): lowest 3 bits 0002

Linux: lowest 2 bits of address must be 002

i.e., treated the same as a 4-byte primitive data type 12 bytes: long double

Windows, Linux: lowest 2 bits of address must be 002

Winter 2013

Page 11: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

11Data Structures II

struct S1 { char c; int i[2]; double v;} *p1;

Satisfying Alignment with Structures Within structure:

Must satisfy every member’s alignment requirement Overall structure placement

Each structure has alignment requirement K K = Largest alignment of any element

Initial address & structure length must be multiples of K Example (under Windows or x86-64): K = ?

K = 8, due to double member

Winter 2013

c i[0] i[1] v3 bytes 4 bytes

p1+0 p1+4 p1+8 p1+16 p1+24

Multiple of 4 Multiple of 8

Multiple of 8 Multiple of 8

Page 12: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

12Data Structures II

Different Alignment Conventions IA32 Windows or x86-64:

K = 8, due to double member

IA32 Linux: K = ? K = 4; double aligned like a 4-byte data type

Winter 2013

c i[0] i[1] v3 bytes 4 bytes

p1+0 p1+4 p1+8 p1+16 p1+24

c i[0] i[1] v3 bytes

p1+0 p1+4 p1+8 p1+12 p1+20

struct S1 { char c; int i[2]; double v;} *p1;

Page 13: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

13Data Structures II

Saving Space Put large data types first:

Effect (example x86-64, both have K=8)

Winter 2013

c i[0] i[1] v3 bytes 4 bytes

p1+0 p1+4 p1+8 p1+16 p1+24

struct S1 { char c; int i[2]; double v;} *p1;

struct S2 { double v; int i[2]; char c;} *p2;

ci[0] i[1]v

p2+0 p2+8 p2+16

Unfortunately, doesn’t satisfy requirement that struct’s total size is a multiple of K

Page 14: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

14Data Structures II

Arrays of Structures Satisfy alignment requirement

for every element

Winter 2013

struct S2 { double v; int i[2]; char c;} a[10];

ci[0] i[1]v

a+24 a+32 a+40

a[0]

a+0

a[1] a[2]

a+24 a+48 a+72

• • •

7 bytes

a+48

Page 15: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

15Data Structures II

Saving Space Put large data types first:

Effect (K=4)

This strategy can save some space for certain structs.

c i3 bytes d 3 bytes

ci d 2 bytes

struct S3 { char c; int i; char d;} *p3;

struct S4 { int i; char c; char d;} *p4;

Winter 2013

Page 16: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

16Data Structures II

Unions Allocated according to largest element Can only use one member at a time

union U1 { char c; int i[2]; double v;} *up;

union U1 { char c; int i[2]; double v;} *up;

struct S1 { char c; int i[2]; double v;} *sp;

struct S1 { char c; int i[2]; double v;} *sp;

c 3 bytes i[0] i[1] 4 bytes v

sp+0 sp+4 sp+8 sp+16 sp+24

c

i[0] i[1]

v

up+0 up+4 up+8

Winter 2013

Page 17: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

17Data Structures II

What Are Unions Good For? Unions allow the same region of memory to be referenced as

different types Different “views” of the same memory location Can be used to circumvent C’s type system (bad idea)

Better idea: use a struct inside a union to access some memory location either as a whole or by its parts

Winter 2013

Page 18: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

18Data Structures II

Unions For Embedded Programming

Winter 2013

typedef union{ unsigned char byte; struct { unsigned char b0:1; unsigned char b1:1; unsigned char b2:1; unsigned char b3:1; unsigned char reserved:4; } bits;} hw_register;

hw_register reg;reg.byte = 0x3F; // 001111112

reg.bits.b2 = 0; // 001110112

reg.bits.b3 = 0; // 001100112

unsigned short a = reg.byte;printf("0x%X\n", a); // output: 0x33

(Note: the placement of these fields and other parts of this example are implementation-dependent)

Page 19: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

19Data Structures II

Summary Arrays in C

Contiguous allocations of memory No bounds checking Can usually be treated like a pointer to first element

Structures Allocate bytes in order declared Pad in middle and at end to satisfy alignment

Unions Provide different views of the same memory location

Winter 2013

Page 20: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

20Data Structures IIWinter 2013

Page 21: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

21Data Structures II

Accessing Array Elements Compute array offset 12i (sizeof(S3)) Element j is at offset 8 within structure Assembler gives offset a+8

Resolved during linking

Winter 2013

// Global:struct S3 { short i; float v; short j;} a[10];

i jv

a+12i a+12i+8

2 bytes 2 bytes

a[0]a+0

a[i]a+12i

• • • • • •

short get_j(int idx){ return a[idx].j;// return (a + idx)->j;}

# %eax = idxleal (%eax,%eax,2),%eax # 3*idxmovswl a+8(,%eax,4),%eax