Skip to content

Commit

Permalink
Print array of cnode structs
Browse files Browse the repository at this point in the history
  • Loading branch information
ChillerDragon committed Aug 4, 2024
1 parent 4ec9f62 commit b377245
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/data/logger.asm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ char_double_quote db 0x22
char_comma db ','
char_open_bracket db '['
char_close_bracket db ']'
char_open_curly db '{'
char_close_curly db '}'

s_dbg_rax_digit db "[debug] value of rax is: "
l_s_dbg_rax_digit equ $ - s_dbg_rax_digit
Expand Down
1 change: 1 addition & 0 deletions src/huffman/huffman.asm
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
%include "src/huffman/bubble_sort.asm"
%include "src/huffman/set_bits_r.asm"
%include "src/huffman/construct_tree.asm"
%include "src/huffman/print_structs.asm"
125 changes: 125 additions & 0 deletions src/huffman/print_structs.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
; struct CNode
; {
; unsigned m_Bits;
; unsigned m_NumBits;
;
; unsigned short m_aLeafs[2];
;
; unsigned char m_Symbol;
; }

; HUFF_CNODE_BITS_OFFSET equ 0
; HUFF_CNODE_NUM_BITS_OFFSET equ 4
; HUFF_CNODE_LEAF_0_OFFSET equ 8
; HUFF_CNODE_LEAF_1_OFFSET equ 10
; HUFF_CNODE_SYMBOL_OFFSET equ 12
; HUFF_CNODE_SIZE equ 13

_huff_print_field_symbol:
push rax
push rbp
mov rbp, rsp
sub rsp, 24
mov dword [rbp-24], 'Symb'
mov dword [rbp-20], 'ol: '
mov byte [rbp-16], 0
lea rax, [rbp-24]
print_c_str rax
mov rsp, rbp
pop rbp
pop rax
ret

_huff_print_field_leafs:
push rax
push rbp
mov rbp, rsp
sub rsp, 24
mov dword [rbp-24], 'Leaf'
mov word [rbp-20], 's:'
mov word [rbp-18], 0x0020
lea rax, [rbp-24]
print_c_str rax
mov rsp, rbp
pop rbp
pop rax
ret

_huff_print_field_num_bits:
push rax
push rbp
mov rbp, rsp
sub rsp, 24
mov dword [rbp-24], 'NumB'
mov dword [rbp-20], 'its:'
mov byte [rbp-16], ' '
mov byte [rbp-15], 0
lea rax, [rbp-24]
print_c_str rax
mov rsp, rbp
pop rbp
pop rax
ret

_huff_print_field_bits:
push rax
push rbp
mov rbp, rsp
sub rsp, 20
mov dword [rbp-20], 'bits'
mov word [rbp-16], ': '
mov byte [rbp-14], 0
lea rax, [rbp-20]
print_c_str rax
mov rsp, rbp
pop rbp
pop rax
ret

huff_print_struct_cnode:
; huff_print_struct_cnode [rax]
; rax = pointer to cnode struct
push_registers
; pointer to struct
mov rsi, rax

call print_open_curly

call _huff_print_field_bits
mov rax, [rsi+HUFF_CNODE_BITS_OFFSET]
call print_int32
call print_comma
call print_space

call _huff_print_field_num_bits
mov rax, [rsi+HUFF_CNODE_NUM_BITS_OFFSET]
call print_int32
call print_comma
call print_space

call _huff_print_field_leafs
call print_open_bracket
mov rax, [rsi+HUFF_CNODE_LEAF_0_OFFSET]
call print_int32
call print_comma
call print_space
mov rax, [rsi+HUFF_CNODE_LEAF_1_OFFSET]
call print_int32
call print_close_bracket
call print_comma
call print_space

call _huff_print_field_symbol
mov rax, [rsi+HUFF_CNODE_SYMBOL_OFFSET]
call print_int32

call print_close_curly

pop_registers
ret

huff_print_arr_nodes:
; print_struct_array [array buffer] [element_print_callback] [element size] [array size]
print_struct_array huff_nodes, huff_print_struct_cnode, HUFF_CNODE_SIZE, HUFFMAN_MAX_NODES
ret

80 changes: 80 additions & 0 deletions src/logger.asm
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,61 @@ print_any_int:
print_int_array %1, 4, %2
%endmacro

%macro print_struct_array 4
; print_struct_array [array buffer] [element_print_callback] [element size] [array size]
push_registers
mov rbp, rsp

; allocate 2 64 bit register and for the pointers
; and 2 32 bit register for the integers
sub rsp, 24

; array buffer
mov qword [rbp-24], %1

; print callback
mov dword [rbp-16], %2

; element size
mov dword [rbp-8], %3

; array size
mov dword [rbp-4], %4

call print_open_bracket

; counter
mov rcx, 0

; 64 bit pointer to struct
mov rax, [rbp-24]

%%loop_elements:
inc ecx

; 32 bit element size
mov rdi, 0
mov dword edi, [rbp-8]
call [rbp-16]

; increment pointer by size
add rax, rdi

cmp ecx, [rbp-4]
je %%loop_elements_skip_comma
call print_comma
call print_space
%%loop_elements_skip_comma:

cmp ecx, [rbp-4]
jl %%loop_elements

call print_close_bracket

mov rsp, rbp
pop_registers
%endmacro

print_newline:
push_registers

Expand All @@ -185,6 +240,31 @@ print_newline:
pop_registers
ret

print_open_curly:
push_registers

mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, char_open_curly
mov rdx, 1
syscall

pop_registers
ret

print_close_curly:
push_registers

mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, char_close_curly
mov rdx, 1
syscall

pop_registers
ret


print_comma:
push_registers

Expand Down

0 comments on commit b377245

Please sign in to comment.