More in-class activities / questions

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

CS241 Computer Organization Spring Data Alignment

Data Structures in Memory!

CS429: Computer Organization and Architecture

Machine-level Programs Data

Computer Systems CEN591(502) Fall 2011

Machine-Level Programming IV: Structured Data

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

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

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

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.

CISC 360 Midterm Exam - Review Alignment

Giving credit where credit is due

Giving credit where credit is due

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

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

Last time. Last Time. Last time. Dynamic Array Multiplication. Dynamic Nested Arrays

MACHINE-LEVEL PROGRAMMING IV: Computer Organization and Architecture

Machine-Level Prog. IV - Structured Data

Assembly IV: Complex Data Types

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

Structs and Alignment CSE 351 Spring

Memory Organization and Addressing

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

Computer Organization: A Programmer's Perspective

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

CSC 252: Computer Organization Spring 2018: Lecture 9

Referencing Examples

Machine-Level Programming V: Advanced Topics

Machine-Level Programming V: Advanced Topics

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

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

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

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

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

Machine Level Programming: Arrays, Structures and More

CS429: Computer Organization and Architecture

Machine Programming 4: Structured Data

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

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

Structs and Alignment

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

Structs & Alignment. CSE 351 Autumn Instructor: Justin Hsia

Data Representation and Storage. Some definitions (in C)

Machine-Level Programming V: Unions and Memory layout

Introduction to Computer Systems , fall th Lecture, Sep. 14 th

CS240: Programming in C

Structs and Alignment

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

EECS 213 Fall 2007 Midterm Exam

Data Representation and Storage

Systems I. Machine-Level Programming VII: Structured Data

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

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

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

Byte Ordering. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

V850 Calling Convention

17. Instruction Sets: Characteristics and Functions

CSE 230 Intermediate Programming in C and C++

CPEG421/621 Tutorial

1.'(*2#3%* 2+6.)* !!"##$%$&'()&*$! +',-.$+'/-$! #0!"#$1"&2"/#',$! &'!)&,21'$3)*!40*,$ ! 5*'6728'*,20*"#$! 9)#46728'*,20*"#$:*',('7;$!

Memory Management. Memory Management

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

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

Porting 32-bit Applications to the Itanium Architecture - Lab 3: Managing Data Size

Machine- Level Representation: Data

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

CS , Fall 2004 Exam 1

Hardware: Logical View

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

Floating Point Stored & operated on in floating point registers Intel GAS Bytes C Single s 4 float Double l 8 double Extended t 10/12/16 long double

C Programming Review CSC 4320/6320

CS , Fall 2001 Exam 1

Introduction to Computer Systems. Exam 1. February 22, This is an open-book exam. Notes are permitted, but not computers.

Lectures 5-6: Introduction to C

Layers of Abstraction. CS 3330: More C. printf. Last time

C++ Structures Programming Workshop 2 (CSCI 1061U)

Unit IV & V Previous Papers 1 mark Answers

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

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

FUNCTION POINTERS. int (*pf)(); // pf is a pointer to a function returning an int.

High Performance Computing in C and C++

CS , Fall 2002 Exam 1

This manual is for Libffi, a portable foreign-function interface library. Copyright c 2008, 2010, 2011 Red Hat, Inc. Permission is granted to copy,

Computer System and programming in C

Syntax and Variables

CS241 Computer Organization Spring Loops & Arrays

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

Prac*ce problem on webpage

TILE PROCESSOR APPLICATION BINARY INTERFACE

Memory Management william stallings, maurizio pizzonia - sistemi operativi

CIS 2107 Computer Systems and Low-Level Programming Spring 2012 Final

Low-Level C Programming. Memory map Pointers Arrays Structures

Question Points Score Total: 100

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

Machine/Assembler Language Putting It All Together

ECE 2035 Programming HW/SW Systems Fall problems, 7 pages Exam Two 23 October 2013

memory management Vaibhav Bajpai

First of all, it is a variable, just like other variables you studied

Transcription:

Reading Responses 6 Yes, 1 No More often, reiew questions? Current Lab Format Yes: 6 yes, 3 No Maybe a 5-min introduction to help get started? More in-class actiities / questions Yes: 5, No: 1 Long Slides are boring / hard to follow: 3 BTW, no luck with Flash lab on linux

Union Allocation Allocate according to largest element Can only use ones field at a time union U1 { c int i[2]; i[0] i[1] double ; } *up; up+0 up+4 up+8 struct S1 { int i[2]; double ; } *sp; c 3 bits i[0] i[1] 4 bits sp+0 sp+4 sp+8 sp+16 sp+24

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; } Same as (float) u? Same as (unsigned) f?

Summary Arrays in C Contiguous allocation of memory Aligned to satisfy eery element s alignment requirement Pointer to first element No bounds checking Structures Allocate bytes in order declared Pad in middle and at end to satisfy alignment Unions Oerlay declarations Way to circument type system

Alignment Aligned Data Primitie data type requires K bytes Address must be multiple of K Required on some machines; adised on IA32 treated differently by IA32 Linux, x86-64 Linux, and Windows! Motiation for Aligning Data Memory accessed by (aligned) chunks of 4 or 8 bytes (system dependent) Inefficient to load or store datum that spans quad word boundaries Virtual memory ery tricky when datum spans 2 pages Compiler Inserts gaps in structure to ensure correct alignment of fields

Specific Cases of Alignment (IA32) 1 byte: char, no restrictions on address 2 bytes: short, lowest 1 bit of address must be 0 2 4 bytes: int, float, char *, lowest 2 bits of address must be 00 2 8 bytes: double, Windows (and most other OS s & instruction sets): lowest 3 bits of address must be 000 2 Linux: lowest 2 bits of address must be 00 2 i.e., treated the same as a 4-byte primitie data type 12 bytes: long double Windows, Linux: lowest 2 bits of address must be 00 2 i.e., treated the same as a 4-byte primitie data type

Satisfying Alignment with Structures Within structure: Must satisfy element s alignment requirement Oerall structure placement Each structure has alignment requirement K K = Largest alignment of any element struct S1 { int i[2]; double ; } *p; Initial address & structure length must be multiples of K Example (under Windows or x86-64): K = 8, due to double element c 3 bytes i[0] i[1] 4 bytes p+0 p+4 p+8 p+16 p+24 Multiple of 4 Multiple of 8 Multiple of 8 Multiple of 8

Different Alignment Conentions x86-64 or IA32 Windows: K = 8, due to double element struct S1 { int i[2]; double ; } *p; c 3 bytes i[0] i[1] 4 bytes p+0 p+4 p+8 p+16 p+24 c 3 bytes i[0] i[1] p+0 p+4 p+8 p+12 p+20 IA32 Linux K = 4; double treated like a 4-byte data type

Saing Space Put large data types first struct S1 { int i[2]; double ; } *p; struct S2 { double ; int i[2]; } *p; Effect (example x86-64, both hae K=8) c 3 bytes i[0] i[1] 4 bytes p+0 p+4 p+8 p+16 p+24 i[0] i[1] c p+0 p+8 p+16

Arrays of Structures Satisfy alignment requirement for eery element struct S2 { double ; int i[2]; } a[10]; a+0 a[0] a[1] a[2] a+24 a+48 a+36 i[0] i[1] c a+24 a+32 a+40 7 bytes a+48

Accessing Array Elements Compute array offset 12i Compute offset 8 with structure Assembler gies offset a+8 Resoled during linking a+0 a[0] struct S3 { short i; float ; short j; } a[10]; a[i] a+12i i 2 bytes j 2 bytes a+12i a+12i+8 short get_j(int idx) { return a[idx].j; } # %eax = idx leal (%eax,%eax,2),%eax # 3*idx moswl a+8(,%eax,4),%eax

Offset per field, total size, and alignment requirement for IA32 struct { int i; int j; char d; } P1; struct { short w[3]; char c[3]; } P2; struct { short w[3]; char *c[3]; } P3;

Offset per field, total size, and alignment requirement for IA32 P1 0 4 8 12 Total = 16 Alignment = 4 P2 0 6 Total = 10 Alignment = 2 P3 0 8 Total = 20 Alignment = 4