Structured Data II Heterogenous Data Sept. 22, 1998 Topics Structure Allocation Alignment Operating on Byte Strings Unions Byte Ordering Alpha Memory Organization.

Slides:



Advertisements
Similar presentations
INSTRUCTION SET ARCHITECTURES
Advertisements

Dynamic Memory Allocation I Topics Basic representation and alignment (mainly for static memory allocation, main concepts carry over to dynamic memory.
MIPS Assembly Language Programming
Machine-Level Programming IV: Structured Data Feb 5, 2004 Topics Arrays Structs Unions class08.ppt “The course that gives CMU its Zip!”
University of Washington Data Structures! Arrays  One-dimensional  Multi-dimensional (nested)  Multi-level Structs  Alignment Unions 1.
Structured Data: Sept. 20, 2001 Topics Arrays Structs Unions class08.ppt “The course that gives CMU its Zip!”
Machine-Level Programming IV: Data Sept. 19, 2007 Structured Data Arrays Structs UnionsData/Control Buffer overflow class07.ppt F’07.
Machine-Level Programming IV: Structured Data Topics Arrays Structures Unions CS213.
– 1 – EECS213, S’08 Alignment Aligned Data Primitive data type requires K bytes Address must be multiple of K Required on some machines; advised on IA32.
Structured Data I: Homogenous Data Sept. 17, 1998 Topics Arrays –Single –Nested Pointers –Multilevel Arrays Optimized Array Code class08.ppt “The.
What is an instruction set?
Data Representation and Alignment Topics Simple static allocation and alignment of basic types and data structures Policies Mechanisms.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Dr Mohamed Menacer College of Computer Science and Engineering Taibah University CS-334: Computer.
Variables and Objects, pointers and addresses: Chapter 3, Slide 1 variables and data objects are data containers with names the value of the variable is.
Machine-Level Programming 6 Structured Data Topics Structs Unions.
Ithaca College 1 Machine-Level Programming VIII: Advanced Topics: alignment & Unions Comp 21000: Introduction to Computer Systems & Assembly Lang Systems.
Machine-Level Programming IV: Structured Data Topics Arrays Structs Unions CS 105 “Tour of the Black Holes of Computing”
Bits, Bytes, and Integers Topics Representing information as bits Bit-level manipulations Boolean algebra Expressing in C Representations of Integers Basic.
Computer Architecture and Organization
Computer Architecture EKT 422
Info stored in computer (memory) Numbers All in binaray – can be converted to octal, hex Characters ASCII – 1-byte/char Unicode – 2-byte/char Unicode-table.com/en.
Topics memory alignment and structures typedef for struct names bitwise & for viewing bits malloc and free (dynamic storage in C) new and delete (dynamic.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming V: Unions and Memory layout.
University of Amsterdam Computer Systems – Data in C Arnoud Visser 1 Computer Systems New to C?
Machine-level Programming IV: Data Structures Topics –Arrays –Structs –Unions.
University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming IV: Data Topics: Arrays.
Fabián E. Bustamante, Spring 2007 Machine-Level Prog. IV - Structured Data Today Arrays Structures Unions Next time Arrays.
Machine-Level Programming IV: Structured Data
Machine-Level Programming IV: Structured Data Topics Arrays Structs Unions CS 105 “Tour of the Black Holes of Computing”
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 7, 8 Instruction Set Architecture.
EECS 370 Discussion 1 xkcd.com. EECS 370 Discussion Topics Today: – ARM Addressing Endianness, Loading, and Storing Data – Data Layout Struct Packing.
Carnegie Mellon /18-243: Introduction to Computer Systems Instructors: Bill Nace and Gregory Kesden (c) All Rights Reserved. All work.
Machine-Level Programming II Control Flow Sept. 10, 1998 Topics Control Flow –Varieties of Loops –Switch Statements class06.ppt “The course that.
Instructor: Fatma CORUT ERGİN
Big-Endians Little-Endians and Bi-Endians
Machine-Level Programming V: Miscellaneous Topics
Machine-Level Programming IV: Data
Machine-Level Programming IV: Data
Machine-Level Programming V: Unions and Memory layout
Machine-Level Programming V: Miscellaneous Topics
Machine-Level Programming IV: Structured Data
Machine-Level Programming IV: Structured Data Sept. 30, 2008
Heterogeneous Data Structures & Alignment
Machine-level Programming IV: Data Structures
Machine-Level Programming IV: Data September 8, 2008
Machine-Level Programming 6 Structured Data
Machine-Level Programming IV: Structured Data Sept. 19, 2002
Machine-Level Programming IV: Data
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Bits and Bytes Topics Representing information as bits
Bits and Bytes Topics Representing information as bits
Machine-Level Programming IV: Structured Data
Structured Data II Heterogenous Data Feb. 15, 2000
Machine-Level Programming IV: Data
Bits and Bytes Topics Representing information as bits
Instructors: Majd Sakr and Khaled Harras
Bits and Bytes Topics Representing information as bits
Machine-Level Programming IV: Machine Data 9th Lecture
Chapter 9 Instruction Sets: Characteristics and Functions
Comp Org & Assembly Lang
Machine-Level Programming IV: Structured Data
Instructor: Fatma CORUT ERGİN
Machine-Level Programming IV: Structured Data
Chapter 10 Instruction Sets: Characteristics and Functions
Machine-Level Programming IV: Structured Data
Machine-Level Programming IV: Structured Data
Presentation transcript:

Structured Data II Heterogenous Data Sept. 22, 1998 Topics Structure Allocation Alignment Operating on Byte Strings Unions Byte Ordering Alpha Memory Organization class09.ppt “The course that gives CMU its Zip!”

CS 213 F’98– 2 – class09.ppt Basic Data Types Integral Stored & operated on in general registers Signed vs. unsigned depends on instructions used AlphaBytesC byte1[ unsigned ] char word2[ unsigned ] short long word4[ unsigned ] int quad word8[ unsigned ] long int, pointers Floating Point Stored & operated on in floating point registers Special instructions for four different formats (only 2 we care about) AlphaBytesC S_floating4 float T_floating8 double

CS 213 F’98– 3 – class09.ppt struct rec { long int i; long int a[3]; long int *p; }; Annotated Assembly set_i: stq $17,0($16)# r->i = val ret $31,($26),1 void set_i(struct rec *r, long int val) { r->i = val; } iap 0832 Structures Concept Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Accessing Structure Member Memory Layout

CS 213 F’98– 4 – class09.ppt struct rec { long int i; long int a[3]; long int *p; }; find_a: s8addq $17,8,$0# $0 = 8*idx +8 addq $16,$0,$0# $0 += r ret $31,($26),1 long int * find_a (struct rec *r, long 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 iap 0832

CS 213 F’98– 5 – class09.ppt struct rec { long int i; long int a[3]; long int *p; }; set_p: ldq $1,0($16)# get r->i s8addq $1,8,$1# Compute 8*i+8 addq $1,$16,$1 # compute &a[i] stq $1,32($16)# store in p ret $31,($26), 1 void set_p(struct rec *r, long int *ptr) { r->p = &r->a[r->i]; } Structure Referencing (Cont.) C Code ia 0832 Element i iap 0832

CS 213 F’98– 6 – class09.ppt Alignment Requirement Primitive data type requires K bytes Address must be multiple of K Specific Cases Long word address must be multiple of 4 –Lower 2 bits of address must be 00 2 Quad word address must be multiple of 8 –Lower 3 bits of address must be Reason Memory accessed by (aligned) quadwords –Inefficient to load or store datum that spans quad word boundaries –Virtual memory very tricky when datum spans 2 pages Compiler Inserts gaps in structure to ensure correct alignment of fields

CS 213 F’98– 7 – class09.ppt struct S1 { char c; int i[2]; long int v; } *p; Satisfying Alignment with Structures Offsets Within Structure Must satisfy element’s alignment requirement Overall Structure Placement Each structure has alignment requirement K –Largest alignment of any element Initial address + structure length must be multiples of K Example K = 8, due to long int element ci[0]i[1]v p+0p+4p+8p+16p+24 Multiple of 4Multiple of 8

CS 213 F’98– 8 – class09.ppt Effect of Overall Alignment Requirement ci[0]i[1]x p+0p+12p+8p+16p+24 struct S2 { int *x; int i[2]; char c; } *p; struct S3 { int x[2]; int i[2]; char c; } *p; ci[0]i[1] p+0p+12p+8p+16p+20 x[0]x[1] p+4 p must be multiple of 8 p must be multiple of 4

CS 213 F’98– 9 – class09.ppt Ordering Elements Within Structure struct S4 { char c1; long int v; char c2; int i; } *p; struct S5 { long int v; char c1; char c2; int i; } *p; c1iv p+0p+20p+8p+16p+24 c2c1iv p+0p+12p+8p+16 c2 10 bytes wasted space 2 bytes wasted space

CS 213 F’98– 10 – class09.ppt Arrays of Structures Principle Allocated by repeating allocation for array type In general, may nest arrays & structures to arbitrary depth a[0] a+0 a[1]a[2] a+24a+48a+72 a+24a+40a+32a+48 struct S6 { int i; long int v; int j; } a[10]; a[1].ia[1].ja[1].v

CS 213 F’98– 11 – class09.ppt Accessing Element within Array Compute offset to start of structure –Compute 24*i as (4i–i)*8 Access element according to its offset within structure –Offset by 8 a[0] a+0 a[i] a+24i long int get_v(int i) { return a[i].v; } s4subq $16,$16,$16# i*= 3 lda $1,a# $1 = a s8addq $16,$1,$16# $16 = a+i ldq $0,8($16)# a[i].v a+24ia+24i+8 struct S6 { int i; long int v; int j; } a[10]; a[i].ia[i].ja[i].v

CS 213 F’98– 12 – class09.ppt Satisfying Alignment within Structure Achieving Alignment Starting address of structure array must be multiple of worst-case alignment for any element – a must be multiple of 8 Offset of element within structure must be multiple of element’s alignment requirement – v ’s offset of 8 is a multiple of 8 Overall size of structure must be multiple of worst- case alignment for any element –Structure padded with unused space to be 24 bytes struct S6 { int i; long int v; int j; } a[10]; a[0] a+0 a[i] a+24i a+24ia+24i+8 a[1].ia[1].ja[1].v Multiple of 8

CS 213 F’98– 13 – class09.ppt Byte-Level Memory Operation Example ABCDEF 0x1000x1070x103 FFEEDDCCBBAA9988 0x2080x20f0x20d char *src, *dest; src = 0x103; dest = 0x20d; *dest = *src; Increasing address FFEE89CCBBAA9988 0x2080x20f0x20d Desired Effect:

CS 213 F’98– 14 – class09.ppt Implementing Byte-Level Operations Issue Byte addresses have no alignment requirement Alpha memory system only designed for 4-byte and 8-byte aligned memory operations Method Could use reqular quad-word memory operations + masking and shifting –17 instructions required to implement statement *dest = *src –single byte transfer Instead, provide set of instructions to perform byte extraction and insertion in quad-words –7 instructions required to implement statement *dest = *src

CS 213 F’98– 15 – class09.ppt Extracting Source Byte Step 1. Get byte at src = 0x103 Pointer src in register $17 ldq_u $1, 0($17) –Rounds effective address 0x103 to nearest quad word boundary ( 0x100 ) »By setting low 3 bits of address to –Loads entire quad word into memory extbl $1, $17, $1 –Uses lower 3 bits of $17 as byte offset (= 3) –Sets destination register to that byte of source ABCDEF 0x1000x1070x103 Increasing address $1: ABCDEF Byte Number: $1: 00 89

CS 213 F’98– 16 – class09.ppt Preparing Destination Step 2. Zero byte at dest = 0x20d Pointer dest in register $16 ldq_u $2, 0($16) –Rounds effective address 0x20d down to nearest quad word boundary ( 0x208 ) –Loads entire quad word into register mskbl $2, $16, $2 –Uses lower 3 bits of $16 as byte offset (= 5) –Copies source to destination, but zeros specified byte Increasing address $2: FFEEDDCCBBAA9988 Byte Number: FFEEDDCCBBAA9988 0x2080x20f0x20d $2: FFEE00CCBBAA9988 Byte Number:

CS 213 F’98– 17 – class09.ppt Step 3. Position Source Byte Pointer dest in register $16 insbl $1, $16, $1 –Uses lower 3 bits of $16 as byte offset (= 5) –Shifts low order byte of source into specified byte position Step 4. Form Destination Quad-Word bis $1, $2, $2 –Merges new byte into destination $2: FFEE00CCBBAA9988 Merging Source into Destination $1: $1: $2: FFEE89CCBBAA9988

CS 213 F’98– 18 – class09.ppt Step 5. Update Destination Quad-Word Pointer dest in register $16 stq_u $2, 0($16) –Rounds effective address 0x20d down to nearest quad word boundary ( 0x208 ) –Stores entire quad word into memory Net Effect Before: After: Updating Destination $2: FFEE89CCBBAA9988 FFEEDDCCBBAA9988 0x2080x20f0x20d FFEE89CCBBAA9988 0x2080x20f0x20d

CS 213 F’98– 19 – class09.ppt Union Allocation Principles Overlay union elements Allocate according to largest element Can only use one field at a time union U1 { char c; int i[2]; long int v; } *up; c i[0]i[1] v up+0up+4up+8 struct S1 { char c; int i[2]; long int v; } *sp; ci[0]i[1]v sp+0sp+4sp+8sp+16sp+24

CS 213 F’98– 20 – class09.ppt Implementing “Tagged” Union Structure can hold 3 kinds of data Only one form at any given time Identify particular kind with flag type typedef enum { CHAR, INT, LONG } utype; typedef struct { utype type; union { char c; int i[2]; long int v; } e; } store_ele, *store_ptr; store_ele k; k.e.i[0]k.e.i[1] k.e.v k.e k.e.c k.type

CS 213 F’98– 21 – class09.ppt Using “Tagged” Union store_ele k1; k1.type = CHAR; k1.e.c = ’a’; ’a’ 0 store_ele k2; k2.type = INT; k2.e.i[0] = 17; k2.e.i[1] = 47; store_ele k3; k3.type = LONG; k1.e.v = 0xFF00FF00FF00FF00; 17471FF00FF00FF00FF002

CS 213 F’98– 22 – class09.ppt typedef union { float f; unsigned u; } bit_float_t; float bit2float(unsigned u) { bit_float_t arg; arg.u = u; return arg.f; } u f 04 unsigned float2bit(float f) { bit_float_t arg; arg.f = f; return arg.u; } Using Union to Access Bit Patterns Get direct access to bit representation of float bit2float generates float with given bit pattern –NOT the same as (float) u float2bit generates bit pattern from float –NOT the same as (unsigned) f

CS 213 F’98– 23 – class09.ppt Byte Ordering Idea Long/quad words stored in memory as 4/8 consecutive bytes Which is most (least) significant? Can cause problems when exchanging binary data between machines Big Endian Most significant byte has lowest address IBM 360/370, Motorola 68K, Sparc Little Endian Least significant byte has lowest address Intel x86, VAX Alpha Chip can be configured to operate either way Our’s are little endian Cray T3E Alpha’s are big endian

CS 213 F’98– 24 – class09.ppt Byte Ordering Example union { unsigned char c[8]; unsigned short s[4]; unsigned int i[2]; unsigned long l[1]; } dw; c[3] s[1] i[0] c[2]c[1] s[0] c[0]c[7] s[3] i[1] c[6]c[5] s[2] c[4] l[0]

CS 213 F’98– 25 – class09.ppt Byte Ordering Example (Cont). int j; for (j = 0; j < 8; j++) dw.c[j] = 0xf0 + j; printf("Characters 0-7 == [0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x]\n", dw.c[0], dw.c[1], dw.c[2], dw.c[3], dw.c[4], dw.c[5], dw.c[6], dw.c[7]); printf("Shorts 0-3 == [0x%x,0x%x,0x%x,0x%x]\n", dw.s[0], dw.s[1], dw.s[2], dw.s[3]); printf("Ints 0-1 == [0x%x,0x%x]\n", dw.i[0], dw.i[1]); printf("Long 0 == [0x%lx]\n", dw.l[0]);

CS 213 F’98– 26 – class09.ppt Byte Ordering on Alpha Little Endian Characters 0-7 == [0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7] Shorts 0-3 == [0xf1f0,0xf3f2,0xf5f4,0xf7f6] Ints 0-1 == [0xf3f2f1f0,0xf7f6f5f4] Long 0 == [0xf7f6f5f4f3f2f1f0] Output on Alpha: c[3] s[1] i[0] LSBMSB c[2]c[1] s[0] c[0] LSBMSB LSBMSB c[7] s[3] i[1] LSBMSB c[6]c[5] s[2] c[4] LSBMSB LSBMSB f0f1f2f3f4f5f6f7 Print l[0] LSBMSB

CS 213 F’98– 27 – class09.ppt Byte Ordering on x86 Little Endian Characters 0-7 == [0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7] Shorts 0-3 == [0xf1f0,0xf3f2,0xf5f4,0xf7f6] Ints 0-1 == [0xf3f2f1f0,0xf7f6f5f4] Long 0 == [f3f2f1f0] Output on Pentium: f0f1f2f3f4f5f6f7 c[3] s[1] i[0] LSBMSB c[2]c[1] s[0] c[0] LSBMSB LSBMSB c[7] s[3] i[1] LSBMSB c[6]c[5] s[2] c[4] LSBMSB LSBMSB Print l[0] LSBMSB

CS 213 F’98– 28 – class09.ppt Byte Ordering on Sun Big Endian Characters 0-7 == [0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7] Shorts 0-3 == [0xf0f1,0xf2f3,0xf4f5,0xf6f7] Ints 0-1 == [0xf0f1f2f3,0xf4f5f6f7] Long 0 == [0xf0f1f2f3] Output on Sun: c[3] s[1] i[0] LSBMSB c[2]c[1] s[0] c[0] MSBLSB MSB c[7] s[3] i[1] LSBMSB c[6]c[5] s[2] c[4] MSBLSB MSB f0f1f2f3f4f5f6f7 Print l[0] MSBLSB

CS 213 F’98– 29 – class09.ppt Alpha Memory Layout Segments Data –Static space for global variables »Allocation determined at compile time »Access via $gp –Dynamic space for runtime allocation »E.g., using malloc Text –Stores machine code for program Stack –Implements runtime stack –Access via $sp Reserved –Used by operating system »page tables, process info, etc. Reserved Text (Code) Static Data Not yet allocated Stack Dynamic Data FF Reserved Not yet allocated $gp $sp

CS 213 F’98– 30 – class09.ppt Alpha Memory Allocation Address Range User code can access memory locations in range 0x to 0x000003FF Nearly 2 42  X10 12 byte range In practice, programs access far fewer Dynamic Memory Allocation Virtual memory system only allocates blocks of memory (“pages”) as needed As stack reaches lower addresses, add to lower allocation As break moves toward higher addresses, add to upper allocation –Due to calls to malloc, calloc, etc. Text (Code) Static Data Stack Region Dynamic Data break Current $sp Minimum $sp