-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalloc.asm
106 lines (82 loc) · 1.16 KB
/
alloc.asm
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
94
95
96
97
98
99
100
101
102
103
104
105
106
section .text
; (number of bytes)
basically_malloc:
push rbp
mov rbp,rsp
mov rsi, rdi
mov rax, 9
mov rdi, 0
mov rdx, 3
mov r10, 34
mov r8, 0
mov r9, 0
syscall
mov rsp, rbp
pop rbp
ret
; (size of objects in bytes, number of objects)
basically_calloc:
push rbp
mov rbp,rsp
; allocate new space (preserving length)
mov rax, rdi
imul rsi
mov rsi, rax
push rsi
mov rax, 9
mov rdi, 0
mov rdx, 3
mov r10, 34
mov r8, 0
mov r9, 0
syscall
; set new space to all zeros (preserving rax)
pop rcx
push rax
mov rdi, rax
mov al, 0
rep stosb
pop rax
mov rsp, rbp
pop rbp
ret
; (pointer, oldsize, newsize)
basically_realloc:
push rbp
mov rbp, rsp
push rdi
push rsi
; allocate new space
mov rsi, rdx
mov rax, 9
mov rdi, 0
mov rdx, 3
mov r10, 34
mov r8, 0
mov r9, 0
syscall
; copy old -> new
pop rcx
pop rsi
push rcx
push rsi
mov rdi, rax
rep movsb
; free old space
mov rax, 11
pop rdi
pop rsi
syscall
mov rsp, rbp
pop rbp
ret
; (pointer, size in bytes)
basically_free:
push rbp
mov rbp, rsp
; rdi and rsi end up in the correct spots based on calling convention
mov rax, 11
syscall
mov rsp,rbp
pop rbp
ret