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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
| .386
.model flat, stdcall
option casemap :none
include E:\masm32\include\windows.inc
include E:\masm32\include\user32.inc
include E:\masm32\include\kernel32.inc
include E:\masm32\include\masm32.inc
includelib E:\masm32\lib\user32.lib
includelib E:\masm32\lib\kernel32.lib
includelib E:\masm32\lib\masm32.lib
.stack 4096
strlen PROTO, pString: PTR BYTE
strcopy PROTO, source:PTR BYTE, target:PTR BYTE
strcomp PROTO, string1:PTR BYTE, string2:PTR BYTE
BOOK STRUCT
author BYTE 80 DUP(0)
bookTitle BYTE 80 DUP(0)
publisher BYTE 80 DUP(0)
ALIGN DWORD
year DWORD 0
BOOK ENDS
AUTHOR STRUCT
author BYTE 80 DUP(0)
numberOfBooks DWORD 0
AUTHOR ENDS
.data
numberOfBooks = 10
ALIGN DWORD
books BOOK <"J.K.Rowling", "Harry Potter And the Sorcerer's Stone", "Scholastic Paperbacks", 1998>
BOOK <"J.K.Rowling", "Harry Potter And The Chamber Of Secrets", "Scholastic Paperbacks", 2000>
BOOK <"J.K.Rowling", "Harry Potter And the Prisoner of Azkaban", "Scholastic Paperbacks", 2001>
BOOK <"J.K.Rowling", "Harry Potter And The Goblet Of Fire", "Scholastic Paperbacks", 2002>
BOOK <"J.K.Rowling", "Harry Potter And The Order Of The Phoenix", "Scholastic Paperbacks", 2004>
BOOK <"J.R.R.Tolkien", "The Fellowship of the Ring", "Mariner Books", 2012>
BOOK <"J.R.R.Tolkien", "The Two Towers", "Mariner Books", 2012>
BOOK <"J.R.R.Tolkien", "The Return of the King", "Mariner Books", 2012>
BOOK <"O.Henry ", "The Lost World", "Dover Publications", 1998>
BOOK <"O.Henry ", "The Hound of the Baskervilles", "Dover Publications", 1994>
ALIGN DWORD
authors AUTHOR numberOfBooks DUP(<>)
numberOfAuthors DWORD 0
szBuffer BYTE 100 DUP(0)
szNumber BYTE 10 DUP(0)
location DWORD 0
.code
main PROC
XOR ESI, ESI
XOR EDI, EDI
MOV ECX, numberOfBooks
outerLoop:
PUSH ECX
XOR EDI, EDI
MOV ECX, numberOfAuthors
CMP ECX, 0
JNE notFirstAuthor
INVOKE strcopy, ADDR books[ESI].author, ADDR authors[EDI].author
INC authors[EDI].numberOfBooks
INC numberOfAuthors
JMP nextBook
notFirstAuthor:
nextAuthor:
INVOKE strcomp, ADDR books[ESI].author, ADDR authors[EDI].author
JE authorsEqual
ADD EDI, TYPE AUTHOR
LOOP nextAuthor
MOV EAX, numberOfAuthors
MOV EBX, TYPE AUTHOR
MUL EBX
PUSH EAX
INVOKE strcopy, ADDR books[ESI].author, ADDR authors[EAX].author
POP EAX
INC authors[EAX].numberOfBooks
INC numberOfAuthors
JMP nextBook
authorsEqual:
INC authors[EDI].numberOfBooks
nextBook:
ADD ESI, TYPE BOOK
POP ECX
DEC ECX
JNZ outerLoop
MOV ECX, numberOfAuthors
XOR ESI, ESI
nextLine:
PUSH ECX
INVOKE szappend, ADDR szBuffer, ADDR authors[ESI].author, 0
MOV location, EAX
LEA EDI, szBuffer
ADD EDI, location
MOV AL, " "
STOSB
INC location
INVOKE dwtoa, authors[ESI].numberOfBooks, ADDR szNumber
INVOKE szappend, ADDR szBuffer, ADDR szNumber, location
MOV location, EAX
LEA EDI, szBuffer
ADD EDI, location
MOV AL, 10
STOSB
INVOKE StdOut, ADDR szBuffer
ADD ESI, TYPE AUTHOR
POP ECX
DEC ECX
JNZ nextLine
INVOKE ExitProcess, 0
main ENDP
strlen PROC USES EDI,
pString: PTR BYTE
MOV EDI, pString
MOV EAX, 0
nextCharacter:
CMP BYTE PTR [EDI], 0
JE endLine
INC EDI
INC EAX
JMP nextCharacter
endLine:
RET
strlen ENDP
strcopy PROC USES eax ecx esi edi,
source:PTR BYTE, target:PTR BYTE
INVOKE strlen, source
MOV ECX, EAX
INC ECX
MOV ESI, source
MOV EDI, target
CLD
REP MOVSB
RET
strcopy ENDP
strcomp PROC USES eax edx esi edi,
string1:PTR BYTE, string2:PTR BYTE
MOV ESI, string1
MOV EDI, string2
nextCharacter:
MOV AL, [ESI]
MOV DL, [EDI]
CMP AL, 0
JNE compare
CMP DL, 0
JNE compare
JMP stringsEquals
compare:
INC ESI
INC EDI
CMP AL, DL
JE nextCharacter
stringsEquals:
RET
strcomp ENDP
END main |