@@ -250,7 +250,7 @@ generate_codebook_recurse:
250250 push rbp
251251 push r12
252252 push r13
253- test rdi , rdi # If we reached a null pointer we're done
253+ test rsi , rsi # If we reached a null pointer we're done
254254 jz generate_codebook_recurse_done
255255 mov r12 , rsi
256256 cmp QWORD PTR [ r12 + tree_left ], 0 # If at least one of the children is not null
@@ -314,7 +314,7 @@ generate_tree_count_chars:
314314generate_tree_leaves_setup:
315315 mov r12 , 255 # The loop counter. We can only get here if the "test" on line 301 resulted in a zero so the next jl instruction will do the right thing
316316generate_tree_leaves:
317- jl generate_tree_branches # If not then it's time to generate the branches
317+ jl generate_tree_one_leaf # If not then it's time to generate the branches
318318 mov r13d , DWORD PTR [ rsp + 4 * r12 ] # Load the count at the ith position
319319 test r13d , r13d # And check if it's zero
320320 jz generate_tree_leaves_counters # If it is we can skip this iteration
@@ -329,6 +329,18 @@ generate_tree_leaves:
329329generate_tree_leaves_counters:
330330 dec r12 # Decrement the loop counter and start over
331331 jmp generate_tree_leaves
332+ generate_tree_one_leaf:
333+ cmp DWORD PTR [ rsp + counts_size ], 1 # Check if there is only one element in the heap
334+ jne generate_tree_branches
335+ lea rdi , [ rsp + counts_size ] # Get the element
336+ call heap_pop
337+ mov r12 , rax
338+ mov rdi , tree_size # Create the new tree node , the pointer to it will be in rax
339+ call malloc
340+ mov QWORD PTR [ rax + tree_left ], r12 # Save element in the left node
341+ mov ecx , DWORD PTR [ r12 + tree_count ] # Save element count in branch
342+ mov DWORD PTR [ rax + tree_count ], ecx
343+ jmp generate_tree_ret # Returning
332344generate_tree_branches:
333345 cmp DWORD PTR [ rsp + counts_size ], 1 # Check if there are still at least two elements in the heap
334346 jle generate_tree_done # If not , we're done
@@ -352,6 +364,7 @@ generate_tree_branches:
352364generate_tree_done:
353365 lea rdi , [ rsp + counts_size ] # The tree's root will be in rax after the pop
354366 call heap_pop
367+ generate_tree_ret:
355368 add rsp , 5128
356369 pop r13
357370 pop r12
@@ -524,3 +537,4 @@ free_tree:
524537free_tree_done:
525538 pop rbx
526539 ret
540+
0 commit comments