Skip to content

Instantly share code, notes, and snippets.

@JackKell
Created April 19, 2016 02:17
Show Gist options
  • Select an option

  • Save JackKell/398839874abe1b8abfc979af4ebe100b to your computer and use it in GitHub Desktop.

Select an option

Save JackKell/398839874abe1b8abfc979af4ebe100b to your computer and use it in GitHub Desktop.
INCLUDE Irvine32.inc
; The amount of total memory allocated
HEAP_MAX = 400000000
.data
; Node struct
Node STRUCT
; current value of the node
value dword 0
; pointer to the next node in the list
nextPtr dword 0
Node ENDS
; Linked list struct
LinkedList Struct
; pointer to the first node in the list
headPtr dword 0
LinkedList Ends
; User input linked list
aLinkedList LinkedList <>
; The allocated heap handle
hHeap Handle ?
.code
; This function allocates all of the memory based on the given size
MemoryAllocation proc USES ebx ecx edx esi edi MA_size:DWORD
invoke HeapAlloc, hHeap, HEAP_ZERO_MEMORY, MA_size
.IF eax == 0
;mWrite "HeapAlloc failed" ;
.ENDIF
ret
MemoryAllocation endp
; Appends a given value to a given linked list
AppendLinkedList proc All_linkedList:Dword, All_value:Dword
; create stack frame
pushad
mov esi, All_linkedList
mov ecx, All_value
mov ebx, (LinkedList Ptr [esi]).headPtr
; check if no nodes in list
.if ebx == 0
; allocate memory that is the size of a node
invoke MemoryAllocation, SIZEOF Node
; set the head to point to the new node
mov (LinkedList Ptr [esi]).headPtr, eax
; set the value of the node to the given value
mov (Node Ptr [eax]).value, ecx
; set the pointer to the next node to 0 (null)
mov (Node Ptr [eax]).nextPtr, 0
; finish appending
jmp Finish
.endif
; This section is used if there is one or more nodes in the list
NextNode:
; current Node pointer
mov edx, ebx
; next node pointer
mov ebx, (Node Ptr [edx]).nextPtr
.if ebx == 0
; allocate memory for the node
invoke MemoryAllocation, SIZEOF Node
; set the current node.nextpointer to the pointer of the next node
mov (Node Ptr [edx]).nextPtr, eax
; set the value of the new node to the given value
mov (Node Ptr [eax]).value, ecx
; set the pointer of the new node to 0 (null)
mov (Node Ptr [eax]).nextPtr, 0
jmp Finish
.else
jmp NextNode
.endif
Finish:
; Close stack frame
popad
ret
AppendLinkedList endp
; Display all of the values in a linked list
DisplayLinkedList proc DLL_linkedList:Dword
; Create stack frame
pushad
mov esi, DLL_linkedList
mov ebx, (LinkedList Ptr [esi]).headPtr
; Increments through all the nodes in the list to get their values and print them
NextNode:
.if ebx == 0
jmp Finish
.else
; Get the value of the current node
mov eax, (Node Ptr [ebx]).value
call WriteInt
call crlf
; Jump to the next node
mov ebx, (Node Ptr [ebx]).nextPtr
jmp NextNode
.endif
Finish:
popad
ret
DisplayLinkedList endp
main PROC
; Randomize seed
call Randomize
; Initialize the heap
invoke HeapCreate, 0, HEAP_ZERO_MEMORY, HEAP_MAX
.if eax == 0
call WriteWindowsMsg
jmp quit
.else
mov hHeap, eax
.endif
; Get user input
GetInput:
call ReadInt
.if eax != 0
invoke AppendLinkedList, offset aLinkedList, eax
jmp GetInput
.endif
invoke DisplayLinkedList, offset aLinkedList
Quit:
call ReadChar
exit
main ENDP
END main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment