-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedListInsertAndDisplay.asm
104 lines (87 loc) · 2.55 KB
/
LinkedListInsertAndDisplay.asm
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
TITLE Linked_List_Insertion_and_Display (Chapter 11, Pr 11) (LinkedListInsertionAndDisplay.asm)
COMMENT !
Student: Rachel Nguyen
Class: CSCI 241
Instructor: Ding
Assignment: Ch11, Linked_List_Insertion_and_Display
Due Date: 5/11/21
Description:
This program implements a singly linked list using
dynamic memory allocation.
!
INCLUDE Irvine32.inc
INCLUDE Macros.inc
NODE STRUCT
intVal SDWORD ?
pNext DWORD ?
NODE ENDS
PNODE TYPEDEF PTR NODE
.data
hHeap HANDLE ?
head NODE <0,0> ; Dummy head
pTailNode PNODE ?
pCurrNode PNODE ?
.code
main PROC
INVOKE GetProcessHeap ; get handle to program's heap
cmp eax, NULL ; failed?
je error ; yes: error msg
mov hHeap, eax ; no: save handle
mov esi, pCurrNode ; ESI = points to current node
mov edi, OFFSET head ; EDI = points to tail node
read:
mWrite "Enter a signed integer node value (zero to end): "
call ReadInt
jz displayList ; zero? yes: display list
call CreateNode ; no: create node
jc error ; failed? yes: error msg
mov (NODE PTR [esi]).intVal, eax ; no: fill node data
mov (NODE PTR [edi]).pNext, esi ; tail->next = curr
mov edi, esi ; tail = curr
jmp read
displayList:
mov (NODE PTR [edi]).pNext, NULL ; tail->next = NULL
mov esi, OFFSET head ; traverse list from head
call crlf
mWriteLn "Contents of Linked List:" ; display prompt
nextNode:
mov ebx, (NODE PTR [esi]).pNext ; save pointer to next node
mov eax, (NODE PTR [esi]).intVal ; save node data
.IF eax == 0 ; dummy head?
mWrite "Dummy Head" ; yes: display
.ELSE
call WriteInt ; no: display contents
INVOKE HeapFree, hHeap, NULL, esi ; free the dynamically allocated node
cmp eax, NULL ; failed?
je error ; yes: error msg
.ENDIF
cmp ebx, NULL ; tail node?
je quit ; yes: quit
mWrite " -> " ; no: display arrow "->"
mov esi, ebx ; point to next node
jmp nextNode
error:
call WriteWindowsMsg ; display error msg
quit:
call crlf
exit
main ENDP
;--------------------------------------------------------
CreateNode PROC uses EAX
;
; Dynamically allocates space to create a node.
; Receives: nothing
; Returns: CF = 0 if allocation succeeds
; CF = 1 if allocation fails
; ESI = pointer to allocated heap (node)
;--------------------------------------------------------
INVOKE HeapAlloc, hHeap, HEAP_ZERO_MEMORY, SIZEOF NODE
.IF eax == NULL
stc ; return with CF = 1
.ELSE
mov esi, eax ; save pointer in current node
clc ; return with CF = 0
.ENDIF
ret
CreateNode ENDP
END main