Carnegie Mellon
1 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
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
Carnegie Mellon
1 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
Carnegie Mellon
2 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Arrays ¡
¢ Structures ¡
¢ Floa?ng ¡Point ¡
Carnegie Mellon
3 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Basic ¡Principle ¡
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 ¡ char *p[3]; x ¡ x ¡+ ¡8 ¡ x ¡+ ¡16 ¡ x ¡+ ¡24 ¡
Carnegie Mellon
4 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Basic ¡Principle ¡
¢ Reference
Carnegie Mellon
5 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Declara?on ¡“zip_dig cmu” ¡equivalent ¡to ¡“int cmu[5]” ¡ ¢ Example ¡arrays ¡were ¡allocated ¡in ¡successive ¡20 ¡byte ¡blocks ¡
Carnegie Mellon
6 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
n Register ¡%rdi ¡contains ¡
n Register ¡%rsi ¡contains ¡ ¡
n Desired ¡digit ¡at ¡ ¡
n Use ¡memory ¡reference ¡
Carnegie Mellon
7 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
Carnegie Mellon
8 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Declara?on ¡
¢ Array ¡Size ¡
¢ Arrangement ¡
A [0] [0] A [0] [C-1]
A [1] [0] A [1] [C-1]
A [R-1] [0] A [R-1] [C-1]
Carnegie Mellon
9 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ “zip_dig pgh[4]” ¡equivalent ¡to ¡“int pgh[4][5]” ¡
¢ “Row-‑Major” ¡ordering ¡of ¡all ¡elements ¡in ¡memory ¡
Carnegie Mellon
10 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Row ¡Vectors ¡
A [i] [0] A [i] [C-1] A[i] ¡
A [R-1] [0] A [R-1] [C-1] A[R-1] ¡
A [0] [0] A [0] [C-1] A[0] ¡
Carnegie Mellon
11 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Row ¡Vector ¡
¢ Machine ¡Code ¡
Carnegie Mellon
12 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Array ¡Elements ¡
¡• ¡• ¡• ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡• ¡• ¡• ¡ A [i] [j] A[i] ¡
A [R-1] [0] A [R-1] [C-1] A[R-1] ¡
A [0] [0] A [0] [C-1] A[0] ¡
Carnegie Mellon
13 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Array ¡Elements ¡
§ = ¡ ¡ ¡pgh + 4*(5*index + dig)
Carnegie Mellon
14 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Variable ¡univ ¡denotes ¡
¢ Each ¡element ¡is ¡a ¡pointer ¡
¢ Each ¡pointer ¡points ¡to ¡array ¡
Carnegie Mellon
15 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Computa?on ¡
§ First ¡get ¡pointer ¡to ¡row ¡array ¡ § Then ¡access ¡element ¡within ¡array ¡
Carnegie Mellon
16 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
Carnegie Mellon
17 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Fixed ¡dimensions ¡
¢ Variable ¡dimensions, ¡
¢ Variable ¡dimensions, ¡
Carnegie Mellon
18 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Array ¡Elements ¡
Carnegie Mellon
19 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Array ¡Elements ¡
Carnegie Mellon
20 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Arrays ¡
¢ Structures ¡
¢ Floa?ng ¡Point ¡
Carnegie Mellon
21 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Structure ¡represented ¡as ¡block ¡of ¡memory ¡
¢ Fields ¡ordered ¡according ¡to ¡declara?on ¡
¢ Compiler ¡determines ¡overall ¡size ¡+ ¡posi?ons ¡of ¡fields ¡
Carnegie Mellon
22 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Genera?ng ¡Pointer ¡to ¡
Carnegie Mellon
23 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ C ¡Code ¡
Carnegie Mellon
24 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Unaligned ¡Data ¡
¢ Aligned ¡Data ¡
3 ¡bytes ¡ 4 ¡bytes ¡
Carnegie Mellon
25 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Aligned ¡Data ¡
¢ Mo?va?on ¡for ¡Aligning ¡Data ¡
§ Inefficient ¡to ¡load ¡or ¡store ¡datum ¡that ¡spans ¡quad ¡word ¡
§ Virtual ¡memory ¡trickier ¡when ¡datum ¡spans ¡2 ¡pages ¡
¢ Compiler ¡
Carnegie Mellon
26 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ 1 ¡byte: ¡char, ¡… ¡
¢ 2 ¡bytes: ¡short, ¡… ¡
¢ 4 ¡bytes: ¡int, ¡float, ¡… ¡
¢ 8 ¡bytes: ¡double, ¡long, ¡char *, ¡… ¡
¢ 16 ¡bytes: ¡long double ¡(GCC ¡on ¡Linux)
Carnegie Mellon
27 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Within ¡structure: ¡
¢ Overall ¡structure ¡placement ¡
§ K ¡= ¡Largest ¡alignment ¡of ¡any ¡element ¡
¢ Example: ¡
3 ¡bytes ¡ 4 ¡bytes ¡
Carnegie Mellon
28 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ For ¡largest ¡alignment ¡requirement ¡K ¡ ¢ Overall ¡structure ¡must ¡be ¡mul?ple ¡of ¡K ¡
7 ¡bytes ¡
Carnegie Mellon
29 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Overall ¡structure ¡length ¡
¢ Sa?sfy ¡alignment ¡requirement ¡ ¡
7 ¡bytes ¡
Carnegie Mellon
30 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Compute ¡array ¡offset ¡12*idx ¡
¢ Element ¡j ¡is ¡at ¡offset ¡8 ¡within ¡structure ¡ ¢ Assembler ¡gives ¡offset ¡a+8 ¡
2 ¡bytes ¡
2 ¡bytes ¡
Carnegie Mellon
31 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Put ¡large ¡data ¡types ¡first ¡ ¢ Effect ¡(K=4) ¡
3 ¡bytes ¡
Carnegie Mellon
32 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Arrays ¡
¢ Structures ¡
¢ Floa?ng ¡Point ¡
Carnegie Mellon
33 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ History ¡
§ Legacy, ¡very ¡ugly ¡
§ Supported ¡by ¡Shark ¡machines ¡ § Special ¡case ¡use ¡of ¡vector ¡instruc;ons ¡
§ Newest ¡version ¡ § Similar ¡to ¡SSE ¡ § Documented ¡in ¡book ¡
Carnegie Mellon
34 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
Carnegie Mellon
35 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
Carnegie Mellon
36 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Arguments ¡passed ¡in ¡%xmm0, ¡%xmm1, ¡... ¡ ¢ Result ¡returned ¡in ¡%xmm0 ¡ ¢ All ¡XMM ¡registers ¡caller-‑saved ¡
Carnegie Mellon
37 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Integer ¡(and ¡pointer) ¡arguments ¡passed ¡in ¡regular ¡registers ¡ ¢ FP ¡values ¡passed ¡in ¡XMM ¡registers ¡ ¢ Different ¡mov ¡instruc?ons ¡to ¡move ¡between ¡XMM ¡registers, ¡
Carnegie Mellon
38 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Lots ¡of ¡instruc?ons ¡
¢ Floa?ng-‑point ¡comparisons ¡
¢ Using ¡constant ¡values ¡
Carnegie Mellon
39 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Arrays ¡
¢ Structures ¡
¢ Combina?ons ¡
¢ Floa?ng ¡Point ¡
Carnegie Mellon
40 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Cmp: ¡Compiles ¡(Y/N) ¡ ¢ Bad: ¡Possible ¡bad ¡pointer ¡reference ¡(Y/N) ¡ ¢ Size: ¡Value ¡returned ¡by ¡sizeof
int A1[3] int *A2
Carnegie Mellon
41 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Cmp: ¡Compiles ¡(Y/N) ¡ ¢ Bad: ¡Possible ¡bad ¡pointer ¡reference ¡(Y/N) ¡ ¢ Size: ¡Value ¡returned ¡by ¡sizeof
int A1[3]
int *A2
A1 A2 Allocated ¡ ¡int ¡ Unallocated ¡pointer ¡ Allocated ¡ ¡pointer ¡ Unallocated ¡ ¡int ¡
Carnegie Mellon
42 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Cmp: ¡Compiles ¡(Y/N) ¡ ¢ Bad: ¡Possible ¡bad ¡pointer ¡reference ¡(Y/N) ¡ ¢ Size: ¡Value ¡returned ¡by ¡sizeof
int A1[3] int *A2[3] int (*A3)[3] int (*A4[3])
Carnegie Mellon
43 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
int A1[3]
int (*A3)[3]
int (*A4[3])
A1 A2/A4 Allocated ¡ ¡int ¡ Unallocated ¡pointer ¡ Allocated ¡ ¡pointer ¡ Unallocated ¡ ¡int ¡ A3
Carnegie Mellon
44 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Cmp: ¡Compiles ¡(Y/N) ¡ ¢ Bad: ¡Possible ¡bad ¡
¢ Size: ¡Value ¡returned ¡by ¡
int A1[3][5] int *A2[3][5] int (*A3)[3][5] int *(A4[3][5]) int (*A5[3])[5]
int A1[3][5] int *A2[3][5] int (*A3)[3][5] int *(A4[3][5]) int (*A5[3])[5]
Carnegie Mellon
45 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
int A1[3][5] int *A2[3][5] int (*A3)[3][5] int *(A4[3][5]) int (*A5[3])[5] A2/A4 A5 Allocated ¡ ¡int ¡ Unallocated ¡pointer ¡ Allocated ¡ ¡pointer ¡ Unallocated ¡ ¡int ¡ Allocated ¡ ¡pointer ¡to ¡unallocated ¡int ¡ A1 A3
Carnegie Mellon
46 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Cmp: ¡Compiles ¡(Y/N) ¡ ¢ Bad: ¡Possible ¡bad ¡
¢ Size: ¡Value ¡returned ¡by ¡
int A1[3][5]
int *A2[3][5]
int (*A3)[3][5]
int *(A4[3][5])
int (*A5[3])[5]
int A1[3][5]
int (*A3)[3][5]
int *(A4[3][5])
int (*A5[3])[5]