Arrays. CSE 351 Autumn Instructor: Justin Hsia

Similar documents
Arrays. CSE 351 Autumn Instructor: Justin Hsia

Executables & Arrays. CSE 351 Autumn Instructor: Justin Hsia

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

Building an Executable

Structs and Alignment

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

Assembly IV: Complex Data Types

Data III & Integers I

Structs & Alignment. CSE 351 Autumn Instructor: Justin Hsia

The Hardware/So=ware Interface CSE351 Winter 2013

Machine-level Programs Data

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

Structs and Alignment CSE 351 Spring

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

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

Structs and Alignment

x86 Programming I CSE 351 Autumn 2016 Instructor: Justin Hsia

x86-64 Programming III & The Stack

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

Systems I. Machine-Level Programming VII: Structured Data

Computer Organization: A Programmer's Perspective

Cache Example, System Control Flow

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

Machine Level Programming: Arrays, Structures and More

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

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

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

CS429: Computer Organization and Architecture

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

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

Machine-Level Prog. IV - Structured Data

Assembly Programming IV

University of Washington

Data III & Integers I

Assembly Programming IV

Stack Frame Components. Using the Stack (4) Stack Structure. Updated Stack Structure. Caller Frame Arguments 7+ Return Addr Old %rbp

Java and C. CSE 351 Autumn 2018

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

Memory, Data, & Addressing I

x86 Programming III CSE 351 Autumn 2016 Instructor: Justin Hsia

Data Structures in Memory!

Machine- Level Representation: Data

Compiling C Programs Into X86-64 Assembly Programs

Data III & Integers I

x86 Programming I CSE 351 Winter

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

Java and C I. CSE 351 Spring Instructor: Ruth Anderson

x86-64 Programming III

The Stack & Procedures

Memory Allocation II. CSE 351 Autumn Instructor: Justin Hsia

CSE351: Memory, Data, & Addressing I

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

The Stack & Procedures

x86 64 Programming II

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Machine Programming 4: Structured Data

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

Machine Program: Procedure. Zhaoguo Wang

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.

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls

Referencing Examples

Virtual Memory I. CSE 351 Spring Instructor: Ruth Anderson

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

Java and C CSE 351 Spring

Java and C II. CSE 351 Spring Instructor: Ruth Anderson

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

Giving credit where credit is due

Giving credit where credit is due

University*of*Washington*

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

L08: Assembly Programming II. Assembly Programming II. CSE 351 Spring Guest Lecturer: Justin Hsia. Instructor: Ruth Anderson

L09: Assembly Programming III. Assembly Programming III. CSE 351 Spring Guest Lecturer: Justin Hsia. Instructor: Ruth Anderson

Hardware: Logical View

We made it! Java: Assembly language: OS: Machine code: Computer system:

CS241 Computer Organization Spring Loops & Arrays

Machine/Assembler Language Putting It All Together

Memory Allocation III

Machine Programming. CSE 351 Autumn Instructor: Justin Hsia

Virtual Memory II. CSE 351 Autumn Instructor: Justin Hsia

x86-64 Programming I CSE 351 Summer 2018 Instructor: Justin Hsia Teaching Assistants: Josie Lee Natalie Andreeva Teagan Horkan

Cache Wrap Up, System Control Flow

The Hardware/Software Interface CSE351 Spring 2015

Memory Organization and Addressing

x86-64 Assembly CSE 351 Winter 2018 Instructor: Mark Wyse

Floating Point II, x86 64 Intro

Assembly Programming I

Question Points Score Total: 100

Computer Systems CEN591(502) Fall 2011

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Virtual Memory I. CSE 351 Winter Instructor: Mark Wyse

Assembly Language: Function Calls

x86 64 Programming I CSE 351 Autumn 2017 Instructor: Justin Hsia

SYSTEMS PROGRAMMING AND COMPUTER ARCHITECTURE Assignment 5: Assembly and C

Data Representa/ons: IA32 + x86-64

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

Do not turn the page until 5:10.

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

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

Caches II. CSE 351 Autumn Instructor: Justin Hsia

Transcription:

rrays CSE 351 utumn 2016 Instructor: Justin Hsia Teaching ssistants: Chris Ma Hunter Zahn John Kaltenbach Kevin Bi Sachin Mehta Suraj Bhat Thomas Neuman Waylon Huang Xi Liu Yufang Sun http://xkcd.com/1513/

dministrivia Lab 2 due Friday Homework 2 released on Friday Midterm on Nov. 2 in lecture Make a cheat sheet! two sided letter page, handwritten Midterm details Piazza post: @225 Past Num Rep and Floating Point questions posted (solutions tonight) Midterm review session 5 7pm on Monday, Oct. 31 in EEB 105 2

Roadmap C: car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); ssembly language: Machine code: Computer system: get_mpg: pushq %rbp movq %rsp, %rbp... popq %rbp ret 0111010000011000 100011010000010000000010 1000100111000010 110000011111101000011111 Java: Car c = new Car(); c.setmiles(100); c.setgals(17); float mpg = c.getmpg(); OS: Memory & data Integers & floats Machine code & C x86 assembly Procedures & stacks rrays & structs Memory & caches Processes Virtual memory Memory allocation Java vs. C 3

Data Structures in ssembly rrays One dimensional Multi dimensional (nested) Multi level Structs lignment Unions lso: Some C details and how they relate to Java and assembly C arrays are convenient but with some unique/strange rules 4

rray llocation Basic Principle T [N]; array of data type T and length N Contiguously allocated region of N*sizeof(T) bytes Identifier returns address of array (type T*) char msg[12]; int val[5]; double a[3]; x x + 12 x x + 4 x + 8 x + 12 x + 16 x + 20 char* p[3]; (or char *p[3];) x x + 8 x + 16 x + 24 x x + 8 x + 16 x + 24 5

rray ccess Basic Principle T [N]; array of data type T and length N Identifier returns address of array (type T*) int x[5]; 3 7 1 9 5 Reference Type Value a a+4 a+8 a+12 a+16 a+20 x[4] int 5 x int * a x+1 int * a + 4 &x[2] int * a + 8 x[5] int?? (whatever s in memory at addr x+20) *(x+1) int 7 x+i int * a + 4*i 6

rray Example typedef int zip_dig[5]; zip_dig cmu = { 1, 5, 2, 1, 3 }; zip_dig uw = { 9, 8, 1, 9, 5 }; zip_dig ucb = { 9, 4, 7, 2, 0 }; initialization int uw[5] 7

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

rray ccessing Example typedef int zip_dig[5]; zip_dig uw; 9 8 1 9 5 36 40 44 48 52 56 int get_digit(zip_dig z, int digit) { return z[digit]; } get_digit: movl (%rdi,%rsi,4), %eax # z[digit] Register %rdi contains starting address of array Register %rsi contains array index Desired digit at %rdi+4*%rsi, so use memory reference (%rdi,%rsi,4) 9

Referencing Examples typedef int zip_dig[5]; zip_dig cmu; 1 5 2 1 3 16 20 24 28 32 36 zip_dig uw; 9 8 1 9 5 36 40 44 48 52 56 zip_dig ucb; 9 4 7 2 0 56 60 64 68 72 76 Reference ddress Value Guaranteed? uw[3] uw[6] uw[-1] cmu[15] 10

Referencing Examples typedef int zip_dig[5]; zip_dig cmu; 1 5 2 1 3 16 20 24 28 32 36 zip_dig uw; 9 8 1 9 5 36 40 44 48 52 56 zip_dig ucb; 9 4 7 2 0 56 60 64 68 72 76 Reference ddress Value Guaranteed? uw[3] uw[6] uw[-1] cmu[15] No bounds checking 36 + 4* 3 = 48 9 Yes 36 + 4* 6 = 60 4 No 36 + 4*-1 = 32 3 No 16 + 4*15 = 76?? No Example arrays happened to be allocated in successive 20 byte blocks Not guaranteed to happen in general 11

rray Loop Example zi = 10*0 + 9 = 9 zi = 10*9 + 8 = 98 zi = 10*98 + 1 = 981 typedef int zip_dig[5]; int zd2int(zip_dig z) { int i; int zi = 0; for (i = 0; i < 5; i++) { zi = 10 * zi + z[i]; } return zi; } zi = 10*981 + 9 = 9819 zi = 10*9819 + 5 = 98195 9 8 1 9 5 12

rray Loop Example Original: Transformed: Eliminate loop variable i, use pointer zend instead Convert array code to pointer code Pointer arithmetic on z Express in do while form (no test at entrance) L01: Intro, L01: L13: Combinational Introduction rrays Logic CSE369, CSE351, utumn 2016 zip_dig uw; 9 8 1 9 5 int zd2int(zip_dig z) { int zi = 0; int *zend = z + 5; do { zi = 10 * zi + *z; } 36 40 44 48 52 56 int zd2int(zip_dig z) { int i; int zi = 0; for (i = 0; i < 5; i++) { zi = 10 * zi + z[i]; } return zi; } z++; } while (z < zend); return zi; address just past 5 th digit Increments by 4 (size of int) 13

rray Loop Implementation Registers: %rdi %rax %rcx z zi zend Computations 10*zi + *z implemented as: *z + 2*(5*zi) z++ increments by 4 (size of int) L01: Intro, L01: L13: Combinational Introduction rrays Logic CSE369, CSE351, utumn 2016 int zd2int(zip_dig z) { int zi = 0; int *zend = z + 5; do { zi = 10 * zi + *z; z++; } while (z < zend); return zi; } gcc with O1 # %ecx = z leaq 20(%rdi),%rcx # rcx = zend = z+5 movl $0,%eax # rax = zi = 0.L17: leal (%rax,%rax,4),%edx # zi + 4*zi = 5*zi movl (%rdi),%eax # eax = *z leal (%rax,%rdx,2),%eax # zi = *z + 2*(5*zi) addq $4,%rdi # z++ cmpq %rdi,%rcx # zend : z jne.l17 # if!= goto loop 14

[[Long Code Demo]] See arrays_in_c.c for warning free code that demonstrates: rrays created on the Heap and on the Stack Reminder of relative positions of Memory sections typedef Macro substitution (#define) Implicit pass by reference of arrays in C sizeof() with arrays 15

Code Demo Takeaways rray variables don t have addresses return address of array Pointers actually have addresses rrays can be created on Stack, Heap, or Static Data Stack (local var) is temporary Heap (malloc) is more permanent, but requires memory management Static Data (global var) can have dangerously broad scope Not automatically initialized! Passing an array to a procedure actually passes a pointer to the array 16

Nested rray Example zip_dig sea[4] = {{ 9, 8, 1, 9, 5 }, { 9, 8, 1, 0, 5 }, { 9, 8, 1, 0, 3 }, { 9, 8, 1, 1, 5 }}; typedef int zip_dig[5]; Remember, T [N] is an array with elements of type T, with length N same as: int sea[4][5]; What is the layout in memory? 17

Nested rray Example zip_dig sea[4] = {{ 9, 8, 1, 9, 5 }, { 9, 8, 1, 0, 5 }, { 9, 8, 1, 0, 3 }, { 9, 8, 1, 1, 5 }}; typedef int zip_dig[5]; Remember, T [N] is an array with elements of type T, with length N sea[3][2]; Row 0 Row 1 Row 2 Row 3 9 8 1 9 5 9 8 1 0 5 9 8 1 0 3 9 8 1 1 5 76 96 116 136 156 Row major ordering of all elements Elements in the same row are contiguous Guaranteed (in C) 18

Two Dimensional (Nested) rrays Declaration: T [R][C]; 2D array of data type T R rows, C columns Each element requires sizeof(t) bytes rray size? [0][0] [R-1][0] [0][C-1] [R-1][C-1] 19

Two Dimensional (Nested) rrays Declaration: T [R][C]; 2D array of data type T R rows, C columns Each element requires sizeof(t) bytes rray size: R*C*sizeof(T) bytes rrangement: row major ordering int [R][C]; [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 20

Nested rray Row ccess Row vectors Given T [R][C], [i] is an array of C elements ( row i ) Each element of type T requires K bytes is address of array Starting address of row i = + 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 21

Nested rray Row ccess Code int* get_sea_zip(int index) { return sea[index]; } What data type is sea[index]? What is its starting address? int sea[4][5] = {{ 9, 8, 1, 9, 5 }, { 9, 8, 1, 0, 5 }, { 9, 8, 1, 0, 3 }, { 9, 8, 1, 1, 5 }}; get_sea_zip(int): movslq %edi, %rdi leaq (%rdi,%rdi,4), %rdx leaq 0(,%rdx,4), %rax addq $sea, %rax ret sea:.long 9.long 8.long 1.long 9.long 5.long 9.long 8... 22

Nested rray Row ccess Code int* get_sea_zip(int index) { return sea[index]; } int sea[4][5] = {{ 9, 8, 1, 9, 5 }, { 9, 8, 1, 0, 5 }, { 9, 8, 1, 0, 3 }, { 9, 8, 1, 1, 5 }}; What data type is sea[index]? What is its starting address? # %rdi = index leaq (%rdi,%rdi,4),%rax leaq sea(,%rax,4),%rax Translation? # 5 * index # sea + (20 * index) 23

Nested rray Row ccess Code int* get_sea_zip(int index) { return sea[index]; } int sea[4][5] = {{ 9, 8, 1, 9, 5 }, { 9, 8, 1, 0, 5 }, { 9, 8, 1, 0, 3 }, { 9, 8, 1, 1, 5 }}; # %rdi = index leaq (%rdi,%rdi,4),%rax leaq sea(,%rax,4),%rax # 5 * index # sea + (20 * index) Row Vector sea[index] is array of 5 ints Starting address = sea+20*index ssembly Code Computes and returns address Compute as: sea+4*(index+4*index)= sea+20*index 24

Nested rray Element ccess int [R][C]; [0] [i] [R-1] [0] [0] [0] [C-1] [i] [j] [R-1] [0] [R-1] [C-1] + i*c*4 + (R-1)*C*4? 25

Nested rray Element ccess rray Elements [i][j] is element of type T, which requires K bytes ddress of [i][j] is + i*(c*k) + j*k == + (i*c + j)*k int [R][C]; [0] [i] [R-1] [0] [0] [0] [C-1] [i] [j] [R-1] [0] [R-1] [C-1] + i*c*4 + (R-1)*C*4 + i*c*4 + j*4 26

Nested rray Element ccess Code int* get_sea_digit (int index, int digit) { return sea[index][digit]; } int sea[4][5] = {{ 9, 8, 1, 9, 5 }, { 9, 8, 1, 0, 5 }, { 9, 8, 1, 0, 3 }, { 9, 8, 1, 1, 5 }}; leaq (%rdi,%rdi,4), %rax # 5*index addl %rax, %rsi # 5*index+digit movl sea(,%rsi,4), %eax # *(sea + 4*(5*index+digit)) rray Elements sea[index][digit] is an int (sizeof(int)=4) ddress = sea + 5*4*index + 4*digit ssembly Code Computes address as: sea + ((index+4*index) + digit)*4 movl performs memory reference 27

Strange Referencing Examples L01: Intro, L01: L13: Combinational Introduction rrays Logic CSE369, CSE351, utumn 2016 typedef int zip_dig[5]; zip_dig sea[4]; 9 8 1 9 5 9 8 1 0 5 9 8 1 0 3 9 8 1 1 5 76 96 116 136 156 Reference ddress Value Guaranteed? sea[3][3] sea[2][5] sea[2][-1] sea[4][-1] sea[0][19] sea[0][-1] Code does not do any bounds checking Ordering of elements within array guaranteed 28

Strange Referencing Examples L01: Intro, L01: L13: Combinational Introduction rrays Logic CSE369, CSE351, utumn 2016 typedef int zip_dig[5]; zip_dig sea[4]; 9 8 1 9 5 9 8 1 0 5 9 8 1 0 3 9 8 1 1 5 76 96 116 136 156 Reference ddress Value Guaranteed? sea[3][3] sea[2][5] sea[2][-1] sea[4][-1] sea[0][19] sea[0][-1] 76+20*3+4*3 = 148 1 Yes 76+20*2+4*5 = 136 9 Yes 76+20*2+4*-1 = 112 5 Yes 76+20*4+4*-1 = 152 5 Yes 76+20*0+4*19 = 152 5 Yes 76+20*0+4*-1 = 72?? No Code does not do any bounds checking Ordering of elements within array guaranteed 29

Multi Level rray Example Multi Level rray Declaration(s): int cmu[5] = { 1, 5, 2, 1, 3 }; int uw[5] = { 9, 8, 1, 9, 5 }; int ucb[5] = { 9, 4, 7, 2, 0 }; int* univ[3] = {uw, cmu, ucb}; 2D rray Declaration: zip_dig univ2d[3] = { { 9, 8, 1, 9, 5 }, { 1, 5, 2, 1, 3 }, { 9, 4, 7, 2, 0 } }; Is a multi level array the same thing as a 2D array? NO One array declaration = one contiguous block of memory 30

Multi Level rray Example int cmu[5] = { 1, 5, 2, 1, 3 }; int uw[5] = { 9, 8, 1, 9, 5 }; int ucb[5] = { 9, 4, 7, 2, 0 }; int* univ[3] = {uw, cmu, ucb}; 160 168 176 univ 36 16 60 cmu uw ucb Variable univ denotes array of 3 elements Each element is a pointer 8 bytes each Each pointer points to array of ints 1 5 2 1 3 16 20 24 28 32 36 9 8 1 9 5 36 40 44 48 52 56 9 4 7 2 0 60 64 68 72 76 80 Note: this is how Java represents multi dimensional arrays 31

Element ccess in Multi Level rray int get_univ_digit (int index, int digit) { return univ[index][digit]; } 160 168 176 univ 36 16 60 cmu uw ucb 1 5 2 1 3 16 20 24 28 32 36 9 8 1 9 5 36 40 44 48 52 56 9 4 7 2 0 60 64 68 72 76 80 salq $2, %rsi # rsi = 4*digit addq univ(,%rdi,8), %rsi # p = univ[index] + 4*digit movl (%rsi), %eax # return *p ret Computation Element access Mem[Mem[univ+8*index]+4*digit] Must do two memory reads First get pointer to row array Then access element within array But allows inner arrays to be different lengths (not in this example) 32

rray Element ccesses Nested array int get_sea_digit (int index, int digit) { return sea[index][digit]; } Multi level array int get_univ_digit (int index, int digit) { return univ[index][digit]; } 160 168 176 univ 36 16 60 cmu uw ucb 1 5 2 1 3 16 20 24 28 32 36 9 8 1 9 5 36 40 44 48 52 56 9 4 7 2 0 60 64 68 72 76 80 ccess looks the same, but it isn t: Mem[sea+20*index+4*digit] Mem[Mem[univ+8*index]+4*digit] 33

Strange Referencing Examples 160 168 176 univ 36 16 60 cmu uw ucb 1 5 2 1 3 16 20 24 28 32 36 9 8 1 9 5 36 40 44 48 52 56 9 4 7 2 0 60 64 68 72 76 80 Reference ddress Value Guaranteed? univ[2][3] univ[1][5] univ[2][-2] univ[3][-1] univ[1][12] C Code does not do any bounds checking Location of each lower level array in memory is not guaranteed 34

Strange Referencing Examples cmu 1 5 2 1 3 univ 16 20 24 28 32 36 160 36 uw 9 8 1 9 5 168 16 176 60 ucb 36 40 44 48 52 56 9 4 7 2 0 60 64 68 72 76 80 Reference ddress Value Guaranteed? univ[2][3] univ[1][5] univ[2][-2] univ[3][-1] univ[1][12] 60+4*3 = 72 16+4*5 = 36 60+4*-2 = 52 #@%!^?? 16+4*12 = 64 2 9 5?? 4 Yes No No No No C Code does not do any bounds checking Location of each lower level array in memory is not guaranteed 35

Summary Contiguous allocations of memory No bounds checking (and no default initialization) Can usually be treated like a pointer to first element int a[4][5]; array of arrays all levels in one contiguous block of memory int* b[4]; array of pointers to arrays First level in one contiguous block of memory Each element in the first level points to another sub array Parts anywhere in memory 36