1 Array. 2 Outline Aggregate scalar data into larger data Pointers vs. Array Array subscript and pointer arithmetic Memory layout for array –Static vs.

Slides:



Advertisements
Similar presentations
Code Optimization and Performance Chapter 5 CS 105 Tour of the Black Holes of Computing.
Advertisements

University of Washington Data Structures! Arrays  One-dimensional  Multi-dimensional (nested)  Multi-level Structs  Alignment Unions 1.
University of Washington Last Time For loops  for loop → while loop → do-while loop → goto version  for loop → while loop → goto “jump to middle” version.
Machine-Level Programming III: Procedures Apr. 17, 2006 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables CS213.
Machine-Level Programming IV: Structured Data Apr. 23, 2008 Topics Arrays Structures Unions EECS213.
Structured Data I: Homogenous Data Sept. 17, 1998 Topics Arrays –Single –Nested Pointers –Multilevel Arrays Optimized Array Code class08.ppt “The.
Arrays Chapter 8 page /24/07CS150 Introduction to Computer Science 1 Arrays (8.1)  One variable that can store a group of values of the same.
Lecture 22 Code Generation Topics Arrays Code Generation Readings: 9 April 10, 2006 CSCE 531 Compiler Construction.
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
Fabián E. Bustamante, Spring 2007 Machine-Level Programming III - Procedures Today IA32 stack discipline Register saving conventions Creating pointers.
Recitation 2 – 2/11/02 Outline Stacks & Procedures Homogenous Data –Arrays –Nested Arrays Mengzhi Wang Office Hours: Thursday.
Carnegie Mellon 1 Odds and Ends Intro to x86-64 Memory Layout.
Addressing Modes Chapter 6 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
1 Procedure Call and Array. 2 Outline Data manipulation Control structure Suggested reading –Chap 3.7, 3.8.
Carnegie Mellon 1 Machine-Level Programming I: Basics Lecture, Feb. 21, 2013 These slides are from website which accompanies the.
University of Amsterdam Computer Systems – Data in C Arnoud Visser 1 Computer Systems New to C?
University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.
Fabián E. Bustamante, Spring 2007 Machine-Level Prog. IV - Structured Data Today Arrays Structures Unions Next time Arrays.
Arrays and Pointers.
Assembly - Arrays תרגול 7 מערכים.
Arrays. Outline 1.(Introduction) Arrays An array is a contiguous block of list of data in memory. Each element of the list must be the same type and use.
Arrays. – 2 – Recall arrays char foo[80]; An array of 80 characters int bar[40]; An array of 40 integers.
Recitation 3 Outline Recursive procedure Complex data structures –Arrays –Structs –Unions Function pointer Reminders Lab 2: Wed. 11:59PM Lab 3: start early.
Arrays and Pointers (part 2) CSE 2031 Fall March 2016.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
Chapter 3 Machine-Level (2) Representation of Programs
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
IA32 Stack –Region of memory managed with stack discipline –Grows toward lower addresses –Register %esp indicates lowest stack address address of top element.
Reading Condition Codes (Cont.)
Machine-Level Programming IV: Data
Machine-Level Programming IV: Data
Conditional Branch Example
Introduction to Compilers Tim Teitelbaum
Arrays.
Chapter 3 Machine-Level Representation of Programs
Arrays and Pointers CSE 2031 Fall September 2018.
Machine-Level Programming IV: Structured Data Sept. 30, 2008
Heterogeneous Data Structures & Alignment
Referencing Examples Code Does Not Do Any Bounds Checking!
Machine-Level Programming IV: x86-64 Procedures, Data
Machine-Level Programming 5 Structured Data
Machine-Level Programming 4 Procedures
Arrays.
Machine-level Programming IV: Data Structures
Machine-Level Programming IV: Structured Data Sept. 19, 2002
Machine-Level Programming IV: Data
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Instructors: Majd Sakr and Khaled Harras
Machine-Level Programming III: Procedures Sept 18, 2001
Machine-Level Representation of Programs III
Machine-Level Programming IV: Data
Instructors: Majd Sakr and Khaled Harras
Machine Level Representation of Programs (III)
Machine-Level Programming VIII: Data Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017 Systems book chapter 3* * Modified slides.
Machine-Level Programming 5 Structured Data
Machine-Level Programming 5 Structured Data
Chapter 3 Machine-Level Representation of Programs
Machine-Level Programming IV: Structured Data
Arrays and Pointers (part 2)
Structured Data I: Homogenous Data Feb. 10, 2000
Instructor: Fatma CORUT ERGİN
Arrays and Pointers (part 2)
Machine-Level Programming VIII: Data Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017 Systems book chapter 3* * Modified slides.
Machine-Level Programming VIII: Data Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017 Systems book chapter 3* * Modified slides.
Machine-Level Programming IV: Structured Data
Machine-Level Programming IV: Structured Data
Presentation transcript:

1 Array

2 Outline Aggregate scalar data into larger data Pointers vs. Array Array subscript and pointer arithmetic Memory layout for array –Static vs. dynamic Compiler optimization for array operations Suggested reading –3.8

3 Array declaration T A[N] ; Allocate a contiguous region in memory –The size of the region is sizeof(T) * N bytes

4 Starting Address of an Array The starting address of an array A –is denoted as X A Identifier A can be used as –A pointer to the beginning of the array –Its value is X A

5 Accessing Array Array elements can be accessed –Using an integer index ranging between 0 and N-1 Array element i is stored at address –X A + sizeof(T) * i

6 Array char a[12] ; xaxa x a +4x a +8 char *b[5] ; xbxb x b +4x b +8x b +12x b +16

7 Array double c[2] ; xcxc x c +8 double *d[5] ; xdxd x d +4x d +8x d +12x d +16

8 Pointer Arithmetic Addition and subtraction –p+i, p-i (result is a pointer) –p-q (result is an int) Referencing & dereferencing –*p, &E Subscription –A[i], *(A+i)

9 Memory referencing instruction E is an array of int’s –Address of E is stored in register %edx –Index i is stored in register %ecx The array element E[i] is translated into – movl(%edx, %ecx, 4), %eax

10 Pointer Arithmetic ExpressionTypeValueAssembly code E int * xE xE movl %edx, %eax E[0] intM[x E ]movl (%edx), %eax E[i] intM[x E +4i]movl (%edx, %ecx, 4), %eax &E[2] int *x E +8leal 8(%edx,) %eax E+i-1 int *x E +4i-4lea -4(%edx, %ecx, 4), %eax *(&E[i]+i) intM[x E +4i+4i]movl (%edx, %ecx, 8), %eax &E[i]-E intimovl %ecx, %eax

11 Example 1 int decimal5(int *x) 2 { 3 int i; 4 int val = 0; 5 6 for (i = 0; i < 5; i++) 7 val = (10 * val) + x[i]; 8 9 return val; 10 }

12 Example xorl%edx, %edx# i = 0 xorl%eax, %eax# val = 0 movl8(%ebp), %ecx# get x L2: leal(%eax,%eax,4), %eax addl%eax, %eax# val *= 10 addl(%ecx,%edx,4), %eax# val += x[i] addl$1, %edx# i++ cmpl$5, %edx# i ? 5 jneL2

13 Nested Array int A[4][3] ; –Array A is a two-dimensional array with four rows and three columns –It is referenced as A[0][0] through A[3][2]

14 Nested Array int A[4][3] ; Array of array –typedef int row3_t[3] ; – row3_t A[4] ; –Array A contains 4 elements, each requiring 12 bytes to store 3 integers –The whole size of array A is 48 bytes Row major ordered in memory

15 Nested Array RowElementAddress A[0]A[0][0]xAxA A[0][1]x A +4 A[0][2]x A +8 A[1]A[1][0]x A +12 A[1][1]x A +16 A[1][2]x A +20 A[2]A[2][0]x A +24 A[2][1]x A +28 A[2][2]x A +32 A[3]A[3][0]x A +36 A[3][1]x A +40 A[3][2]x A +44

16 Nested Array T D[R][C] ; D[i][j] is at memory address –x D + L* ( C * i + j ) –L is sizeof(T)

17 Access A[i,j] It is in memory M [ xA +j*4+ i * 12 ] %eax contains x A %edx holds i, %ecx holds j sall $2, %ecx j*4 leal (%edx, %edx, 2), %edx i*3 leal (%ecx, %edx, 4), %ecx j*4+ i*12 movl (%eax, %ecx), %eax

18 Fixed-size Arrays 1 #define N 16 2 typedef int fix_matrix[N][N]; 3 4 /* Compute i,k of fixed matrix product */ 5 int fix_prod_ele (fix_matrix A, fix_matrix B, int i, int k) 6 { 7 int j; 8 int result = 0; 9 10 for (j = 0; j < N; j++) 11 result += A[i][j] * B[j][k]; return result; 14 }

19 Observations The loop will access the elements of array A –as A[i][0], A[i][1], …, A[i][15] in sequence These elements occupy adjacent positions in memory –starting with the address of array A[i][0] Use a pointer variable Aptr –to access these successive locations.

20 Observations The loop will access the elements of array B –as B[0][k], B[1][k], …, B[15][k] in sequence These elements occupy positions in memory –starting with the address of array B[0][i] –and space 64 bytes apart. Use a pointer variable Bptr –to access these locations. Use a simple counter –to keep track of the number of iterations required

21 Fixed-size Arrays 1 /* Compute i,k of fixed matrix product */ 2 int fix_prod_ele_opt (fix_matrix A, fix_matrix B, int i, int k) 3 { 4 int *Arow=&A[i][0], *Bptr = &B[0][k]; 5 int j, result = 0; 6 7 For ( cnt = 0; cnt < 16; cnt++) { 8 result += Arow[j] * (*Bptr); 9 Bptr += N; j++; 10} return result; 13 }

22 Fixed-size Arrays Aptr is in %edx, Bptr in %ecx, result in %esi, cnt in %ebx 1.L6: loop: 2 movl (%ecx),%eax Get *Bptr 3 imull (%esi, %edx, 4),%eax Multiply by Arow[j] 4 addl %eax,%ebx Add to result 5 addl $1, %edxj++ 6 addl $64,%ecx Add 64 to Bptr 7 cmpl $16,%edxcompare j : 16 8 jne.L6 if !=, goto loop

23 Variable-Size Arrays 1 int var_ele (int n, int A[n][n], int i, int j) 2 { 3 return A[i][j]; 4 } Declare an array int A[exp1][exp2] –either as a local variable –or as an argument to a function The dimensions of the array are determined –by evaluating the expressions at the time the declaration is encounterd

24 Optimization 1 /* Compute i,k of variable matrix product */ 2 int var_prod_ele (int n, int A[n][n], int B[n][n], int i, int k) { 3 int j; 4 int result = 0; 5 6 for (j = 0; j < n; j++) 7 result += A[i][j] * B[j][k]; 8 9 return result; 10 }

25 Assembly Code n at %ebp+8, A at %ebp+12, i at %ebp+16, j at %ebp+20 1.movl 8(%ebp), %eax Get n 2.sall $2, %eax Compute 4*n 3.movl %eax, %edx Copy 4*n 4.imull 16(%ebp), %edx Compute 4*n*i 5.movl 20(%ebp), %eax Get j 6.sall $2, %eax Compute 4*j 7.addl 12(%ebp), %eax Compute xA+ 4 ∗ j 8.movl (%eax,%edx), %eax Read from xA+ 4 ∗ (n ∗ i + j)

26 Assembly Code n stored at %ebp+8 Registers: Arow in %esi, Bptr in %ecx, j in %edx, result in %ebx, %edi holds 4*n 1.L30: loop: 2 movl(%ecx), %eax Get *Bptr 3 imull (%esi,%edx,4), %eax Multiply by Arow[j] 4 addl %eax, %ebx Add to result 5 addl $1, %edx Increment j 6 addl %edi, %ecx Add 4*n to Bptr 7 cmpl %edx, 8(%ebp) Compare n:j 8 jg.L30 If >, goto loop

27 Example static char daytab[2][13] = { {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} } /* day_of_year: set day of year from month & day */ int day_of_year(int year, int month, int day) { int i, leap; leap = year%4 == 0 && year%100 != 0 && year%400 ==0; for (i = 1; i < month; i++) day += daytab[leap][i]; return day; }

28 Example /* month_day: set month, day from day of year */ int month_day(int year, int yearday, int *pmonth, int *pday) { int i, leap; leap = year%4 == 0 && year%100 != 0 && year%400 ==0; for (i = 1; yearday > daytab[leap][i] ; i++) yearday -= daytab[leap][i]; *pmonth = i; *pday = yearday; }

29 Pointer Array /* month_name: return name of n-th month */ int month_name(int n) { static char *name[] = { “Illegal month”, “January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December” }; return (n 12) ? name[0] : name[n]; }

30 Pointer Array Illegal month\0 January\0 February\0 March\0 April\0 May\0 June\0 July\0 August\0 September\0 October\0 November\0 December\0

Command-line Arguments $ echo hello, world $ hello, world #include /* echo command-line arguments */ main(int argc, char *argv[]) { int i ; for ( i = 1; i < argc ; i++) printf(“%s%s”, argv[i], (i < argc-1) ? “ ” : “”); printf(“\n”) ; return 0; } 0 echo\0 Hello,\0 World\0 argv:

Command-line Arguments #include /* echo command-line arguments ; 2 nd version*/ main(int argc, char *argv[]) { while ( --argc > 0 ) printf(“%s%s”, ++argv, (argc > 1) ? “ ” : “”); printf(“\n”) ; return 0; } printf((argc > 1) ? “%s ” : “%s”);

Pointer to Function #include /* numcmp: compare s1 and s2 numerically */ int numcmp(char *s1, char *s2) { double v1, v2; v1 = atof(s1); v2 = atof(s2); if (v1 < v2) return -1; else if ( v1 > v2 )return 1; elsereturn 0; }

Pointer to Function #include { …… int numeric = 0, (*cmp)(void *, void *); char *s1, *s2; …… if (…) numeric = 1 ; …… cmp = ( int (*)(void *, void *)) (numeric ? numcmp : strcmp); (*cmp)(s1, s2); …… }