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

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

Machine-Level Prog. IV - Structured Data

Systems I. Machine-Level Programming VII: Structured Data

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

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

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.

Giving credit where credit is due

Assembly IV: Complex Data Types

Giving credit where credit is due

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

Referencing Examples

Machine-Level Programming IV: Structured Data

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

Data Structures in Memory!

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

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

The Hardware/So=ware Interface CSE351 Winter 2013

2 Systems Group Department of Computer Science ETH Zürich. Argument Build 3. %r8d %rax. %r9d %rbx. Argument #6 %rcx. %r10d %rcx. Static chain ptr %rdx

CS241 Computer Organization Spring Loops & Arrays

Machine-level Programs Data

Machine Programming 4: Structured Data

h"p://news.illinois.edu/news/12/0927transient_electronics_johnrogers.html

Lecture 7: More procedures; array access Computer Architecture and Systems Programming ( )

Computer Organization: A Programmer's Perspective

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

Machine Level Programming: Arrays, Structures and More

Computer Systems CEN591(502) Fall 2011

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

Memory Organization and Addressing

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

Machine-Level Programming IV: Structured Data

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

Arrays. CSE 351 Autumn Instructor: Justin Hsia

Arrays. CSE 351 Autumn Instructor: Justin Hsia

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

CS429: Computer Organization and Architecture

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

More in-class activities / questions

CS429: Computer Organization and Architecture

Machine- Level Programming IV: x86-64 Procedures, Data

Prac*ce problem on webpage

MACHINE-LEVEL PROGRAMMING IV: Computer Organization and Architecture

Machine- Level Representation: Data

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

Compiling C Programs Into X86-64 Assembly Programs

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Arrays. Young W. Lim Mon. Young W. Lim Arrays Mon 1 / 17

Arrays. Young W. Lim Wed. Young W. Lim Arrays Wed 1 / 19

CSC 252: Computer Organization Spring 2018: Lecture 9

CS241 Computer Organization Spring Data Alignment

Structs and Alignment CSE 351 Spring

EECS 213 Fall 2007 Midterm Exam

Assembly I: Basic Operations. Jo, Heeseung

Executables & Arrays. CSE 351 Autumn Instructor: Justin Hsia

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

CISC 360 Midterm Exam - Review Alignment

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung

CMSC 313 Fall2009 Midterm Exam 2 Section 01 Nov 11, 2009

CAS CS Computer Systems Spring 2015 Solutions to Problem Set #2 (Intel Instructions) Due: Friday, March 20, 1:00 pm

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

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

The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002

CS , Fall 2001 Exam 1

Process Layout and Function Calls

CS213. Machine-Level Programming III: Procedures

Machine-Level Programming III: Procedures

Machine-level Programming (3)

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only

Sungkyunkwan University

Machine Programming 3: Procedures

Data Representa/ons: IA32 + x86-64

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

Assembly I: Basic Operations. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Assembly II: Control Flow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Homework. In-line Assembly Code Machine Language Program Efficiency Tricks Reading PAL, pp 3-6, Practice Exam 1

Instruction Set Architecture

Instruction Set Architecture

Building an Executable

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

1 /* file cpuid2.s */ 4.asciz "The processor Vendor ID is %s \n" 5.section.bss. 6.lcomm buffer, section.text. 8.globl _start.

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction

CS241 Computer Organization Spring 2015 IA

Machine-Level Programming I: Introduction Jan. 30, 2001

Applications of Arrays (1A) Young Won Lim 3/15/17

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p

Introduction to Computer Systems. Exam 1. February 22, Model Solution fp

University of Washington

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

Structs and Alignment

Y86 Processor State. Instruction Example. Encoding Registers. Lecture 7A. Computer Architecture I Instruction Set Architecture Assembly Language View

CS241 Computer Organization Spring Addresses & Pointers

Code Optimization April 6, 2000

Machine-level Representation of Programs. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Machine Programming 1: Introduction

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

Instruction Set Architecture

Transcription:

ssembly IV: Complex Data Types Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu

Basic Data Types Integer Stored & operated on in general registers Signed vs. unsigned depends on instructions used Intel GS Bytes C byte b 1 [unsigned] char word w 2 [unsigned] short double word l 4 [unsigned] int Floating point Stored & operated on in floating point registers Intel GS Bytes C Single s 4 float Double l 8 double Extended t 10/12 long double 2

Complex Data Types Complex data types in C Pointers rrays Structures Unions... Can be combined Pointer to pointer, pointer to array, rray of array, array of structure, array of pointer, Structure in structure, pointer in structure, array in structure, 3

rray llocation Basic principle: T [L]; rray of data type T and length L Contiguously allocated region of L*sizeof(T) bytes char string[12]; int val[5]; double a[4]; x x + 12 x x + 4 x + 8 x + 12 x + 16 x + 20 x x + 8 x + 16 char *p[3]; x x + 4 x + 8 x + 24 x + 32 4

rray ccess Basic principle: T [L]; rray of data type T and length L Identifier can be used as a pointer to element 0 int val[5]; 1 5 2 1 3 x x + 4 x + 8 x + 12 x + 16 x + 20 Reference Type Value val[4] int 3 val int * x val + 1 int * x + 4 &val[2] int * x + 8 val[5] int?? *(val+1) int 5 val + i int * x + 4 * i 5

rray Example Notes typedef int zip_dig[5]; zip_dig cmu = { 1, 5, 2, 1, 3 }; zip_dig mit = { 0, 2, 1, 3, 9 }; zip_dig ucb = { 9, 4, 7, 2, 0 }; zip_dig cmu; 1 5 2 1 3 16 20 24 28 32 36 zip_dig mit; 0 2 1 3 9 36 40 44 48 52 56 zip_dig ucb; 9 4 7 2 0 56 60 64 68 72 76 Example arrays were allocated in successive 20 byte blocks Not guaranteed to happen in general 6

rray ccessing Example (1) Computation Register %edx contains starting address of array Register %eax contains array index Desired digit at 4 * %eax + %edx Use memory reference: (%edx,%eax,4) int get_digit (zip_dig z, int dig) { return z[dig]; } Memory Reference Code # %edx = z # %eax = dig movl (%edx,%eax,4),%eax # z[dig] 7

rray ccessing Example (2) zip_dig cmu; 1 5 2 1 3 16 20 24 28 32 36 zip_dig mit; 0 2 1 3 9 36 40 44 48 52 56 zip_dig ucb; 9 4 7 2 0 56 60 64 68 72 76 Code does not do any bounds checking! Reference ddress Value Guaranteed? mit[3] 36 + 4 * 3 = 48 3 Yes mit[5] 36 + 4 * 5 = 56 9 No mit[-1] 36 * 4 * (-1) = 32 3 No cmu[15] 16 + 4 * 15 = 76?? No Out of range behavior implementation-dependent No guaranteed relative allocation of different arrays 8

rray Loop Example (1) Original source Transformed version s generated by GCC Eliminate loop variable i Convert array code to pointer code Express in do-while form No need to test at entrance int zd2int(zip_dig z){ int i; int zi = 0; for (i = 0; i < 5; i++) zi = 10 * zi + z[i]; return zi; } int zd2int(zip_dig z) { int zi = 0; int *zend = z + 4; do { zi = 10 * zi + *z; z++; } while(z <= zend); return zi; } 9

rray Loop Example (2) Registers %ecx %eax %ebx z zi zend int zd2int(zip_dig z){ int zi = 0; int *zend = z + 4; do { zi = 10 * zi + *z; z++; } while(z <= zend); return zi; } z++ increments by 4 10 * zi + *z = *z + 2*(zi+4*zi) # %ecx = z xorl %eax,%eax # zi = 0 leal 16(%ecx),%ebx # zend = z + 4.L59: leal (%eax,%eax,4),%edx # 5*zi movl (%ecx),%eax # *z addl $4,%ecx # z++ leal (%eax,%edx,2),%eax # zi = *z + 2*(5*zi) cmpl %ebx,%ecx # z : zend jle.l59 # if <= goto loop 10

Nested rray (1) Declaration: T [R][C]; 2D array of data type T R rows, C columns rray size = R * C * sizeof(t) rrangement Row-major ordering [0][0] [R-1][0] [0][C-1] [R-1][C-1] [0] [0] [0] [C-1] [1] [0] [1] [C-1] [R-1] [0] [R-1] [C-1] 4*R*C Bytes 11

Nested rray (2) C code Variable pgh denotes array of 4 elements llocated contiguously Each element is an array of 5 int s llocated contiguously int pgh[4][5] = {{1, 5, 2, 0, 6}, {1, 5, 2, 1, 3 }, {1, 5, 2, 1, 7 }, {1, 5, 2, 2, 1 }}; Row-major ordering of all elements guaranteed int pgh[4][5]; 1 5 2 0 6 1 5 2 1 3 1 5 2 1 7 1 5 2 2 1 76 96 116 136 156 12

Nested rray ccess (1) Row vectors [i] is array of C elements Each element of type T requires K bytes Starting address + i * (C * K) int [R][C]; [0] [i] [R-1] [0] [0] [0] [C-1] [i] [0] [i] [C-1] [R-1] [0] [R-1] [C-1] +i*c*4 +(R-1)*C*4 13

Nested rray ccess (2) Row vector pgh[index] is array of 5 int s Starting address pgh + 20 * index Code int *get_pgh_zip(int index) { return pgh[index]; } Computes and returns address Compute as pgh + 4 * (index + 4 * index) # %eax = index leal (%eax,%eax,4),%eax leal pgh(,%eax,4),%eax # 5 * index # pgh + (20 * index) 14

Nested rray ccess (3) rray elements [i][j] is element of type T ddress +i*(c*k)+j*k = +(i*c+j)*k int [R][C]; [0] [i] [R-1] [0] [0] [0] [C-1] [i] [R-1] [j] [0] [R-1] [C-1] +i*c*4 +(i*c+j)*4 +(R-1)*C*4 15

Nested rray ccess (4) rray Elements pgh[index][dig] is int ddress: pgh+20*index+4*dig int get_pgh_digit (int index, int dig) { return pgh[index][dig]; } Code Computes address pgh+4*dig+4*(index+4*index) movl performs memory reference # %ecx = dig # %eax = index leal 0(,%ecx,4),%edx leal (%eax,%eax,4),%eax movl pgh(%edx,%eax,4),%eax # 4*dig # 5*index # *(pgh + 4*dig + 20*index) 16

Nested rray ccess (5) Strange referencing examples Code does not do any bounds checking Ordering of elements within array guaranteed int pgh[4][5]; 1 5 2 0 6 1 5 2 1 3 1 5 2 1 7 1 5 2 2 1 76 96 116 136 156 Reference ddress Value Guaranteed? pgh[3][3] 76 +20*3+4*3 = 148 2 Yes pgh[2][5] 76 +20*2+4*5 = 136 1 Yes pgh[2][-1] 76 +20*2+4*(-1) = 112 3 Yes pgh[4][-1] 76 +20*4+4*(-1) = 152 1 Yes pgh[0][19] 76 +20*0+4*19 = 152 1 Yes pgh[0][-1] 76 +20*0+4*(-1) = 72?? No 17

Summary rrays in C Contiguous allocation of memory 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 18

Structures Concept Contiguously-allocated region of memory Refer to members within structure by names Members may be of different type struct rec { int i; int a[3]; int *p; }; void set_i (struct rec *r, int val) { r->i = val; } # %eax = val # %edx = r movl %eax,(%edx) Memory Layout i a p 0 4 16 20 ssembly # Mem[r] = val 19

Structure Referencing (1) Generating pointer to structure member Offset of each member determined at compile time struct rec { int i; int a[3]; int *p; }; int *find_a (struct rec *r, int idx) { return &r->a[idx]; } r i a p 0 4 16 r + 4 + 4*idx # %ecx = idx # %edx = r leal 0(,%ecx,4),%eax leal 4(%eax,%edx),%eax # 4*idx # r+4*idx+4 20

Structure Referencing (2) Generating pointer to member (cont d) struct rec { int i; int a[3]; int *p; }; void set_p (struct rec *r) { r->p = &r->a[r->i]; } r i a p 0 4 16 r + 4 + 4*i # %edx = r movl (%edx),%ecx leal 0(,%ecx,4),%eax leal 4(%eax,%edx),%eax movl %eax,16(%edx) # r->i # 4*(r->i) # r+4+4*(r->i) # update r->p 21

lignment (1) ligned data Primitive data type requires K bytes ddress must be multiple of K Required on some machines; advised on I-32 treated differently by Linux and Windows Motivation for aligning data 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 Compiler Inserts gaps (or pads ) in structure to ensure correct alignment of fields 22

lignment (2) Size of primitive data type: 1 byte (e.g., char): No restrictions on address 2 bytes (e.g., short) lowest 1 bit of address must be 0 2 4 bytes (e.g., int, float, char *, etc) lowest 2 bits of address must be 00 2 8 bytes (e.g., 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 primitive 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 primitive data type) 23

lignment (3) 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 (under Windows): K = 8, due to double element c i[0] i[1] v struct S1 { char c; int i[2]; double v; } *p; p+0 p+4 p+8 p+16 p+24 Multiple of 4 Multiple of 8 Multiple of 8 Multiple of 8 24

lignment (4) Linux vs. Windows Windows (including Cygwin): K = 8 struct S1 { char c; int i[2]; double v; } *p; c i[0] i[1] v p+0 p+4 p+8 p+16 p+24 Multiple of 4 Multiple of 8 Multiple of 8 Multiple of 8 Linux: K = 4 c i[0] i[1] p+0 p+4 p+8 v p+12 p+20 Multiple of 4 Multiple of 4 Multiple of 4 Multiple of 4 25

lignment (5) Overall alignment requirement struct S2 { double x; int i[2]; char c; } *p; x p must be multiple of: 8 for Windows 4 for Linux i[0] i[1] p+0 p+8 p+12 p+16 Windows: p+24 struct S3 { float x[2]; Linux: p+20 int i[2]; char c; p must be multiple of 4 (all cases) } *p; c x[0] x[1] i[0] i[1] c p+0 p+4 p+8 p+12 p+16 p+20 26

lignment (6) Ordering elements within structure struct S4 { char c1; double v; char c2; int i; } *p; 10 bytes wasted space in Windows c1 v p+0 p+8 p+16 p+20 p+24 c2 i struct S5 { double v; char c1; char c2; int i; } *p; v c1 c2 p+0 p+8 p+12 p+16 2 bytes wasted space i 27

Union llocation Principles Overlay union elements llocate according to largest element Can only use one field at a time struct S1 { char c; int i[2]; double v; } *sp; union U1 { char c; int i[2]; double v; } *up; (Windows alignment) c i[0] i[1] v up+0 up+4 up+8 c i[0] i[1] v sp+0 sp+4 sp+8 sp+16 sp+24 28

Summary Structures llocate bytes in order declared Pad in middle and at end to satisfy alignment Unions Overlay declarations Way to circumvent type system 29