EL3011 - 12 Data Structure
EL3011 - 12 Data Structure
Modul 12
Data Structure
EL3011 Arsitektur Sistem Komputer
Contents
1. Arrays
• One-dimensional
• Multi-dimensional (nested)
• Multi-level
2. Structure
• Allocation
• Access
• Alignment
This module adopted from 15-213 Introduction to Computer Systems Lecture, Carnegie Mellon
University, 2020
3
12.2. Arrays
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
5
Array Access
• Basic Principle
T A[L];
• Array of data type T and length L
• Identifier A can be used as a pointer to array element 0:
Type T*
int val[5]; 1 5 2 1 3
x x + 4 x + 8 x + 12 x + 16 x + 20
Array Access
• Basic Principle
T A[L];
• Array of data type T and length L
• Identifier A can be used as a pointer to array element 0:
Type T*
int val[5]; 1 5 2 1 3
x x + 4 x + 8 x + 12 x + 16 x + 20
Array Access
• Basic Principle
T A[L];
• Array of data type T and length L
• Identifier A can be used as a pointer to array element 0:
Type T*
int val[5]; 1 5 2 1 3
x x + 4 x + 8 x + 12 x + 16 x + 20
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 };
zip_dig cmu; 1 5 2 1 3
16 20 24 28 32 36
zip_dig mit; 0 2 1 3 9
36 40 44 48 52 56
zip_dig ucb; 9 4 7 2 0
56 60 64 68 72 76
• Declaration “zip_dig cmu” equivalent to “int cmu[5]”
• Example arrays were allocated in successive 20 byte blocks
• Not guaranteed to happen in general
9
int get_digit
(zip_dig z, int digit)
{
return z[digit]; n Register %rdi contains
} starting address of array
n Register %rsi contains
x86-64 array index
# %rdi = z n Desired digit at
# %rsi = digit %rdi + 4*%rsi
movl (%rdi,%rsi,4), %eax # z[digit] n Use memory reference
(%rdi,%rsi,4)
10
# %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
11
# %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
12
A1 Allocated pointer
Unallocated pointer
A2
Allocated int
Unallocated int
A1 Allocated pointer
Unallocated pointer
A2
Allocated int
Unallocated int
Allocated pointer
Unallocated pointer
Allocated int
Unallocated int
16
A1
A2
A3
Allocated pointer
Unallocated pointer
Allocated int
Unallocated int
17
A1
A2
A3
Allocated pointer
Unallocated pointer
Allocated int
Unallocated int
18
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
19
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];
int A[R][C];
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)
21
• Row Vector
• pgh[index] is array of 5 int’s
• Starting address pgh+20*index
• Machine Code
• Computes and returns address
• Compute as pgh + 4*(index+4*index)
22
int A[R][C];
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)
23
• Array Elements
• pgh[index][dig] is int
• Address: pgh + 20*index + 4*dig
= pgh + 4*(5*index + dig)
24
cmu
1 5 2 1 3
univ
16 20 24 28 32 36
160 36 mit
0 2 1 3 9
168 16
176 56 ucb 36 40 44 48 52 56
9 4 7 2 0
56 60 64 68 72 76
25
• 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
26
Mem[pgh+20*index+4*digit] Mem[Mem[univ+8*index]+4*digit]
27
N X N Matrix #define N 16
16 X 16 Matrix Access
¢ Array Elements
§ int A[16][16];
§ Address A + i * (C * K) + j * K
§ C = 16, K = 4
/* Get element A[i][j] */
int fix_ele(fix_matrix A, size_t i, size_t j) {
return A[i][j];
}
n X n Matrix Access
¢ Array Elements
§ size_t n;
§ int A[n][n];
§ Address A + i * (C * K) + j * K
§ C = n, K = 4
§ Must perform integer multiplication
/* Get element A[i][j] */
int var_ele(size_t n, int A[n][n], size_t i, size_t j)
{
return A[i][j];
}
Decl ***An
• Cmp: Compiles (Y/N)
• Bad: Possible bad pointer Cmp Bad Size
reference (Y/N) int A1[3][5]
• Size: Value returned by int *A2[3][5]
sizeof int (*A3)[3][5]
int *(A4[3][5])
int (*A5[3])[5]
33
A2/A4
A3
A5
35
12.2. Structures
Structure Representation
r
struct rec {
int a[4];
size_t i; a i next
struct rec *next;
0 16 24 32
};
.L11: # loop:
addq $1, %rax # len ++
movq 24(%rdi), %rdi # r = Mem[r+24]
testq %rdi, %rdi # Test r
jne .L11 # If != 0, goto loop
40
struct rec {
.L11: # loop:
movq 16(%rdi), %rax # i = Mem[r+16]
movl %esi, (%rdi,%rax,4) # Mem[r+4*i] = val
movq 24(%rdi), %rdi # r = Mem[r+24]
testq %rdi, %rdi # Test r
jne .L11 # if !=0 goto loop
41
• Aligned Data
• Primitive data type requires B bytes implies
Address must be multiple of B
c 3 bytes i[0] i[1] 4 bytes v
p+0 p+4 p+8 p+16 p+24
Multiple of 4 Multiple of 8
Multiple of 8 Multiple of 8
42
Alignment Principles
• Aligned Data
• Primitive data type requires B bytes
• Address must be multiple of B
• Required on some machines; advised on x86-64
• Motivation for Aligning Data
• Memory accessed by (aligned) chunks of 4 or 8 bytes (system
dependent)
• Inefficient to load or store datum that spans cache lines (64 bytes).
Intel states should avoid crossing 16 byte boundaries.
[Cache lines will be discussed in Lecture 11.]
• Virtual memory trickier when datum spans 2 pages (4 KB pages)
[Virtual memory pages will be discussed in Lecture 17.]
• Compiler
• Inserts gaps in structure to ensure correct alignment of fields
43
Multiple of 4 Multiple of 8
Multiple of 8 Multiple of 8
Internal padding
45
struct S2 {
double v;
• For largest alignment requirement K int i[2];
char c;
• Overall structure must be multiple of K } *p;
External padding
v i[0] i[1] c 7 bytes
p+0 p+8 p+16 p+24
Multiple of K=8
46
i 2 bytes v j 2 bytes
a+12*idx a+12*idx+8
Saving Space
• Put large data types first
struct S4 { struct S5 {
char c; int i;
int i; char c;
char d; char d;
} *p; } *p;
http://www.cs.cmu.edu/~213/oldexams/exam1-f12.pdf
50
a X X X X X X X b b b b b b b b
c c c c d d d X e e e e e e e e
f f f f f f f f|
http://www.cs.cmu.edu/~213/oldexams/exam1-f12.pdf
51
http://www.cs.cmu.edu/~213/oldexams/exam1-f12.pdf
52
a d d d c c c c b b b b b b b b
e e e e e e e e f f f f f f f f|
http://www.cs.cmu.edu/~213/oldexams/exam1-f12.pdf
53
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
• Combinations
• Can nest structure and array code arbitrarily