Introduction to 886 Assembly Lecture 7 D and N-D Arrays
D Arrays 4 5 tabular data rows and columns 4 6 8 6 9 5 4 8 6
D Arrays 4 5 4 6 8 6 9 5 https://advancedmathclubsk.weebly.com/matrices.html 4 8 6 5 5 5
D Arrays https://hiveminer.com/tags/desert,isfahan/recent
D Arrays https://hiveminer.com/tags/desert,isfahan/recent
D Arrays
D Arrays int a[][4] = {{,, 5, 7}, {, 4, 6, 8}, {4,,-,7}}; printf("%d\n", a[][]); 5 7 4 6 8 4-7
D Arrays int a[][4] = {{,, 5, 7}, {, 4, 6, 8}, {4,,-,7}}; printf("%d\n", a[i][j]); a[][] a[][] a[][] a[][] a[][] a[][] a[][] a[][] a[][] a[][] a[][] a[][]
How to implement D arrays?
Memory Memory 88 9 96 88 9 96 row by row column by column
Memory Memory 88 9 96 88 9 96 Row Major Column Major
Memory Memory 88 9 96 88 9 96 Row Major (C,C++,Pascal, Python-numpy) Column Major (Fortran, Matlab, R, )
Memory Memory 88 9 96 Assembly? 88 9 96 Row Major (C,C++,Pascal, Python-numpy) Column Major (Fortran, Matlab, R, )
Memory 88 9 96 Row Major (C,C++,Pascal, numpy)
Memory 88 9 96 a[i][] :? Row Major (C,C++,Pascal, numpy)
Memory 88 9 96 a[i][] : 4*i Row Major (C,C++,Pascal, numpy)
Memory 88 9 96 a[i][] : 4*i a[i][] :? Row Major (C,C++,Pascal, numpy)
Memory 88 9 96 a[i][] : 4*i a[i][] : 4*i+ Row Major (C,C++,Pascal, numpy)
Memory 88 9 96 a[i][] : 4*i a[i][j] :? Row Major (C,C++,Pascal, numpy)
Memory 88 9 96 a[i][] : 4*i a[i][j] : 4*i+j Row Major (C,C++,Pascal, numpy)
Memory 88 9 96 int a[m][n]; n-... 9 a[i][j] : n*i + j... 9 : : : : : : m- 8 8... 8 Row Major (C,C++,Pascal, numpy)
Memory 88 9 96 int a[m][n]; n-... 9... 9 a[i][j] offset = (n*i+j)*sizeof(int) : : : : : : m- 8 8... 8 Row Major (C,C++,Pascal, numpy)
Memory 88 9 96 int a[m][n]; m- n-... 9... 9 : : : : 8 8... 8 : : a[i][j] : n*i + j to find a[i][j] we need to know element size? m (no. of rows of a)? n (no. of columns of a)? Row Major (C,C++,Pascal, numpy)
Other standards? Column major? a[i][j] :? 4
Other standards? Column major? a[i][j] : 4*i+j 4
Other standards? Column major? a[i][j] : 4*i+j Index starting at a[i][j] :? 4
Other standards? Column major? a[i][j] : 4*i+j Index starting at a[i][j] : 4*i+j+ 4
Example: Print a D Array
Example: Print a D Array
Example: Print a D Array
Example: Print a D Array segment.data printdarray.asm array : dd,,, 4, 5, 6 dd,,, 4, 5, 6 dd,,, 4, 5, 6 dd 4, 4, 4, 44, 54, 64 segment.text ; printdarray (array, m, n) push 6 ; no of columns push 4 ; no of rows push array ; address of array call printdarray add esp,
Example: Print a D Array segment.data printdarray.asm array : dd,,, 4, 5, 6 dd,,, 4, 5, 6 dd,,, 4, 5, 6 dd 4, 4, 4, 44, 54, 64 segment.text ; printdarray (array, m, n) push 6 ; no of columns push 4 ; no of rows push array ; address of array call printdarray add esp,
Example: Print a D Array int printdarray(int a[][6], int m, int n) { for (int i = ; i < m; i++) { for (int j = ; j < n; j++) printf("%d,", a[i][j]); } putchar('\n'); } delim: db ", ", %define ARRAY [ebp+8] %define M [ebp+] %define N [ebp+6] ; printdarray(array, M, N) printdarray: push ebp mov ebp, esp mov ebx, ARRAY mov esi, loop: cmp esi, M jge endloop mov edi, loop: cmp edi, N jge endloop ; index = esi*n+edi mov eax, N mul esi add eax, edi mov eax, [ebx+4*eax] call print_int mov eax, delim call print_string inc edi jmp loop endloop: mov al, call print_char inc esi jmp loop endloop: mov esp, ebp pop ebp ret printdarray.asm
Example: Print a D Array int printdarray(int a[][6], int m, int n) { for (int i = ; i < m; i++) { for (int j = ; j < n; j++) printf("%d,", a[i][j]); } putchar('\n'); } delim: db ", ", %define ARRAY [ebp+8] %define M [ebp+] %define N [ebp+6] ; printdarray(array, M, N) printdarray: push ebp mov ebp, esp mov ebx, ARRAY mov esi, loop: cmp esi, M jge endloop mov edi, loop: cmp edi, N jge endloop ; index = esi*n+edi mov eax, N mul esi add eax, edi mov eax, [ebx+4*eax] call print_int mov eax, delim call print_string inc edi jmp loop endloop: mov al, call print_char inc esi jmp loop endloop: mov esp, ebp pop ebp ret printdarray.asm
Example: Print a D Array int printdarray(int a[][6], int m, int n) { for (int i = ; i < m; i++) { for (int j = ; j < n; j++) printf("%d,", a[i][j]); } putchar('\n'); } delim: db ", ", %define ARRAY [ebp+8] %define M [ebp+] %define N [ebp+6] ; printdarray(array, M, N) printdarray: push ebp mov ebp, esp mov ebx, ARRAY mov esi, loop: cmp esi, M jge endloop mov edi, loop: cmp edi, N jge endloop ; index = esi*n+edi mov eax, N mul esi add eax, edi mov eax, [ebx+4*eax] call print_int mov eax, delim call print_string inc edi jmp loop endloop: mov al, call print_char inc esi jmp loop endloop: mov esp, ebp pop ebp ret printdarray.asm
Example: Print a D Array int printdarray(int a[][6], int m, int n) { for (int i = ; i < m; i++) { for (int j = ; j < n; j++) printf("%d,", a[i][j]); } putchar('\n'); } delim: db ", ", %define ARRAY [ebp+8] %define M [ebp+] %define N [ebp+6] ; printdarray(array, M, N) printdarray: push ebp mov ebp, esp Make it faster? mov ebx, ARRAY mov esi, loop: cmp esi, M jge endloop mov edi, loop: cmp edi, N jge endloop ; index = esi*n+edi mov eax, N mul esi add eax, edi mov eax, [ebx+4*eax] call print_int mov eax, delim call print_string inc edi jmp loop endloop: mov al, call print_char inc esi jmp loop endloop: mov esp, ebp pop ebp ret printdarray.asm
Example: Print a D Array printdarray.asm mov ebx, ARRAY mov esi, loop: cmp esi, M jge endloop mov edi, loop: cmp edi, N jge endloop ; index = esi*n+edi mov eax, N mul esi add eax, edi mov eax, [ebx+4*eax] call print_int mov eax, delim call print_string printdarray4.asm mov ebx, ARRAY mov esi, loop: cmp esi, M jge endloop mov edi, loop: cmp edi, N jge endloop mov eax, [ebx] call print_int mov eax, delim call print_string add ebx, 4
D Arrays N D array M N M D arrays (rows) of size N Array[i, j] = Array[N*i + j] D array M N P M D arrays of dimension N P Array[i, j, k] = Array[ i, j*p + k] = Array[ i *N*P + j * P + k ] M P M N
D Arrays N D array M N M D arrays (rows) of size N Array[i, j] = Array[N*i + j] D array M N P M D arrays of dimension N P Array[i, j, k] = Array[ i, j*p + k] = Array[ i *N*P + j * P + k ] An M N D array of D arrays of size P Array[i, j, k] = Array[ i*n + j, k] = Array[ (i * N + j) P + k ] M M P N
D Arrays N D array M N M D arrays (rows) of size N Array[i, j] = Array[N*i + j] D array M N P M D arrays of dimension N P Array[i, j, k] = Array[ i, j*p + k] = Array[ i *N*P + j * P + k ] An M N D array of D arrays of size P Array[i, j, k] = Array[ i*n + j, k] = Array[ (i * N + j) P + k ] M M P number of + and * operations? N
ND Arrays D array M N M D arrays (rows) of size N Array[i, j] = Array[N*i + j] D array ND array M N P M D arrays of dimension N P Array[i, j, k] = Array[ i, j*p + k] = Array[ i *N*P + j * P + k ] An M N D array of D arrays of size P Array[i, j, k] = Array[ i*n + j, k] = Array[ (i * N + j) P + k ] M M M n Array[ i, i,, i n ] = Array[ M * * M n * i + M * * M n * i + + M n * i n- + i n ]
ND Arrays ND array M M M n Array[i, i,, i n ] = Array[ M * * M n * i + M * * M n * i + + M n * i n- + i n ] j = i j = M * j + i j = M * j + i : number of + and * operations? j n = M n * j n- + i n Array[i, i,, i n ] = Array[j n ]
ND Arrays: Row-major vs. Column-major What does row-major and column-major mean? Matlab vs Numpy ND-arrays