-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmalloc.c
More file actions
72 lines (60 loc) · 1.38 KB
/
malloc.c
File metadata and controls
72 lines (60 loc) · 1.38 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
/*
auther: Naman Tamrakar
date: 2023-07-19
decription: program to show how malloc can be use used to allocate memory
*/
#include <stdio.h>
#include <stdlib.h>
int *copy_arr_C(int *arr, int n) {
int *v = malloc(n * sizeof(int));
int i = 0;
while (i < n) {
v[i] = arr[i];
i++;
}
return v;
}
__attribute__((naked))
int *copy_arr(int *arr, int n) {
__asm__(
"pushq %rbp;"
"movq %rsp, %rbp;"
"sub $64, %rsp;"
"movq %rdi, -8(%rbp);"
"movl %esi, -16(%rbp);"
// int *v = malloc(n * sizeof(int));
"movq %rsi, %rdi;"
"imul $4, %rdi;"
"call malloc;"
"mov %rax, -24(%rbp);" // v
// i = 0
"movl $0, %edi;"
"movq -8(%rbp), %rdx;" // arr
"l1:;"
"cmpl -16(%rbp), %edi;"
"je end;"
"movl (%rdx,%rdi,4), %ecx;" // arr[i]
"movl %ecx, (%rax,%rdi,4);" // v[i] = arr[i]
"incl %edi;" // i++
"jmp l1;"
"end:;"
// restore stack
"add $64, %rsp;"
"movq %rbp, %rsp;"
"popq %rbp;"
// return result already in %rax register
"ret;"
);
}
int main() {
int n;
scanf("%d", &n);
int arr[n];
for (int i=0; i<n; i++)
scanf("%d", &arr[i]);
int *res = copy_arr(arr, n);
for (int i=0; i<n; i++)
printf("%d ", res[i]);
free(res);
return 0;
}