aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Cunningham <cc@localhost>2024-07-14 10:52:51 -0700
committerChristian Cunningham <cc@localhost>2024-07-14 10:52:51 -0700
commit5b0f04f0c5d406def1acbcdbcc114e9b4bf5c7da (patch)
treee75b1ef3878d279ce50baaaaa43a993e05d90474
parentf4c8ec9028f19fe334ecd4ae49521011fcdba012 (diff)
Double Linked List
-rw-r--r--linked_list.inc62
-rw-r--r--main.asm43
2 files changed, 103 insertions, 2 deletions
diff --git a/linked_list.inc b/linked_list.inc
index 4fbb923..0e5042b 100644
--- a/linked_list.inc
+++ b/linked_list.inc
@@ -20,19 +20,62 @@ endstruc
mov qword [rax + ll_next], 0
%endmacro
-%macro dlln_alloc 0
+%macro dll_alloc 0
alloc DoublyLinkedListNode_size
mov qword [rax + dll_next], 0
+ mov qword [rax + dll_prev], 0
%endmacro
%macro lln_free 0-1 rax
free %1, LinkedListNode_size
%endmacro
-%macro dlln_free 0-1 rax
+%macro dll_free 0-1 rax
free %1, DoublyLinkedListNode_size
%endmacro
+%macro ll_seek 0-1 rax
+ mov rax, [%1 + ll_next]
+%endm
+
+_dll_seek_backward:
+ mov rax, [rax + dll_prev]
+ ret
+
+_dll_seek_forward:
+ mov rax, [rax + dll_next]
+ ret
+
+%macro dll_seek 0-2 rax,"forward"
+%if "%1" != "%rax"
+ mov rax, %1
+%endif
+%if %2 == "forward"
+ call _dll_seek_forward
+%elif %2 == "backward"
+ call _dll_seek_backward
+%else
+ %error "Direction not specified!"
+%endif
+%endm
+
+_dll_end:
+.loop:
+ mov rcx, [rax + dll_next]
+ cmp rcx, 0
+ je .end
+ call _dll_seek_forward
+ jmp .loop
+.end:
+ ret
+
+%macro dll_end 0-1 rax
+%if "%1" != "rax"
+ mov rax, %1
+%endif
+ call _dll_end
+%endm
+
%macro ll_push 2
;; %1 = Current Linked List Node
;; %2 = Value to push (Must fit in a register, larger must be pushed as pointer to the underlying structure)
@@ -41,4 +84,19 @@ endstruc
mov qword [rax + ll_value], %2
%endm
+%macro dll_push 2
+ ;; %1 = Current Doubly Linked List Node
+ ;; %2 = Value to push (Must fit in a register, larger must be pushed as pointer to the underlying structure)
+ dll_alloc
+ mov rcx, [%1 + dll_next]
+ cmp rcx, 0
+ je %%skip
+ mov qword [rcx + dll_prev], rax
+%%skip:
+ mov qword [%1 + dll_next], rax
+ mov qword [rax + dll_next], rcx
+ mov qword [rax + dll_value], %2
+ mov qword [rax + dll_prev], %1
+%endm
+
%endif
diff --git a/main.asm b/main.asm
index 66f73f6..77d214b 100644
--- a/main.asm
+++ b/main.asm
@@ -110,6 +110,49 @@ _main:
pop rax
lln_free
+ dll_alloc
+ push rax
+ mov rbx, rax
+ mov r12, 0xDEADBEEF
+ dll_push rbx, r12
+ push rax
+ mov rbx, [rsp+8]
+ mov r12, 0xCAFEBABE
+ dll_push rbx, r12
+ push rax
+
+ mov rbx, [rsp+16]
+
+ mov rax, rbx
+ mov rax, [rax + dll_value]
+ REGD rax
+
+ mov rax, rbx
+ dll_seek
+ mov rax, [rax + dll_value]
+ REGD rax
+
+ mov rax, rbx
+ dll_seek
+ dll_seek
+ mov rax, [rax + dll_value]
+ REGD rax
+
+ mov rax, rbx
+ dll_end
+ mov rax, [rax + dll_value]
+ REGD rax
+
+ pop rax
+ pop rax
+ pop rax
+ dll_end
+.free_loop:
+ mov rbx, [rax + dll_prev]
+ dll_free
+ mov rax, rbx
+ cmp rbx, 0
+ jne .free_loop
exit_prog