Basic Data Types 15 213 The course that gives CMU its Zip Stored operated on in general registers Signed vs unsigned depends on instructions used Intel GAS Bytes C byte b 1 unsigned char word w 2 unsigned short double word l 4 unsigned int char quad word 8 Structured Data II Heterogenous Data Sept 26 2000 Topics Integral Floating Point Structure Allocation Alignment Unions Byte Ordering Byte Operations IA32 Linux Memory Organization Understanding C declarations Stored operated on in floating point registers Intel GAS Bytes C Single s 4 float Double l 8 double Extended t 10 12 long double class09 ppt class09 ppt 2 CS 213 F 00 Generating Pointer to Structure Member Structures r Concept struct rec int i int a 3 int p Contiguously allocated region of memory Refer to members within structure by names Members may be of different types struct rec int i int a 3 int p Memory Layout i 0 a 4 p 16 20 Accessing Structure Member void set i struct rec r int val r i val class09 ppt i 0 a 4 p 16 r 4 4 idx Generating Pointer to Array Element int find a struct rec r int idx return r a idx Offset of each structure member determined at compile time Assembly eax val edx r movl eax edx 3 ecx idx edx r leal 0 ecx 4 eax 4 idx leal 4 eax edx eax r 4 idx 4 Mem r val CS 213 F 00 class09 ppt 4 CS 213 F 00 Structure Referencing Cont C Code Aligned Data i struct rec int i int a 3 int p void set p struct rec r r p r a r i class09 ppt 0 a 4 i 0 p Primitive data type requires K bytes Address must be multiple of K Required on some machines advised on IA32 treated differently by Linux and Windows 16 a 4 Motivation for Aligning Data 16 Memory accessed by aligned double or quad words Inefficient to load or store datum that spans quad word boundaries Virtual memory very tricky when datum spans 2 pages Element i edx r movl edx ecx leal 0 ecx 4 eax leal 4 edx eax eax movl eax 16 edx 5 Compiler r i 4 r i r 4 4 r i Update r p CS 213 F 00 Specific Cases of Alignment Size of Primitive Data Type 7 Inserts gaps in structure to ensure correct alignment of fields class09 ppt 6 CS 213 F 00 Satisfying Alignment with Structures Offsets Within Structure 1 byte e g char no restrictions on address 2 bytes e g short lowest 1 bit of address must be 02 4 bytes e g int float char etc lowest 2 bits of address must be 002 8 bytes e g double Windows and most other OS s instruction sets lowest 3 bits of address must be 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 Linux lowest 2 bits of address must be 002 i e treated the same as a 4 byte primitive data type class09 ppt Alignment CS 213 F 00 Must satisfy element s alignment requirement Overall Structure Placement Each structure has alignment requirement K Largest alignment of any element struct S1 Initial address structure length must be char c multiples of K int i 2 double v Example under Windows p K 8 due to double element c p 0 i 0 p 4 i 1 p 8 Multiple of 4 p 24 Multiple of 8 Multiple of 8 class09 ppt v p 16 Multiple of 8 8 CS 213 F 00 Linux vs Windows struct S1 char c int i 2 double v p Windows including Cygwin K 8 due to double element c p 0 i 0 p 4 Effect of Overall Alignment Requirement i 1 struct S2 double x int i 2 char c p v p 8 p 16 Multiple of 4 Multiple of 8 p 8 p 8 Multiple of 4 Multiple of 4 i 1 p 12 struct S3 float x 2 int i 2 char c p v p 12 x 0 Multiple of 4 9 Windows p 24 Linux p 20 p 0 CS 213 F 00 i 0 p 8 p 4 class09 ppt i 1 p 12 c p 16 p 20 10 CS 213 F 00 Arrays of Structures Principle struct S4 char c1 double v char c2 int i p Allocated by repeating allocation for array type In general may nest arrays structures to arbitrary depth 10 bytes wasted space in Windows c1 v c2 p 8 p 16 struct S5 double v char c1 char c2 int i p i p 20 struct S6 short i float v short j a 10 p 24 a 1 i a 1 v a 12 a 16 a 1 j a 20 a 24 2 bytes wasted space a 0 class09 ppt p 16 p must be multiple of 4 in either OS x 1 Ordering Elements Within Structure p 0 c p 20 Multiple of 4 class09 ppt v i 1 Multiple of 8 i 0 p 0 i 0 p 0 Multiple of 8 K 4 double treated like a 4 byte data type p 4 x p 24 Linux c p 0 p must be multiple of 8 for Windows 4 for Linux c1 c2 p 8 i p 12 11 a 0 a 1 a 12 a 2 a 24 a 36 p 16 CS 213 F 00 class09 ppt 12 CS 213 F 00 Accessing Element within Array Achieving Alignment Compute offset to start of structure Compute 12 i as 4 i 2i Access element according to its offset within structure Offset by 8 Assembler gives displacement as a 8 Linker must set actual value short get j int idx return a idx j Satisfying Alignment within Structure struct S6 short i float v short j a 10 eax idx leal eax eax 2 eax 3 idx movswl a 8 eax 4 eax Starting address of structure array must be multiple of worst case alignment for any element a must be multiple of 4 Offset of element within structure must be multiple of element s alignment requirement v s offset of 4 is a multiple of 4 Overall size of structure must be multiple of worstcase alignment for any element Structure padded with unused space to be 12 bytes a 0 a i a i a 1 i a 12i a 0 a 12i a i i a i v a 12i a 0 a 0 struct S6 short i float v short j a 10 a 1 v a 1 j a 12i 4 a i j Multiple of 4 a 12i a 12i 8 class09 ppt 13 class09 ppt CS 213 F 00 Principles Structure can hold 3 kinds of data Only one form at any given time Identify particular kind with flag type Overlay union elements Allocate according to largest element Can only use one field at a time …
View Full Document