-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmalloc.asm
91 lines (74 loc) · 1.28 KB
/
malloc.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
section .bss
v: resb 4
vp: resb 4
section .text
global _start
free:
pop edi
pop eax
mov DWORD [eax], 0
mov eax, 0
push edi
ret
alloc:
pop edi
mov eax, 45 ;sys_brk
xor ebx, ebx
int 80h
pop ebx
add eax, ebx ;number of bytes to be reserved
mov ebx, eax
mov eax, 45 ;sys_brk
int 80h
cmp eax, 0
jl exit ;exit, if error
; mov edi, eax ;EDI = highest available address
sub eax, 4 ;pointing to the last DWORD
; xor eax, eax
std ;backward
cld ;put DF flag to normal state
push edi
ret
malloc:
pop edi
pop edx
pop eax
mov [eax], edx
mov eax, 0
push edi
ret
_start:
mov eax, 4
push eax
call alloc
; mov ebx, [eax]
mov [v], eax ; make v pointer to whatever eax is pointing to
mov edi, msg
; mov [eax], edi ; put value at pointed address
mov ecx, [v]
push ecx
mov ecx, msg
push ecx
call malloc
mov ecx, [v]
mov ebx, [ecx]
mov [vp], ebx ; deref
mov ecx, [vp]
mov eax, 4
mov ebx, 1
mov edx, len
int 80h ;print a message
mov eax, [v]
push eax
call free
exit:
mov eax, 1
xor ebx, ebx
int 80h
section .data
x db '5'
y db '3'
msg db "Allocated 16 kb of memory!", 10
len equ $ - msg
segment .bss
sum resb 1