Presentation is loading. Please wait.

Presentation is loading. Please wait.

Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming IV: Data Slides adapted.

Similar presentations


Presentation on theme: "Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming IV: Data Slides adapted."— Presentation transcript:

1 Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming IV: Data Slides adapted from Bryant and O’Hallaron

2 Carnegie Mellon 2 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Today Arrays  One-dimensional  Multi-dimensional (nested)  Multi-level Structures  Allocation  Access  Alignment

3 Carnegie Mellon 3 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Array Allocation T A[ L ];  Contiguously allocated region of L * sizeof( T ) bytes in memory char str[12]; xx + 12 int val[5]; x x + 4x + 8x + 12x + 16x + 20 char *p[3]; x x + 8x + 16 x + 24 &str[0] &str[1] x+1 &val[0] &val[1] &val[2]&val[3]&val[4] &str[12] &val[5] &p[0] &p[1]&p[2]&p[3]

4 Carnegie Mellon 4 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Array Accessing Example Register %rdi contains starting address of array Register %rsi contains array index Desired digit at %rdi + 4*%rsi Use memory reference (%rdi,%rsi,4) int get_digit(int *z, int ind) { return z[ind]; } # %rdi = z # %rsi = ind movl (%rdi,%rsi,4), %eax # z[ind] IA32 #define LEN 5 int arr[LEN]; 15213 16 2024283236

5 Carnegie Mellon 5 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition # %rdi = z movl $0, %rax # i = 0 jmp.L3 # goto middle.L4: # loop: addl $1, (%rdi,%rax,4) # z[i]++ addq $1, %rax # i++.L3: # middle cmpq $4, %rax # i:4 jbe.L4 # if <=, goto loop rep; ret Array Loop Example void incr(int *z) { for (int i = 0; i < LEN; i++) z[i]++; }

6 Carnegie Mellon 6 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Multi-dimentional arrays “Row-Major” ordering of all elements in memory #define ROW 4 #define COL 5 int A[ROW][COL] = {{1, 5, 2, 0, 6}, {1, 5, 2, 1, 3 }, {1, 5, 2, 1, 7 }, {1, 5, 2, 2, 1 }}; xx+20x+40x+60x+80 152061521315217 152 2 1 x+(i*COL+j)*sizeof(int) &A[2][0]

7 Carnegie Mellon 7 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 2D Array Element Access int A[4][5] int get_digit(int i, int j) { return A[i][j]; } leaq(%rdi,%rdi,4), %rax # 5*i addl%rax, %rsi # 5*i+j movl0x890d0d(,%rsi,4), %eax# Memory[A + 4*(5*i+j)]

8 Carnegie Mellon 8 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Row vectors int *get_array(int i) { return A[i]; } # %rdi = i leaq (%rdi,%rdi,4),%rax # 5 * i leaq 0x80de(,%rax,4),%rax# A + (20 * i) 152061521315217152 2 1 A[1] A[0]: array of 5 ints A[2] A[3]

9 Carnegie Mellon 9 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Multi-Level Array Example int A[5] = { 1, 5, 2, 1, 3 }; int B[5] = { 0, 2, 1, 3, 9 }; int C[5] = { 9, 4, 7, 2, 0 }; int *aofa[3] = {B, A, C}; 36 160 16 56 168 176 aofa A B C 15213 16 2024283236 02139 4044485256 94720 6064687276

10 Carnegie Mellon 10 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Element Access in Multi-Level Array salq $2, %rsi # 4*j addq 0x8eaf(,%rdi,8), %rsi # p = a2a[i] + 4*j movl (%rsi), %eax # return *p ret int *aofa[3] = {A, B, C}; int get_digit(long i, long j) { return a2a[i][j]; } Mem[A+4*(5*i+j)] Mem[Mem[aofa+8*i]+4*j] How does this differ from accessing a 2D array?

11 Carnegie Mellon 11 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Today Arrays Structures  Allocation  Access  Alignment

12 Carnegie Mellon 12 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Structure Representation Fields ordered according to declaration Compiler determines overall size + positions of fields a r inext 0 16 2432 struct node { int a[4]; long i; struct node *next; };

13 Carnegie Mellon 13 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition.L11: # loop: movslq 16(%rdi), %rax # i = M[r+16] movl %esi, (%rdi,%rax,4) # M[r+4*i] = val movq 24(%rdi), %rdi # r = M[r+24] testq %rdi, %rdi # Test r jne.L11 # if !=0 goto loop void set_val (struct node *r, int val) { while (r) { int i = r->i; r->a[i] = val; r = r->next; } Following Linked List RegisterValue %rdir %rsival r inext 0 16 2432 a

14 Carnegie Mellon 14 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Structures & Alignment Unaligned Data Aligned Data  Primitive data type of x bytes  Address must be multiple of x  Alignment makes field access faster ci[0]i[1]v 3 bytes4 bytes p+0p+4p+8p+16p+24 Multiple of 4Multiple of 8 ci[0]i[1]v pp+1p+5p+9p+17 struct S1 { char c; int i[2]; double v; } *p; struct S1 { char c; int i[2]; double v; } *p;

15 Carnegie Mellon 15 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition struct S1 { char c; int i[2]; double v; } *p; struct S1 { char c; int i[2]; double v; } *p; Satisfying Alignment with Structures Within structure:  Must satisfy each element’s alignment requirement Initial address must also be aligned  K = Largest alignment of any element  Initial address & structure length must be multiples of K Example:  K = 8, due to double element ci[0]i[1]v 3 bytes4 bytes p+0p+4p+8p+16p+24 Multiple of 4Multiple of 8

16 Carnegie Mellon 16 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Saving Space Put large data types first Effect (K=4) struct S4 { char c; int i; char d; } *p; struct S4 { char c; int i; char d; } *p; struct S5 { int i; char c; char d; } *p; struct S5 { int i; char c; char d; } *p; ci 3 bytes d cid 2 bytes

17 Carnegie Mellon 17 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Summary Arrays  Elements packed into contiguous region of memory  Use index arithmetic to locate individual elements Structures  Elements packed into single region of memory  Access using offsets determined by compiler  Possible require internal and external padding to ensure alignment

18 Carnegie Mellon 18 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Understanding Pointers & Arrays #1 Cmp: Compiles (Y/N) Bad: Possible bad pointer reference (Y/N) Size: Value returned by sizeof Decl AnAn*An CmpBadSizeCmpBadSize int A1[3] int *A2

19 Carnegie Mellon 19 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Understanding Pointers & Arrays #1 Cmp: Compiles (Y/N) Bad: Possible bad pointer reference (Y/N) Size: Value returned by sizeof Decl AnAn*An CmpBadSizeCmpBadSize int A1[3] YN12YN4 int *A2 YN8YY4 A1 A2 Allocated int Unallocated pointer Allocated pointer Unallocated int

20 Carnegie Mellon 20 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Understanding Pointers & Arrays #2 Cmp: Compiles (Y/N) Bad: Possible bad pointer reference (Y/N) Size: Value returned by sizeof Decl AnAn*An**An CmpBadSizeCmpBadSizeCmpBadSize int A1[3] int *A2[3] int (*A3)[3]

21 Carnegie Mellon 21 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Understanding Pointers & Arrays #2 Decl AnAn*An**An CmpBadSizeCmpBadSizeCmpBadSize int A1[3] YN12YN4N-- int *A2[3] YN24YN8YY4 int (*A3)[3] YN8YY12YY4 A1 A2 Allocated int Unallocated pointer Allocated pointer Unallocated int A3

22 Carnegie Mellon 22 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Understanding Pointers & Arrays #3 Cmp: Compiles (Y/N) Bad: Possible bad pointer reference (Y/N) Size: Value returned by sizeof Decl AnAn*An**An Cm p BadSizeCm p BadSizeCm p BadSize int A1[3][5] int *A2[3][5] int (*A3)[3][5] int (*A4[3])[5] Decl ***An Cm p BadSize int A1[3][5] int *A2[3][5] int (*A3)[3][5] int (*A4[3])[5]

23 Carnegie Mellon 23 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Declaration int A1[3][5] int *A2[3][5] int (*A3)[3][5] int (*A4[3])[5] A2 A4 Allocated int Unallocated pointer Allocated pointer Unallocated int Allocated pointer to unallocated int A1 A3

24 Carnegie Mellon 24 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Understanding Pointers & Arrays #3 Cmp: Compiles (Y/N) Bad: Possible bad pointer reference (Y/N) Size: Value returned by sizeof Decl AnAn*An**An Cm p BadSizeCm p BadSizeCm p BadSize int A1[3][5] YN60YN20YN4 int *A2[3][5] YN120YN40YN8 int (*A3)[3][5] YN8YY60YY20 int (*A4[3])[5] YN24YN8YY20 Decl ***An Cm p BadSize int A1[3][5] N-- int *A2[3][5] YY4 int (*A3)[3][5] YY4 int (*A4[3])[5] YY4


Download ppt "Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming IV: Data Slides adapted."

Similar presentations


Ads by Google