summaryrefslogtreecommitdiff
path: root/3/8_array_allocation_and_access
diff options
context:
space:
mode:
Diffstat (limited to '3/8_array_allocation_and_access')
-rw-r--r--3/8_array_allocation_and_access/36_sizes_and_addresses.c11
-rw-r--r--3/8_array_allocation_and_access/37_array_arithmetic.c7
-rw-r--r--3/8_array_allocation_and_access/38_nested_arrays.c18
-rw-r--r--3/8_array_allocation_and_access/39_explain_rows_cols.c8
-rw-r--r--3/8_array_allocation_and_access/40_diagonal_elements.c48
5 files changed, 92 insertions, 0 deletions
diff --git a/3/8_array_allocation_and_access/36_sizes_and_addresses.c b/3/8_array_allocation_and_access/36_sizes_and_addresses.c
new file mode 100644
index 0000000..c9b8056
--- /dev/null
+++ b/3/8_array_allocation_and_access/36_sizes_and_addresses.c
@@ -0,0 +1,11 @@
+// size total size start address element i
+short S[7];
+// 2 14 x_s xs + 2 * i
+short *T[3];
+// 8 24 x_t x_t + 8 * i
+short **U[6];
+// 8 48 x_u x_u + 8 * i
+int V[8];
+// 4 32 x_v x_v + 4 * i
+double *W[4];
+// 8 32 x_w x_w + 8 * i
diff --git a/3/8_array_allocation_and_access/37_array_arithmetic.c b/3/8_array_allocation_and_access/37_array_arithmetic.c
new file mode 100644
index 0000000..c2ecbf2
--- /dev/null
+++ b/3/8_array_allocation_and_access/37_array_arithmetic.c
@@ -0,0 +1,7 @@
+// S and i in rdx and rcx
+// type value assembly
+// S+1 short * xs + 2 leaq $2(%rdx), %rax, // leaq since we want to store the address not the value
+// S[3] short M[xs+6] movw $6(%rdx), %ax
+// &S[i] short * xs+2i leaq (rdx, rcx, 2), %rax
+// S[4*i+1] short M[xs+8i+2] movw $2(%rdx, %rcx, 8), %ax
+// S+i-5 short * xs+2i-10 leaq $-10(%rdx, %rcx, 2), %rax
diff --git a/3/8_array_allocation_and_access/38_nested_arrays.c b/3/8_array_allocation_and_access/38_nested_arrays.c
new file mode 100644
index 0000000..b53c7be
--- /dev/null
+++ b/3/8_array_allocation_and_access/38_nested_arrays.c
@@ -0,0 +1,18 @@
+#define M 20;
+#define N 30;
+
+long P[5][7];
+long Q[7][5];
+
+// i in rdi, j in rsi
+// sum_element:
+// leaq 0(,%rdi, 8), %rdx %rdx <- 8*i
+// subq %rdi, %rdx %rdx <- %rdx - i
+// addq %rsi, %rdx %rdx <- %rdx + j
+// leaq (%rsi, %rsi, 4), %rax %rax <- 5j
+// addq %rax, %rdi %rdi <- %rax + %rdi // rdi <- 5j + i
+// movq Q(, %rdi, 8), %rax %rax <- xq + 8(5j + i)
+// addq P(, %rdx, 8), %rax %rax <- %rax + xp + 8(7i + j)
+long sum_element(long i, long j) {
+ return P[i][j] + Q[j][i];
+}
diff --git a/3/8_array_allocation_and_access/39_explain_rows_cols.c b/3/8_array_allocation_and_access/39_explain_rows_cols.c
new file mode 100644
index 0000000..7304f23
--- /dev/null
+++ b/3/8_array_allocation_and_access/39_explain_rows_cols.c
@@ -0,0 +1,8 @@
+// addq %rdx, %rdi Aptr = xa + 64i
+// fix_matrix = int[16][16], so each int is 4 bytes and we need to multiple by the number of columns to find the
+// i row, A + L(C*i + j) <=> A + 4(16*i)
+//
+// leaq (%rsi, %rcx, 4), %rcx Bptr = xb + 4k, this one is simple B + L(C*i + j) <=> B + 4(j), find the first row of the column j
+//
+// leaq 1024(%rcx), %rsi Bend = xb + 4k + 1024, find the N row of column k,
+// B + 4(16*16 + k) <=> B + 1024 + 4k
diff --git a/3/8_array_allocation_and_access/40_diagonal_elements.c b/3/8_array_allocation_and_access/40_diagonal_elements.c
new file mode 100644
index 0000000..ab541db
--- /dev/null
+++ b/3/8_array_allocation_and_access/40_diagonal_elements.c
@@ -0,0 +1,48 @@
+// A in rdi, val in rsi
+// fix_set_diag:
+// movl $0, %eax
+// .L13
+// movl %esi, (%rdi, %rax)
+// addq $68, %rax
+// cmpq $1088, %rax
+// jne .L13
+// rep; ret
+#include <stdio.h>
+#define N 16
+typedef int fix_matrix[N][N];
+
+void fix_set_diag(fix_matrix A, int val) {
+ long i;
+ for (i = 0; i < N; i++) {
+ A[i][i] = val;
+ }
+}
+
+void fix_set_diag_opt(fix_matrix A, int val) {
+ int *Aptr = &A[0][0];
+ int *Aend = &A[N][N];
+ do {
+ *Aptr = val;
+ Aptr += N; // Next row
+ Aptr ++; // Next column
+ } while (Aptr != Aend);
+}
+
+int main(void) {
+ int i, j;
+ fix_matrix A;
+ for (i=0; i < N; i++) {
+ for (j=0; j < N; j++) {
+ A[i][j] = 0;
+ }
+ }
+
+ i, j = 0, 0;
+ fix_set_diag_opt(A, 1);
+ for (i=0; i < N; i++) {
+ for (j=0; j < N; j++) {
+ printf("%ld", A[i][j]);
+ }
+ printf("\n");
+ }
+}