-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMap.asm
More file actions
349 lines (290 loc) · 5.48 KB
/
Copy pathHashMap.asm
File metadata and controls
349 lines (290 loc) · 5.48 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
section .data
fmt_put: db "Put %d -> %d", 10, 0
fmt_get: db "Get %d -> %d (found=%d)", 10, 0
fmt_info: db "HashMap: size=%d capacity=%d", 10, 0
fmt_contains:db "Contains %d: %d", 10, 0
fmt_pass: db "All tests passed.", 10, 0
HM_CAP equ 0
HM_SIZE equ 4
HM_BUCKETS equ 8
HM_STRUCT equ 16
NODE_KEY equ 0
NODE_VAL equ 4
NODE_NEXT equ 8
NODE_SIZE equ 16
section .text
global main, hashmap_create, hashmap_put, hashmap_get, hashmap_remove, hashmap_free
extern malloc, calloc, free, printf
hashmap_create:
push rbp
mov rbp, rsp
push rbx
push r12
sub rsp, 16
mov r12d, edi
mov edi, HM_STRUCT
call malloc
mov rbx, rax
mov dword [rbx + HM_CAP], r12d
mov dword [rbx + HM_SIZE], 0
movsxd rdi, r12d
mov esi, 8
call calloc
mov [rbx + HM_BUCKETS], rax
mov rax, rbx
add rsp, 16
pop r12
pop rbx
pop rbp
ret
hashmap_hash:
mov eax, edi
mov ecx, esi
xor edx, edx
cdq
idiv ecx
mov eax, edx
test eax, eax
jns .hash_pos
neg eax
.hash_pos:
ret
hashmap_put:
push rbp
mov rbp, rsp
push rbx
push r12
push r13
push r14
sub rsp, 16
mov rbx, rdi
mov r12d, esi
mov r13d, edx
mov edi, r12d
mov esi, [rbx + HM_CAP]
call hashmap_hash
mov r14d, eax
mov rcx, [rbx + HM_BUCKETS]
movsxd rax, r14d
mov rdi, [rcx + rax*8]
.put_search:
test rdi, rdi
jz .put_new
cmp dword [rdi + NODE_KEY], r12d
jne .put_next
mov dword [rdi + NODE_VAL], r13d
jmp .put_done
.put_next:
mov rdi, [rdi + NODE_NEXT]
jmp .put_search
.put_new:
mov edi, NODE_SIZE
call malloc
mov dword [rax + NODE_KEY], r12d
mov dword [rax + NODE_VAL], r13d
mov rcx, [rbx + HM_BUCKETS]
movsxd rdx, r14d
mov rdi, [rcx + rdx*8]
mov [rax + NODE_NEXT], rdi
mov [rcx + rdx*8], rax
inc dword [rbx + HM_SIZE]
.put_done:
add rsp, 16
pop r14
pop r13
pop r12
pop rbx
pop rbp
ret
hashmap_get:
push rbp
mov rbp, rsp
push rbx
push r12
push r13
sub rsp, 8
mov rbx, rdi
mov r12d, esi
mov r13, rdx
mov edi, r12d
mov esi, [rbx + HM_CAP]
call hashmap_hash
mov rcx, [rbx + HM_BUCKETS]
movsxd rax, eax
mov rdi, [rcx + rax*8]
.get_search:
test rdi, rdi
jz .get_not_found
cmp dword [rdi + NODE_KEY], r12d
jne .get_next
mov eax, [rdi + NODE_VAL]
mov [r13], eax
mov eax, 1
jmp .get_done
.get_next:
mov rdi, [rdi + NODE_NEXT]
jmp .get_search
.get_not_found:
xor eax, eax
.get_done:
add rsp, 8
pop r13
pop r12
pop rbx
pop rbp
ret
hashmap_contains:
push rbp
mov rbp, rsp
push rbx
push r12
sub rsp, 16
mov rbx, rdi
mov r12d, esi
mov edi, r12d
mov esi, [rbx + HM_CAP]
call hashmap_hash
mov rcx, [rbx + HM_BUCKETS]
movsxd rax, eax
mov rdi, [rcx + rax*8]
.cont_search:
test rdi, rdi
jz .cont_no
cmp dword [rdi + NODE_KEY], r12d
jne .cont_next
mov eax, 1
jmp .cont_done
.cont_next:
mov rdi, [rdi + NODE_NEXT]
jmp .cont_search
.cont_no:
xor eax, eax
.cont_done:
add rsp, 16
pop r12
pop rbx
pop rbp
ret
hashmap_free:
push rbp
mov rbp, rsp
push rbx
push r12
push r13
push r14
sub rsp, 16
mov rbx, rdi
mov r14d, [rbx + HM_CAP]
xor r12d, r12d
.free_bucket:
cmp r12d, r14d
jge .free_struct
mov rcx, [rbx + HM_BUCKETS]
movsxd rax, r12d
mov r13, [rcx + rax*8]
.free_chain:
test r13, r13
jz .free_next_bucket
mov rdi, r13
mov r13, [r13 + NODE_NEXT]
call free
jmp .free_chain
.free_next_bucket:
inc r12d
jmp .free_bucket
.free_struct:
mov rdi, [rbx + HM_BUCKETS]
call free
mov rdi, rbx
call free
add rsp, 16
pop r14
pop r13
pop r12
pop rbx
pop rbp
ret
main:
push rbp
mov rbp, rsp
push rbx
sub rsp, 24
mov edi, 8
call hashmap_create
mov rbx, rax
mov rdi, rbx
mov esi, 10
mov edx, 100
call hashmap_put
mov rdi, rbx
mov esi, 20
mov edx, 200
call hashmap_put
mov rdi, rbx
mov esi, 30
mov edx, 300
call hashmap_put
mov rdi, rbx
mov esi, 42
mov edx, 420
call hashmap_put
lea rdi, [rel fmt_info]
mov esi, [rbx + HM_SIZE]
mov edx, [rbx + HM_CAP]
xor eax, eax
call printf
mov rdi, rbx
mov esi, 10
call hashmap_contains
mov edx, eax
lea rdi, [rel fmt_contains]
mov esi, 10
xor eax, eax
call printf
mov rdi, rbx
mov esi, 99
call hashmap_contains
mov edx, eax
lea rdi, [rel fmt_contains]
mov esi, 99
xor eax, eax
call printf
lea rdx, [rbp - 20]
mov rdi, rbx
mov esi, 20
call hashmap_get
mov r8d, eax
mov ecx, [rbp - 20]
lea rdi, [rel fmt_get]
mov esi, 20
mov edx, ecx
mov ecx, r8d
xor eax, eax
call printf
mov rdi, rbx
mov esi, 20
mov edx, 999
call hashmap_put
lea rdx, [rbp - 20]
mov rdi, rbx
mov esi, 20
call hashmap_get
mov r8d, eax
mov ecx, [rbp - 20]
lea rdi, [rel fmt_get]
mov esi, 20
mov edx, ecx
mov ecx, r8d
xor eax, eax
call printf
lea rdi, [rel fmt_pass]
xor eax, eax
call printf
mov rdi, rbx
call hashmap_free
xor eax, eax
add rsp, 24
pop rbx
pop rbp
ret
section .note.GNU-stack noalloc noexec nowrite progbits