-
-
Notifications
You must be signed in to change notification settings - Fork 359
/
Copy pathtree_traversal.s
518 lines (495 loc) · 13.8 KB
/
tree_traversal.s
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
.intel_syntax noprefix
# System V calling convention cheatsheet
# Params: rdi, rsi, rdx, rcx, r8, r9, xmm0-7
# Return: rax (int 64 bits), rax:rdx (int 128 bits), xmm0 (float)
# Callee cleanup: rbx, rbp, r12-15
# Scratch: rax, rdi, rsi, rdx, rcx, r8, r9, r10, r11
.section .rodata
not_bt: .string "This is not a binary tree.\n"
fmt_tree: .string "%d \n"
.equ stack_size, 16
.equ stack_array, 0
.equ stack_top, 8
.equ stack_cap, 12
.equ queue_size, 20
.equ queue_array, 0
.equ queue_front, 8
.equ queue_back, 12
.equ queue_cap, 16
.equ tree_children, 0
.equ tree_num_children, 8
.equ tree_value, 12
.equ tree_size, 16
.section .text
.global main
.extern printf, malloc, free, memcpy
# rdi - stack ptr
get_stack:
push r12
mov r12, rdi
mov rdi, 32 # Creating a 32 byte array
call malloc
mov QWORD PTR [r12], rax # Saving the data into the stack
mov DWORD PTR [r12 + 8], 0
mov DWORD PTR [r12 + 12], 32
pop r12
ret
# rdi - stack ptr
# rsi - element ptr
stack_push:
push r12
push r13
push r14
mov r12, rdi # Saving the variables
mov r13, rsi
mov r14d, DWORD PTR [r12 + 8]
mov esi, DWORD PTR [r12 + 12]
cmp rsi, r14 # Check if top is equal to capacity
jne stack_push_append
shl rsi, 1 # Calculate new capacity in bytes
mov DWORD PTR [r12 + 12], esi # Saving new capcaity
mov rdi, [r12]
call realloc # Making the array bigger
mov QWORD PTR [r12], rax
stack_push_append:
add r14, 8
mov rax, QWORD PTR [r12]
lea rax, [rax + r14]
mov QWORD PTR [rax], r13 # Saving element and new top
mov DWORD PTR [r12 + 8], r14d
pop r14
pop r13
pop r12
ret
# rdi - stack ptr
# RET rax - element ptr
stack_pop:
push r12
mov r12d, DWORD PTR [rdi + 8] # Get top
test r12, r12 # Check if top is zero
jne stack_pop_element
xor rax, rax # Return 0
jmp stack_pop_return
stack_pop_element:
mov rax, [rdi]
lea rax, [rax + r12] # Get the element
mov rax, QWORD PTR [rax]
sub r12, 8 # Subtract 1 from top and save it
mov DWORD PTR [rdi + 8], r12d
stack_pop_return:
pop r12
ret
# rdi - stack ptr
free_stack:
mov rdi, QWORD PTR [rdi]
call free # Free stack array
ret
# rdi - queue ptr
get_queue:
push r12
mov r12, rdi
mov rdi, 32 # Create a 32 byte array
call malloc
mov QWORD PTR [r12], rax # Saving data to the queue pointer
mov QWORD PTR [r12 + 8], 0
mov DWORD PTR [r12 + 16], 32
pop r12
ret
# rdi - queue ptr
queue_resize:
push r12
push r13
push r14
mov r12, rdi
mov edi, DWORD PTR [r12 + 16] # Get new capacity and create new array
shl rdi, 1
call malloc
mov r13, rax
mov r14, QWORD PTR[r12]
mov rdi, r13 # Copy data from front to capacity
mov eax, DWORD PTR [r12 + 8]
lea rsi, [r14 + rax]
mov edx, DWORD PTR [r12 + 16]
sub edx, DWORD PTR [r12 + 8]
call memcpy
mov eax, DWORD PTR [r12 + 16] # Copy data from start of array to front
sub eax, DWORD PTR [r12 + 8]
lea rdi, [r13 + rax]
mov rsi, r14
mov edx, DWORD PTR [r12 + 8]
call memcpy
mov rdi, r14 # New array has front at 0 and back at the old capacity
call free # So free the old array then save the new queue
mov QWORD PTR [r12], r13
mov eax, DWORD PTR [r12 + 16]
sub rax, 8
mov DWORD PTR [r12 + 12], eax
mov DWORD PTR [r12 + 8], 0
mov eax, DWORD PTR [r12 + 16]
shl rax, 1
mov DWORD PTR [r12 + 16], eax
pop r14
pop r13
pop r12
ret
# rdi - queue ptr
# rsi - element
enqueue:
push r12
push r13
push r14
push r15
mov r12, rdi # Saving parameters
mov r13, rsi
mov r14d, DWORD PTR [rdi + 8]
mov eax, DWORD PTR [rdi + 12]# Calculating new back
add eax, 8
mov edi, DWORD PTR [r12 + 16]
cdq
idiv edi
cmp rdx, r14 # Check if front and new back are equal
jne enqueue_append
mov rdi, r12 # If so resize the queue
call queue_resize
enqueue_append:
mov r14, QWORD PTR [r12] # Saving the element
mov r15d, DWORD PTR [r12 + 12]
lea r14, [r14 + r15]
mov QWORD PTR [r14], r13
mov r14d, DWORD PTR [r12 + 16]# Calculating new back and then saving it
add r15, 8
mov rax, r15
cdq
idiv r14d
mov DWORD PTR [r12 + 12], edx
pop r15
pop r14
pop r13
pop r12
ret
# rdi - queue ptr
# RET rax - element
dequeue:
push r12
push r13
mov r12d, DWORD PTR [rdi + 8] # Check if queue is empty
mov r13d, DWORD PTR [rdi + 12]
xor rax, rax
cmp r12, r13
je dequeue_return # if empty return null
mov r12, QWORD PTR [rdi] # else return element pointer
mov r13d, DWORD PTR [rdi + 8]
lea r13, [r12 + r13]
mov eax, DWORD PTR [rdi + 8]
add eax, 8
mov r12d, DWORD PTR [rdi + 16] # Calculate new front
cdq
idiv r12d
mov DWORD PTR [rdi + 8], edx # Save new front
mov rax, QWORD PTR [r13]
dequeue_return:
pop r13
pop r12
ret
# rdi - queue ptr
free_queue:
mov rdi, QWORD PTR [rdi] # Free queue array
call free
ret
# rdi - levels
# rsi - children_size
# RET rax:rdx - the tree - children|value|children_size
create_tree:
push rbx
push r12
push r13
push r14
push r15
mov r12, rdi
mov r13, rsi
test rdi, rdi
jz create_tree_leaf
mov r14, rsi # We'll allocate sizeof(tree) * children_size bytes of memory
shl r14, 4 # save the size calculation to a callee-saved register so we can reuse it after the malloc
mov rdi, r14
call malloc
mov r15, rax # Save the children address twice, once for the return value, once for the loop variable
mov rbx, rax
lea r14, [rax + r14] # Calculate the address of the element after last of the children array
create_tree_children:
cmp rbx, r14
je create_tree_return
lea rdi, [r12 - 1] # levels - 1
mov rsi, r13
call create_tree
mov QWORD PTR [rbx], rax # Save the created tree to memory
mov QWORD PTR [rbx + 8], rdx # The offset of children_size, writing out explicitly would've made the line way too long
add rbx, tree_size
jmp create_tree_children
create_tree_leaf:
mov r15, 0
xor r13, r13 # Leaves won't have any children
create_tree_return:
mov rax, r15 # The children pointer will be in r15
mov rdx, r12
shl rdx, 32 # The tree's value will be the current "levels"
shl r13, 4
or rdx, r13 # Generate the return value by moving the value to the upper 32 bits
pop r15
pop r14
pop r13
pop r12
pop rbx
ret
# rdi - children ptr
# rsi - children size
free_tree:
push r12
push r13
push r14
push r15
test rdi, rdi # Make sure the pointer is non-zero
jz free_tree_return
mov r12, rdi # Saving array
lea r13, [r12 + rsi] # Get start and end of the array
mov r14, r12
free_tree_free_kid:
cmp r14, r13 # Loop thought the array and free all children
je free_tree_free_array
mov rdi, QWORD PTR [r14]
mov esi, DWORD PTR [r14 + 8]
call free_tree
add r14, tree_size
jmp free_tree_free_kid
free_tree_free_array:
mov rdi, r12 # Free the array
call free
free_tree_return:
pop r15
pop r14
pop r13
pop r12
ret
# rdi - children ptr
# rsi - value|children_size
dfs_recursive:
push r12
push r13
mov r12, rdi
mov r13, rsi
mov rdi, OFFSET fmt_tree # Handle the current node
shr rsi, 32 # The tree value is in the upper 32 bits
xor rax, rax
call printf
mov r13d, r13d # Zero out the top 32 bits
add r13, r12 # Pointer pointing after the last element of the children array
dfs_recursive_children:
cmp r12, r13 # If we reached the end, return
je dfs_recursive_return
mov rdi, QWORD PTR [r12]
mov rsi, QWORD PTR [r12 + 8]
call dfs_recursive
add r12, tree_size
jmp dfs_recursive_children
dfs_recursive_return:
pop r13
pop r12
ret
# rdi - children ptr
# rsi - value|children_size
dfs_recursive_postorder:
push r12
push r13
push r14
mov r12, rdi
mov r13, rsi
mov r14, rsi
mov r13d, r13d # Zero out the top 32 bits
add r13, r12 # Pointer pointing after the last element of the children array
dfs_recursive_po_children:
cmp r12, r13 # If we reached the end, return
je dfs_recursive_po_return
mov rdi, QWORD PTR [r12]
mov rsi, QWORD PTR [r12 + 8]
call dfs_recursive_postorder
add r12, tree_size
jmp dfs_recursive_po_children
dfs_recursive_po_return:
mov rdi, OFFSET fmt_tree # Handle the current node
mov rsi, r14
shr rsi, 32 # The tree value is in the upper 32 bits
xor rax, rax
call printf
pop r14
pop r13
pop r12
ret
# rdi - children ptr
# rsi - value|children_size
dfs_recursive_inorder_btree:
push r12
push r13
mov r12, rdi
mov r13, rsi
mov rax, rsi
mov eax, eax
cmp rax, 0 # Check what type of tree it is.
je dfs_recursive_bt_size0
cmp rax, 16
je dfs_recursive_bt_size1
cmp rax, 32
je dfs_recursive_bt_size2
mov rdi, OFFSET not_bt # If the tree is not binary then print a warning
xor rax, rax
call printf
jmp dfs_recursive_bt_return
dfs_recursive_bt_size0:
mov rdi, OFFSET fmt_tree # If the node is a leaf then print its id
shr rsi, 32
xor rax, rax
call printf
jmp dfs_recursive_bt_return
dfs_recursive_bt_size1:
mov rdi, QWORD PTR [r12] # If the node has 1 child then call the function and print the id
mov rsi, QWORD PTR [r12 + 8]
call dfs_recursive_inorder_btree
mov rdi, OFFSET fmt_tree
mov rsi, r13
shr rsi, 32
xor rax, rax
call printf
jmp dfs_recursive_bt_return
dfs_recursive_bt_size2:
mov rdi, QWORD PTR [r12] # Same as above just print id inbetween the calls
mov rsi, QWORD PTR [r12 + 8]
call dfs_recursive_inorder_btree
mov rdi, OFFSET fmt_tree
mov rsi, r13
shr rsi, 32
xor rax, rax
call printf
mov rdi, QWORD PTR [r12 + 16]
mov rsi, QWORD PTR [r12 + 24]
call dfs_recursive_inorder_btree
dfs_recursive_bt_return:
pop r13
pop r12
ret
# rdi - children ptr
# rsi - value|children_size
dfs_stack:
push r12
push r13
push r14
sub rsp, 16 # Create stack
mov r12, rsp
push rsi # Save node to use as pointer
push rdi
mov rdi, r12
call get_stack # Init stack
mov rdi, r12
mov rsi, rsp
call stack_push # Push node
mov rdi, r12 # Pop stack
call stack_pop
dfs_stack_loop:
test rax, rax # Test if stack is empty
jz dfs_stack_return
mov r13, rax
mov rdi, OFFSET fmt_tree # Print id
mov esi, DWORD PTR [r13 + 12]
xor rax, rax
call printf
mov eax, DWORD PTR [r13 + 8] # Get start and end of array
mov r13, QWORD PTR [r13]
lea r14, [r13 + rax]
dfs_stack_push_child:
cmp r13, r14 # Check if the pointers are the same
je dfs_stack_end_push
mov rdi, r12 # Push node into the stack
mov rsi, r13
call stack_push
add r13, tree_size
jmp dfs_stack_push_child
dfs_stack_end_push:
mov rdi, r12 # Pop stack
call stack_pop
jmp dfs_stack_loop
dfs_stack_return:
mov rdi, r12 # Free stack
call free_stack
add rsp, 32
pop r14
pop r13
pop r12
ret
# rdi - children ptr
# rsi - value|children_size
bfs_queue:
push r12
push r13
push r14
sub rsp, 20 # Create queue
mov r12, rsp
push rsi # Save node to use as pointer
push rdi
mov rdi, r12
call get_queue # Init queue
mov rdi, r12
mov rsi, rsp
call enqueue # enqueue node
mov eax, DWORD PTR [r12 + 8]
mov edi, DWORD PTR [r12 + 12]
bfs_queue_loop:
cmp eax, edi
je bfs_queue_return
mov rdi, r12 # dequeue
call dequeue
test rax, rax # Test if queue is empty
jz bfs_queue_return
mov r13, rax
mov rdi, OFFSET fmt_tree # Print id
mov esi, DWORD PTR [r13 + 12]
xor rax, rax
call printf
mov eax, DWORD PTR [r13 + 8] # Get start and end of array
mov r13, QWORD PTR [r13]
lea r14, [r13 + rax]
bfs_queue_push_child:
cmp r13, r14 # Check if the pointers are the same
je bfs_queue_end_push
mov rdi, r12 # enqueue node
mov rsi, r13
call enqueue
add r13, tree_size
jmp bfs_queue_push_child
bfs_queue_end_push:
mov eax, DWORD PTR [r12 + 8]
mov edi, DWORD PTR [r12 + 12]
jmp bfs_queue_loop
bfs_queue_return:
mov rdi, r12 # Free queue
call free_queue
add rsp, 36
pop r14
pop r13
pop r12
ret
main:
push r12
push r13
mov rdi, 3
mov rsi, 3
call create_tree
mov r12, rax
mov r13, rdx
mov rdi, rax
mov rsi, rdx
call bfs_queue
mov rdi, r12
mov rsi, r13
mov esi, esi
call free_tree
pop r13
pop r12
ret