CS429: Computer Organization and Architecture

Similar documents
Machine-Level Programming IV: Structured Data

Assembly IV: Complex Data Types. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Machine-level Programs Data

Assembly IV: Complex Data Types

MACHINE-LEVEL PROGRAMMING IV: Computer Organization and Architecture

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

Memory Organization and Addressing

More in-class activities / questions

Basic Data Types The course that gives CMU its Zip! Machine-Level Programming IV: Data Feb. 5, 2008

Integral. ! Stored & operated on in general registers. ! Signed vs. unsigned depends on instructions used

Basic Data Types The course that gives CMU its Zip! Machine-Level Programming IV: Structured Data Sept. 18, 2003.

Basic Data Types. Lecture 6A Machine-Level Programming IV: Structured Data. Array Allocation. Array Access Basic Principle

Giving credit where credit is due

Machine-Level Programming V: Advanced Topics

Structs and Alignment CSE 351 Spring

Giving credit where credit is due

Assembly IV: Complex Data Types. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

u Arrays One-dimensional Multi-dimensional (nested) Multi-level u Structures Allocation Access Alignment u Floating Point

CS241 Computer Organization Spring Data Alignment

CISC 360. Machine-Level Programming IV: Structured Data Sept. 24, 2008

Computer Organization: A Programmer's Perspective

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Basic Data Types. CS429: Computer Organization and Architecture. Array Allocation. Array Access

Page 1. Basic Data Types CISC 360. Machine-Level Programming IV: Structured Data Sept. 24, Array Access. Array Allocation.

Machine-Level Prog. IV - Structured Data

L14: Structs and Alignment. Structs and Alignment. CSE 351 Spring Instructor: Ruth Anderson

CSC 252: Computer Organization Spring 2018: Lecture 9

CS429: Computer Organization and Architecture

Arrays and Structs. CSE 351 Summer Instructor: Justin Hsia. Teaching Assistants: Josie Lee Natalie Andreeva Teagan Horkan.

Machine Level Programming: Arrays, Structures and More

Computer Systems CEN591(502) Fall 2011

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19

Structs & Alignment. CSE 351 Autumn Instructor: Justin Hsia

Data Structures in Memory!

Structs and Alignment

Machine-Level Programming V: Advanced Topics

Byte Ordering. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Machine Representa/on of Programs: Arrays, Structs and Unions. This lecture. Arrays Structures Alignment Unions CS Instructor: Sanjeev Se(a

Data Representation and Storage. Some definitions (in C)

Byte Ordering. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Today. Machine-Level Programming V: Advanced Topics. x86-64 Linux Memory Layout. Memory Allocation Example. Today. x86-64 Example Addresses

CISC 360 Midterm Exam - Review Alignment

We would like to find the value of the right-hand side of this equation. We know that

Topics of this Slideset. CS429: Computer Organization and Architecture. It s Bits All the Way Down. Why Binary? Why Not Decimal?

CS429: Computer Organization and Architecture

Structs and Alignment

Machine- level Programming IV: Data Structures. Topics Arrays Structs Unions

Structures, Unions Alignment, Padding, Bit Fields Access, Initialization Compound Literals Opaque Structures Summary. Structures

Today. Machine-Level Programming V: Advanced Topics. x86-64 Linux Memory Layout. Memory Allocation Example. Today. x86-64 Example Addresses

17. Instruction Sets: Characteristics and Functions

CS 31: Intro to Systems Arrays, Structs, Strings, and Pointers. Kevin Webb Swarthmore College March 1, 2016

Reverse Engineering II: Basics. Gergely Erdélyi Senior Antivirus Researcher

CSE351: Memory, Data, & Addressing I

Topics. CS429: Computer Organization and Architecture. File Inclusion. A Simple C Program. Intro to C

Machine- Level Representation: Data

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists

Reverse Engineering II: The Basics

CS C Primer. Tyler Szepesi. January 16, 2013

Data Representation and Storage

TILE PROCESSOR APPLICATION BINARY INTERFACE

CS429: Computer Organization and Architecture

CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.

Machine Programming 4: Structured Data

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Survey. Motivation 29.5 / 40 class is required

ECE 486/586. Computer Architecture. Lecture # 6

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Machine-Level Programming V: Unions and Memory layout

Memory, Data, & Addressing I

CS240: Programming in C

Machine/Assembler Language Putting It All Together

Hardware: Logical View

CS 61C: Great Ideas in Computer Architecture. (Brief) Review Lecture

Pointers (1A) Young Won Lim 2/6/18

CPEG421/621 Tutorial

Type (1A) Young Won Lim 2/17/18

Low Level Optimization by Data Alignment. Presented by: Mark Hauschild

Instruction Sets: Characteristics and Functions

Pointers (1A) Young Won Lim 2/10/18

Dept. of Computer and Information Science (IDA) Linköpings universitet Sweden

Arrays and Pointers in C & C++

Math 230 Assembly Programming (AKA Computer Organization) Spring 2008

SH-5 Generic and C Specific ABI

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

CMSC 313 Lecture 13 Project 4 Questions Reminder: Midterm Exam next Thursday 10/16 Virtual Memory

EC 413 Computer Organization

Representation of Information

Lectures 5-6: Introduction to C

Systems I. Machine-Level Programming VII: Structured Data

Do not start the test until instructed to do so!

PRINCIPLES OF OPERATING SYSTEMS

CENG3420 Lecture 03 Review

Lecture 2 More C++ Jon Macey

Final CSE 131B Spring 2004

Data in Memory. variables have multiple attributes. variable

Announcements HW1 is due on this Friday (Sept 12th) Appendix A is very helpful to HW1. Check out system calls

Introduction to C. Why C? Difference between Python and C C compiler stages Basic syntax in C

M1 Computers and Data

C Programming Review CSC 4320/6320

EL2310 Scientific Programming

Transcription:

CS429: Computer Organization and Architecture Dr. Bill Young Department of Computer Sciences University of Texas at Austin Last updated: March 5, 2018 at 05:33 CS429 Slideset 11: 1

Alignment CS429 Slideset 11: 2

Structures and Alignment Unaligned Data 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; Aligned Data Primitive data type requires K bytes Address must be a multiple of K extra extra c 3 bytes i[0] i[1] 4 bytes v p+0 p+4 p+8 p+16 p+24 Multiple of 4 Multiple of 8 Multiple of 8 Multiple of 8 CS429 Slideset 11: 3

Alignment Principles Aligned Data Primitive data type requires K bytes Address must be a multiple of K Required on some machines; advised on x86-64 Motivation for Aligning Data Memory accessed by (aligned) chunks of 4, 8 or more bytes (system dependent) It s inefficient to load or store datum that spans quad word boundaries Virtual memory is trickier when datum spans 2 pages Compiler Inserts gaps in structure to ensure correct alignment of fields CS429 Slideset 11: 4

Specific Cases of Alignment (x86-64) 1 byte: char,... no restrictions on address 2 bytes: short,... lowest 1 bit of address must be 0 2 4 bytes: int, float,... lowest 2 bits of address must be 00 2 8 bytes: double, long, char *,... lowest 3 bits of address must be 000 2 16 bytes: long double (GCC on Linux) lowest 4 bits of address must be 0000 2 CS429 Slideset 11: 5

Satisfying Alignment with Structures Within structure: Must satisfy each element s alignment requirement Overall structure placement struct S1 { char c; int i [2]; double v; } p; Each structure has alignment requirement K, where K is the largest alignment of any element Initial address and structure length must be multiples of K Example: K = 8, due to double element c 3 bytes i[0] i[1] 4 bytes v p+0 p+4 p+8 p+16 p+24 extra Multiple of 4 Multiple of 8 CS429 Slideset 11: 6 extra Multiple of 8 Multiple of 8

Meeting Overall Alignment Requirement For largest alignment requirement K Overall structure must be multiple of K struct S2 { double v; int i [2]; char c; } v i[0] i[1] c extra 7 bytes p+0 p+8 p+16 p+24 Multiple of 8 CS429 Slideset 11: 7

Arrays of Structures Overall structure length multiple of K Satisfy alignment requirement for every element struct S2 { double v; int i [2]; char c; } a [10]; a[0] a[1] a[2]... a+0 a+24 a+48 a+72 v i[0] i[1] c extra 7 bytes p+24 p+32 p+40 p+48 CS429 Slideset 11: 8

Accessing Array Elements Compute array offset 12*idx sizeof(s3), including alignment spacers Element j is at offset 8 within structure Assembler gives offset a+8 Resolved during linking struct S3 { short i ; float v; short j ; } a [10]; CS429 Slideset 11: 9

Accessing Array Elements a[0]... a[idx]... a+0 a+12 a+12*idx i 2 bytes v j 2 bytes a+12*idx a+12*idx+8 short get j ( int idx ) { return a[ idx ]. j ; } # %rdi holds idx leaq (%rdi,%rdi,2),%rax # 3 idx movzwl a+8(,%rax,4), %eax CS429 Slideset 11: 10

Saving Space Put large data types first! Instead of: struct S4 { char c; int i ; char d; } p; do this: struct S5 { int i ; char c; char d; } p; Effect (K = 4) c 3 bytes i d 3 bytes i c d 2 bytes CS429 Slideset 11: 11

Union Allocation Principles Overlay union elements. Allocate according to the largest element. Can only use one field at a time. union U1 { char c; int i [2]; double v; } up c i[0] v i[1] up+0 up+4 up+8 CS429 Slideset 11: 12

Using Union to Access Bit Patterns typedef union { float f ; unsigned u; } bit float t ; u f 0 4 float bit2float (unsigned u) { bit float t arg ; arg.u = u; return arg. f ; } unsigned float2bit ( float f ) { bit float t arg ; arg. f = f ; return arg.u; } Get direct representation to bit representation of float. bit2float generates float with given bit pattern. Note: this is not the same as (float) u. float2bit generates bit pattern from float. Note: this is not the same as (unsigned) f. CS429 Slideset 11: 13

Byte Order Revisited Idea Short/long/quad words stored in memory as 2/4/8 consecutive bytes. Which is the most (least) significant? Can cause problems when exchanging binary data between machines. Big Endian Most significant byte has lowest address. PowerPC, Sparc Little Endian Least significant byte has lowest address. Intel x86, Alpha CS429 Slideset 11: 14

Byte Ordering Example union { unsigned char c [8]; unsigned short s [4]; unsigned int i [2]; unsigned long l ; } dw; c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] s[0] s[1] s[2] s[3] i[0] i[1] l int j ; for ( j = 0; j < 8; j++) dw. c[ j ] = 0xf0 + j ; printf ( Chars 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 == [0x%lx ]\n, dw. l ) ; CS429 Slideset 11: 15

Byte Ordering on the x86 Little Endian f0 f1 f2 f3 f4 f5 f6 f7 c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] LSB MSB LSB MSB LSB MSB LSB MSB s[0] s[1] s[2] s[3] LSB i[0] MSB LSB i[1] MSB LSB MSB l Print Output on Pentium: Chars 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] CS429 Slideset 11: 16

Byte Ordering on Sun Big Endian f0 f1 f2 f3 f4 f5 f6 f7 c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] MSB LSB MSB LSB MSB LSB MSB LSB s[0] s[1] s[2] s[3] MSB LSB MSB LSB i[0] i[1] MSB l LSB Print Output on Sun: Chars 0-7 == [0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7] Shorts 0-3 == [0xf0f1,0xf2f3,0xf4f5,0xf6f7] Ints 0-1 == [0xf0f1f2f3,0xf4f5f6f7] Long 0 == [0xf0f1f2f3f4f5f6f7] CS429 Slideset 11: 17

Summary Arrays in C Contiguous allocation of memory, row order. Pointer to first element. No bounds checking. Compiler Optimizations Compiler often turns array code into pointer code. Uses addressing modes to scale array indices. Lots of tricks to improve array indexing in loops. Structures Allocate bytes in order declared. Pad in middle and at end to satisfy alignment. Unions Overlay declarations. Way to circumvent type system. CS429 Slideset 11: 18