machine level programming iv data 15 213 introduc on to
play

Machine-Level Programming IV: Data 15-213: Introduc;on to - PowerPoint PPT Presentation

Carnegie Mellon Machine-Level Programming IV: Data 15-213: Introduc;on to Computer Systems 8 th Lecture, Sep. 24, 2015 Instructors: Randal E. Bryant and


  1. Carnegie Mellon Machine-­‑Level ¡Programming ¡IV: ¡ Data ¡ ¡ 15-­‑213: ¡Introduc;on ¡to ¡Computer ¡Systems ¡ 8 th ¡Lecture, ¡Sep. ¡24, ¡2015 ¡ Instructors: ¡ ¡ Randal ¡E. ¡Bryant ¡and ¡David ¡R. ¡O’Hallaron ¡ 1 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  2. Carnegie Mellon Today ¡ ¢ Arrays ¡ § One-­‑dimensional ¡ § Mul;-­‑dimensional ¡(nested) ¡ § Mul;-­‑level ¡ ¢ Structures ¡ § Alloca;on ¡ § Access ¡ § Alignment ¡ ¢ Floa?ng ¡Point ¡ 2 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  3. Carnegie Mellon Array ¡Alloca?on ¡ ¢ Basic ¡Principle ¡ T ¡ ¡ A[ L ]; ¡ § Array ¡of ¡data ¡type ¡ T ¡and ¡length ¡ L ¡ § Con;guously ¡allocated ¡region ¡of ¡ L ¡* ¡ sizeof ( T ) ¡bytes ¡in ¡memory ¡ char string[12]; x ¡ x ¡ + ¡12 ¡ int val[5]; x ¡ x ¡ + ¡4 ¡ x ¡ + ¡8 ¡ x ¡ + ¡12 ¡ x ¡ + ¡16 ¡ x ¡ + ¡20 ¡ double a[3]; x ¡ x ¡ + ¡8 ¡ x ¡ + ¡16 ¡ x ¡ + ¡24 ¡ char *p[3]; x ¡ x ¡ + ¡8 ¡ x ¡ + ¡16 ¡ x ¡ + ¡24 ¡ 3 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  4. Carnegie Mellon Array ¡Access ¡ ¢ Basic ¡Principle ¡ T ¡ ¡ A[ L ]; ¡ § Array ¡of ¡data ¡type ¡ T ¡and ¡length ¡ L ¡ § Iden;fier ¡ A ¡can ¡be ¡used ¡as ¡a ¡pointer ¡to ¡array ¡element ¡0: ¡Type ¡ T* ¡ 1 ¡ 5 ¡ 2 ¡ 1 ¡ 3 ¡ int val[5]; x ¡ x ¡ + ¡4 ¡ x ¡ + ¡8 ¡ x ¡ + ¡12 ¡ x ¡ + ¡16 ¡ x ¡ + ¡20 ¡ ¡ ¢ Reference ¡Type ¡Value ¡ 3 ¡ val[4] int x ¡ val int * ¡ int * x ¡+ ¡4 ¡ ¡ ¡ ¡ ¡ val+1 ¡ int * x ¡+ ¡8 ¡ ¡ ¡ ¡ &val[2] ¡ int ?? ¡ val[5] ¡ int 5 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ *(val+1) val + i ¡ int * x ¡ + ¡4 ¡i ¡ 4 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  5. Carnegie Mellon Array ¡Example ¡ #define ZLEN 5 typedef int zip_dig[ZLEN]; zip_dig cmu = { 1, 5, 2, 1, 3 }; zip_dig mit = { 0, 2, 1, 3, 9 }; zip_dig ucb = { 9, 4, 7, 2, 0 }; 1 ¡ 5 ¡ 2 ¡ 1 ¡ 3 ¡ zip_dig cmu; 16 ¡ 20 ¡ 24 ¡ 28 ¡ 32 ¡ 36 ¡ 0 ¡ 2 ¡ 1 ¡ 3 ¡ 9 ¡ zip_dig mit; 36 ¡ 40 ¡ 44 ¡ 48 ¡ 52 ¡ 56 ¡ 9 ¡ 4 ¡ 7 ¡ 2 ¡ 0 ¡ zip_dig ucb; 56 ¡ 60 ¡ 64 ¡ 68 ¡ 72 ¡ 76 ¡ ¢ Declara?on ¡“ zip_dig cmu ” ¡equivalent ¡to ¡“ int cmu[5] ” ¡ ¢ Example ¡arrays ¡were ¡allocated ¡in ¡successive ¡20 ¡byte ¡blocks ¡ § Not ¡guaranteed ¡to ¡happen ¡in ¡general ¡ 5 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  6. Carnegie Mellon Array ¡Accessing ¡Example ¡ 1 ¡ 5 ¡ 2 ¡ 1 ¡ 3 ¡ zip_dig cmu; 16 ¡ 20 ¡ 24 ¡ 28 ¡ 32 ¡ 36 ¡ int get_digit (zip_dig z, int digit) { return z[digit]; n Register ¡ %rdi ¡contains ¡ } star?ng ¡address ¡of ¡array ¡ IA32 ¡ n Register ¡ %rsi ¡contains ¡ ¡ array ¡index ¡ # %rdi = z # %rsi = digit n Desired ¡digit ¡at ¡ ¡ movl (%rdi,%rsi,4), %eax # z[digit] %rdi + 4*%rsi ¡ n Use ¡memory ¡reference ¡ (%rdi,%rsi,4) ¡ 6 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  7. Carnegie Mellon Array ¡Loop ¡Example ¡ void zincr(zip_dig z) { size_t i; for (i = 0; i < ZLEN; i++) z[i]++; } # %rdi = z movl $0, %eax # 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 7 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  8. Carnegie Mellon Mul?dimensional ¡(Nested) ¡Arrays ¡ ¢ Declara?on ¡ A[0][0] • • • A[0][C-1] T ¡ ¡ ¡ A [ R ][ C ]; ¡ • • § 2D ¡array ¡of ¡data ¡type ¡ T ¡ • • § R ¡rows, ¡ C ¡columns ¡ • • § Type ¡ T ¡element ¡requires ¡ K ¡bytes ¡ A[R-1][0] • • • A[R-1][C-1] ¢ Array ¡Size ¡ § R ¡* ¡ C ¡ * ¡ K ¡ bytes ¡ ¢ Arrangement ¡ § Row-­‑Major ¡Ordering ¡ int A[R][C]; A A A A A A [0] [0] [1] [1] [R-1] [R-1] • • • • • • • • • • • • [0] [C-1] [0] [C-1] [0] [C-1] 4*R*C ¡ ¡Bytes ¡ 8 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  9. Carnegie Mellon Nested ¡Array ¡Example ¡ #define PCOUNT 4 zip_dig pgh[PCOUNT] = {{1, 5, 2, 0, 6}, {1, 5, 2, 1, 3 }, {1, 5, 2, 1, 7 }, {1, 5, 2, 2, 1 }}; zip_dig 1 5 2 0 6 1 5 2 1 3 1 5 2 1 7 1 5 2 2 1 pgh[4]; 76 96 116 136 156 ¢ “ zip_dig pgh[4] ” ¡equivalent ¡to ¡“ int pgh[4][5] ” ¡ § Variable ¡ pgh : ¡array ¡of ¡4 ¡elements, ¡allocated ¡con;guously ¡ § Each ¡element ¡is ¡an ¡array ¡of ¡5 ¡ int ’s, ¡allocated ¡con;guously ¡ ¢ “Row-­‑Major” ¡ordering ¡of ¡all ¡elements ¡in ¡memory ¡ 9 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  10. Carnegie Mellon Nested ¡Array ¡Row ¡Access ¡ ¢ Row ¡Vectors ¡ § ¡ A[i] ¡is ¡array ¡of ¡ C ¡elements ¡ § Each ¡element ¡of ¡type ¡ T ¡ requires ¡ K ¡ bytes ¡ § Star;ng ¡address ¡ A + ¡ i ¡* ¡( C ¡ * ¡ K ) ¡ int A[R][C]; A[0] ¡ A[i] ¡ A[R-1] ¡ A A A A A A • ¡ ¡• ¡ ¡• ¡ • ¡ ¡• ¡ ¡• ¡ • ¡• ¡• ¡ • ¡• ¡• ¡ • ¡• ¡• ¡ [0] [0] [i] [i] [R-1] [R-1] [0] [C-1] [0] [C-1] [0] [C-1] A A+(i*C*4) A+((R-1)*C*4) 10 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  11. Carnegie Mellon Nested ¡Array ¡Row ¡Access ¡Code ¡ 1 5 2 0 6 1 5 2 1 3 1 5 2 1 7 1 5 2 2 1 int *get_pgh_zip(int index) pgh { return pgh[index]; } # %rdi = index leaq (%rdi,%rdi,4),%rax # 5 * index leaq pgh(,%rax,4),%rax # pgh + (20 * index) ¢ Row ¡Vector ¡ § ¡ pgh[index] ¡ is ¡array ¡of ¡5 ¡ int ’s ¡ § Star;ng ¡address ¡ pgh+20*index ¢ Machine ¡Code ¡ § Computes ¡and ¡returns ¡address ¡ § Compute ¡as ¡ pgh + 4*(index+4*index) 11 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  12. Carnegie Mellon Nested ¡Array ¡Element ¡Access ¡ ¢ Array ¡Elements ¡ § ¡ A[i][j] ¡ is ¡element ¡of ¡type ¡ T, ¡ which ¡requires ¡ K ¡bytes § Address ¡ ¡ A + i ¡ * ¡( C ¡ * ¡ K ) ¡ + ¡ ¡ j ¡* ¡ K ¡= ¡A ¡+ ¡ ( i ¡* ¡C ¡+ ¡ ¡j ) * ¡K ¡ int A[R][C]; A[0] ¡ A[i] ¡ A[R-1] ¡ A A A A A • ¡ ¡• ¡ ¡• ¡ • ¡ ¡• ¡ ¡• ¡ • ¡• ¡• ¡ ¡• ¡• ¡• ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡• ¡• ¡• ¡ • ¡• ¡• ¡ [0] [0] [i] [R-1] [R-1] [0] [C-1] [j] [0] [C-1] A A+(i*C*4) A+((R-1)*C*4) A+(i*C*4)+(j*4) 12 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  13. Carnegie Mellon Nested ¡Array ¡Element ¡Access ¡Code ¡ 1 5 2 0 6 1 5 2 1 3 1 5 2 1 7 1 5 2 2 1 pgh int get_pgh_digit (int index, int dig) { return pgh[index][dig]; } leaq (%rdi,%rdi,4), %rax # 5*index addl %rax, %rsi # 5*index+dig movl pgh(,%rsi,4), %eax # M[pgh + 4*(5*index+dig)] ¢ Array ¡Elements ¡ § ¡ pgh[index][dig] ¡ is ¡ int § Address: ¡ pgh + 20*index + 4*dig § = ¡ ¡ ¡ pgh + 4*(5*index + dig) 13 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend