-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomNumberGeneratorWithOperations.asm
427 lines (384 loc) · 9.39 KB
/
RandomNumberGeneratorWithOperations.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
TITLE Program Template (template.asm)
; Author: Haris Hambasic
; Description: This program generates a random list of numbers then displays the original list, sorts the list, displays the median value of the list,
; displays the list sorted in ascending order, and finally displays the number of instances of each generated value.
INCLUDE Irvine32.inc
; (insert constant definitions here)
ARRAYSIZE = 200
LO = 15
HI = 50
.data
; (insert variable definitions here)
half BYTE ".5",0
introduction_title BYTE "Generating, Sorting, and Counting Random integers! Programmed by Haris Hambasic.",0
introduction_statement BYTE "This program generates a random list of numbers then displays the original list, sorts the list, displays the median value of the list, displays the list sorted in ascending order, then displays the number of instances of each generated value.",0
goodbye_statement BYTE "Goodbye, and thanks for using this program!",0
unsorted_random_numbers_statement BYTE "Your unsorted random numbers:",0
median_value_of_array_statement BYTE "The median value of the array: ",0
sorted_random_numbers_statement BYTE "Your sorted random numbers:",0
list_of_instances_statement BYTE "Your list of instances of each generated number:",0
space_between BYTE " ",0
randArray BYTE ARRAYSIZE DUP(?)
.code
main PROC
CALL Randomize
; introduce program
PUSH OFFSET introduction_title
CALL introduction
CALL CrLf
CALL CrLf
PUSH OFFSET introduction_statement
CALL introduction
CALL CrLf
CALL CrLf
POP EDX
; populate the array
PUSH OFFSET space_between
PUSH OFFSET randArray
CALL fillArray
POP EDX
POP EDX
; display the array elements
PUSH OFFSET randArray
PUSH OFFSET space_between
PUSH OFFSET unsorted_random_numbers_statement
CALL displayList
POP EDX
POP EDX
POP EDX
; sort the array
PUSH OFFSET randArray
CALL sortList
POP EDX
; displat the median of the array elements
CALL CrLf
CALL CrLf
PUSH OFFSET randArray
PUSH OFFSET median_value_of_array_statement
CALL displayMedian
POP EDX
POP EDX
; display the sorted array elements
CALL CrLf
CALL CrLf
PUSH OFFSET randArray
PUSH OFFSET space_between
PUSH OFFSET sorted_random_numbers_statement
CALL displayList
POP EDX
POP EDX
POP EDX
; additional formatting
CALL CrLf
CALL CrLf
; display the count of each element of the array
PUSH OFFSET randArray
PUSH OFFSET space_between
PUSH OFFSET list_of_instances_statement
CALL countList
POP EDX
POP EDX
POP EDX
; display a goodbye message
PUSH OFFSET goodbye_statement
CALL goodbye
POP EDX
Invoke ExitProcess,0
main ENDP
introduction PROC
; ---------------------------------------------------------------------------------
; Name: introduction
;
; Introduce the user to the program by displaying the title and a short description
; of the program.
;
; Receives:
; - Push the string to display, onto the stack for use as parameter value
;
; Returns: No return value
; ---------------------------------------------------------------------------------
PUSH EBP
MOV EBP, ESP
MOV EDX, [EBP + 8]
CALL WriteString
POP EBP
RET 4
introduction ENDP
goodbye PROC
; ---------------------------------------------------------------------------------
; Name: goodbye
;
; Display a goodbye message to the user and thank them for using your program.
;
; Receives:
; - Push the string to display, onto the stack for use as parameter value
;
; Returns: No return value
; ---------------------------------------------------------------------------------
PUSH EBP
MOV EBP, ESP
CALL CrLf
CALL CrLf
MOV EDX, [EBP + 8]
CALL WriteString
CALL CrLf
RET 4
goodbye ENDP
fillArray PROC
; ---------------------------------------------------------------------------------
; Name: fillArray
;
; Populate the random array with values
;
; Preconditions:
; - A global variable (of array) to hold the random values
;
; Receives:
; - Push the string to display, onto the stack for use as parameter value
;
; Returns:
; - A populated global variable (or array) to hold the random values
; ---------------------------------------------------------------------------------
PUSH EBP
MOV EBP, ESP
MOV ECX, ARRAYSIZE
MOV EDI, [EBP + 8]
PUSH EDI
_generateRandomIntegers:
_generateRandomInteger:
MOV EAX, HI
CALL RandomRange
_isValidInteger:
MOV EBX, LO
CMP EBX, EAX
JG _generateRandomInteger
STOSD
LOOP _generateRandomIntegers
POP EDI
MOV ESI, EDI
POP EBP
_end:
RET 8
fillArray ENDP
exchangeElements PROC
; ---------------------------------------------------------------------------------
; Name: exchangeElements
;
; Swap element positions when sorting an unsorted array
;
; Returns:
; - Sorted sub-array of remaining unsorted array
; ---------------------------------------------------------------------------------
MOV [ESI - 4], EDX
MOV [ESI], EAX
RET
exchangeElements ENDP
sortList PROC
; ---------------------------------------------------------------------------------
; Name: sortList
;
; Sorts an unsorted array
;
; Receives:
; - An array to sort, as a passed argument
;
; Returns:
; - A sorted array
; ---------------------------------------------------------------------------------
PUSH EBP
MOV EBP, ESP
MOV EBX, ARRAYSIZE
_outerLoop:
MOV ESI, [EBP + 8]
MOV ECX, EBX
_innerLoop:
LODSD
MOV EDX, [ESI]
CMP EAX, EDX
JLE _jumpOver
CALL exchangeElements
_jumpOver:
DEC ECX
JNZ _innerLoop
DEC EBX
JNZ _outerLoop
POP EBP
RET 4
sortList ENDP
displayList PROC
; ---------------------------------------------------------------------------------
; Name: displayList
;
; Displays all elements of an array
;
; Preconditions:
; -
;
; Postconditions:
; -
;
; Receives:
; - An array to print/display
; - A statement describing what is being displayed
; - A special formatting string to include spacing between array elements
;
; Returns: N/A
; ---------------------------------------------------------------------------------
PUSH EBP
MOV EBP, ESP
MOV EDX, [EBP + 8]
CALL WriteString
CALL CrLf
MOV ECX, ARRAYSIZE
MOV ESI, [EBP + 16]
_printAllElementsOfArray:
CMP ECX, 10
JLE _printLastRow
PUSH ECX
MOV ECX, 10
_printRow:
LODSD
CALL WriteInt
MOV EDX, [EBP + 12]
CALL WriteString
LOOP _printRow
CALL CrLf
POP ECX
SUB ECX, 9
LOOP _printAllElementsOfArray
_printLastRow:
LODSD
CALL WriteInt
MOV EDX, [EBP + 12]
CALL WriteString
LOOP _printLastRow
POP EBP
RET 12
displayList ENDP
displayMedian PROC
; ---------------------------------------------------------------------------------
; Name: displayMedian
;
; Displays the median value of the array
;
; Preconditions:
; - An already sorted array
;
; Postconditions:
; -
;
; Receives:
; - An array to search
;
; Returns: N/A
; ---------------------------------------------------------------------------------
PUSH EBP
MOV EBP, ESP
MOV EDX, [EBP + 8]
CALL WriteString
MOV ECX, ARRAYSIZE
MOV EAX, ARRAYSIZE
MOV EDX, 0
MOV EBX, 2
DIV EBX
CMP EDX, 0
MOV ESI, [EBP + 12]
MOV EDX, EAX
JG _findMedianOddArraySize
_findMedianEvenArraySize:
LODSD
CMP ECX, EDX
JE _computeMedian
LOOP _findMedianEvenArraySize
_computeMedian:
MOV EBX, EAX ; 1 of 2 middle values is in EAX and ESI points to that value --> move that value into EBX
MOV EAX, [ESI - 8] ; get other middle value
ADD EAX, EBX
MOV EDX, 0
MOV EBX, 2
DIV EBX
CMP EDX, 1
JE _roundUp
CALL WriteInt
JMP _end
_roundUp:
INC EAX
CALL WriteInt
JMP _end
_findMedianOddArraySize:
INC EAX
MOV EDX, EAX
_getMedianOfOddArraySize:
LODSD
CMP EDX, ECX
JE _printMedianOfOddArraySize
LOOP _getMedianOfOddArraySize
_printMedianOfOddArraySize:
CALL WriteInt
_end:
POP EBP
RET 8
displayMedian ENDP
countList PROC
; ---------------------------------------------------------------------------------
; Name: countList
;
; Displays the count of each element in the array
;
; Preconditions:
; - An array of random values
;
; Receives:
; - An array to iterate over and display the count for each value in the array
;
; Returns: N/A
; ---------------------------------------------------------------------------------
PUSH EBP
MOV EBP, ESP
MOV EDI, 0
MOV EDX, [EBP + 8]
CALL WriteString
CALL CrLf
MOV ESI, [EBP + 16]
MOV ECX, ARRAYSIZE
MOV EDX, 0
LODSD
MOV EBX, EAX
INC EDX
_forEach:
CMP ECX, 1
JE _getNextNumber
LODSD
CMP EBX, EAX
JNE _getNextNumber
INC EDX
PUSH EDX
POP EDX
LOOP _forEach
_getNextNumber:
PUSH EAX
MOV EAX, EDX
CMP EDI, 20
JE _addNewRow
_printNumber:
CALL WriteInt
POP EAX
MOV EDX, 1
MOV EBX, EAX
CMP ECX, 1
JLE _end
PUSH EDX
MOV EDX, [EBP + 12]
CALL WriteString
POP EDX
INC EDI
LOOP _forEach
_addNewRow:
CALL CrLf
MOV EDI, 0
JMP _printNumber
_end:
POP EBP
RET 12
countList ENDP
END main