-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy path2d_arr.c
More file actions
94 lines (75 loc) · 1.65 KB
/
Copy path2d_arr.c
File metadata and controls
94 lines (75 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
auther: Naman Tamrakar
date: 2023-07-19
description: A sample program to add values in 2 array
*/
#include <stdio.h>
#include <stdlib.h>
int sol_C(int **arr, int m, int n) {
int s = 0, i = 0;
while (i < m) {
int j = 0;
while (j < n) {
s += arr[i][j];
j++;
}
i++;
}
return s;
}
__attribute__((naked))
int sol(int **arr, int m, int n) {
__asm__(
"push %rbp;"
"movq %rsp, %rbp;"
"movq %rdi, -8(%rbp);"
"movl %esi, -16(%rbp);"
"movl %edx, -24(%rbp);"
// int s = 0
"movl $0, %eax;"
// i = 0;
"movl $0, %ecx;"
"l1:;"
// while (i < m) {
"cmpl -16(%rbp), %ecx;"
"jge end;"
// j = 0;
"movl $0, %edx;"
"l2:;"
// while (j < n) {
"cmpl -24(%rbp), %edx;"
"jge l1_end;"
// s += arr[i][j];
"movq -8(%rbp), %rdi;" // arr
"movq (%rdi,%rcx,8), %rdi;" // arr[i]
"movl (%rdi,%rdx,4), %edi;" // arr[i][j]
"addl %edi, %eax;"
// j++;
"incl %edx;"
"jmp l2;"
"l1_end:;"
// i++;
"incl %ecx;"
"jmp l1;"
"end:;"
// return s;
"movq %rbp, %rsp;"
"popq %rbp;"
"ret;"
);
}
int main() {
int m, n;
scanf("%d", &m);
scanf("%d", &n);
int **arr = malloc(m * sizeof(int *));
for (int i=0; i<m; i++) {
arr[i] = malloc(n * sizeof(int));
for (int j=0; j<n; j++)
scanf("%d", &arr[i][j]);
}
printf("%d", sol(arr, m, n));
for (int i=0; i<m; i++)
free(arr[i]);
free(arr);
}