-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment3.asm
More file actions
621 lines (488 loc) · 10.6 KB
/
Assignment3.asm
File metadata and controls
621 lines (488 loc) · 10.6 KB
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
; Timmy Glynn
; Program 3
include Irvine32.inc
.data
beginProgram db "Enter '1' to begin", 0ah, 0dh, 0
break db "......................................................", 0ah, 0dh, 0 ;makes the table look clean
;list the steps so the user understands what tasks are being performed and in what order
step1 db "Step 1: Read 10 integers and store them in an array ", 0ah, 0dh, 0
step2 db "Step 2: Reverse the array and shift the elements once to the left ", 0ah, 0dh, 0
step3 db "Step 3: Insert 42 at random position ", 0ah, 0dh, 0
step4 db "Step 4: Delete the 5th element ", 0ah, 0dh, 0
step5 db "Step 5: Calculate the sum ", 0ah, 0dh, 0
step6 db "Step 6: Sort the array", 0ah, 0dh, 0
step7 db "Step 7: Search for a key ", 0ah, 0dh, 0
;enter the 10 integers for the array
enterNums db "Enter 10 elements", 0ah, 0dh, 0
;explain what each array is before printing the arrays
PrintArray1 db "The 10 integer array is: ", 0ah, 0dh, 0
PrintArray2 db "The reversed and shifted array is: ", 0ah, 0dh, 0
PrintArray3 db "The array with 42 inserted randomly is: ", 0ah, 0dh, 0
PrintArray4 db "The array with 5th element deleted is: ", 0ah, 0dh, 0
PrintArray5 db "The 2D array is: ", 0ah, 0dh, 0
PrintArray6 db "The sorted array is: ", 0ah, 0dh, 0
;integer declarations
int1 dword ?
int2 dword ?
int3 dword ?
int4 dword ?
int5 dword ?
int6 dword ?
int7 dword ?
int8 dword ?
int9 dword ?
int10 dword ?
randval dword ?
;data for the sums in the 2D array proc (task5)
sum1 dword ?
sum2 dword ?
sum3 dword ?
sum4 dword ?
sum5 dword ?
rowSum db " The sum of this row is: ", 0
;data for the key search proc (task7)
first dword 0
last dword 4
mid dword ?
key dword ?
keyEnter db "Enter the key ", 0ah, 0dh, 0
keyFound db "the key is found at position: ", 0
keyNotFound db "the key wasnt found ", 0ah, 0dh, 0
;counters for each task - unnecessary but it helped me understand better
maincount dword 1
task1count dword 10
task2count dword 10
task3count dword 10
task4count dword 1
task5count dword 1
task6count dword 4
;other data useful for array manipulation
flag dword 0
space db " ", 0
num dword 0
;arrays
array1 Dword 10 DUP (?)
array2 Dword 10 DUP (?)
array3 Dword 10 DUP (?)
array4 Dword 5 DUP (?)
;restart string
askRestart db "Enter '2' if you want to restart ", 0ah, 0dh, 0
.code
main proc
mov edx, offset break
call writeString
mov edx, offset step1
call writeString
mov edx, offset step2
call writeString
mov edx, offset step3
call writeString
mov edx, offset step4
call writeString
mov edx, offset step5
call writeString
mov edx, offset step6
call writeString
mov edx, offset step7
call writeString
mov edx, offset break
call writeString
mov edx, offset beginProgram
call writeString
call readChar
cmp al, '1' ;start the program by entering 1
je main1
jmp stop
main1:
call task1 ;call first step
call crlf
jmp stop
stop:
mov edx, offset askRestart
call writeString
call readChar
cmp al, '2'
mov eax, 1
mov maincount, eax
je main ;repeat the program if user enters 2
exit
main endp
task1 proc
mov edx, offset enterNums
call writeString
mov ecx, task1count
mov esi, offset array1 ;reads numbers and stores in array1
task1Start1:
call readDec
mov [esi], eax
add esi, type array1
loop task1Start1
mov esi, offset array1
mov ecx, task1count
mov edx, offset PrintArray1
call writeString
task1Start2:
mov eax, [esi]
call writeDec
mov edx, offset space
call writeString
add esi, type array1
Loop task1Start2
call crlf
call task2 ;go to task2
ret
task1 endp
task2 proc
call crlf
mov esi, offset array1
mov ecx, task2count
task2Start1:
mov eax, [esi]
push eax
add esi, type array1
loop task2Start1
mov ecx, task2count
mov esi, offset array2
task2Start2:
pop eax
mov [esi], eax
add esi, type array2
loop task2Start2
mov ecx, task2count
mov esi, offset array2 ;declare array2
mov eax, [esi]
shl eax, 1 ;shift each integer to the left one time
mov int1, eax
add esi, type array2 ;move it into array2 .....
mov eax, [esi]
shl eax, 1
mov int2, eax
add esi, type array2
mov eax, [esi]
shl eax, 1
mov int3, eax
add esi, type array2
mov eax, [esi]
shl eax, 1
mov int4, eax
add esi, type array2
mov eax, [esi]
shl eax, 1
mov int5, eax
add esi, type array2
mov eax, [esi]
shl eax, 1
mov int6, eax
add esi, type array2
mov eax, [esi]
shl eax, 1
mov int7, eax
add esi, type array2
mov eax, [esi]
shl eax, 1
mov int8, eax
add esi, type array2
mov eax, [esi]
shl eax, 1
mov int9, eax
add esi, type array2
mov eax, [esi]
shl eax, 1
mov int10, eax
add esi, type array2 ;... finish shifting
mov esi, offset array3 ;declare array3
mov eax, int1 ;insert each integer into array3...
mov [esi], eax
add esi, type array3
mov eax, int2
mov [esi], eax
add esi, type array3
mov eax, int3
mov [esi], eax
add esi, type array3
mov eax, int4
mov [esi], eax
add esi, type array3
mov eax, int5
mov [esi], eax
add esi, type array3
mov eax, int6
mov [esi], eax
add esi, type array3
mov eax, int7
mov [esi], eax
add esi, type array3
mov eax, int8
mov [esi], eax
add esi, type array3
mov eax, int9
mov [esi], eax
add esi, type array3
mov eax, int10
mov [esi], eax ;... finish inserting
mov esi, offset array3
mov ecx, task2count
mov edx, offset PrintArray2
call writeString
task2Start3:
mov eax, [esi]
call writeDec
mov edx, offset space
call writeString
add esi, type array3
loop task2Start3
call crlf
call task3 ;go to task3
ret
task2 endp
task3 proc ;THIS WAS WORKING FINE, IDK WHY IT DOESNT NOW...??????
call randProc ;calls randomizing procedure for random place in array
mov ecx, task3count
mov esi, offset array1
task3Start1:
mov eax, randval ;randval from randProc
cmp eax, task3count ;if counter=randval then it will insert 42 into that pos
je set
inc task3count
add esi, type array1
loop task3Start1
begin:
mov esi, offset array1
mov ecx, task3count
mov edx, offset PrintArray3
call writeString
task3Start2:
mov eax, [esi]
mov edx, offset space
call writeString
call writeDec
add esi, type array1
loop task3Start2
jmp task3Stop1
set:
mov eax, 42 ;insert 42 into the array at randval!
mov [esi], eax
jmp begin
task3Stop1:
call crlf
call task4
ret
task3 endp
randProc proc ;random proc (taken from class notes)
call Randomize
mov eax, 10
call RandomRange
inc eax
mov randval, eax
call crlf
ret
randProc endp
task4 proc
call crlf
mov task4count, 1
mov ecx, 4
mov esi, offset array2
mov edx, offset PrintArray4
call writeString
task4Start1:
mov eax, [esi]
mov edx, offset space
call writeString
call writeDec
add esi, type array2
loop task4Start1
add esi, type array2
mov ecx, 5
task4Start2:
mov eax, [esi]
mov edx, offset space
call writeString
call writeDec
add esi, type array2
loop task4Start2
call crlf
call task5 ;go to task5
ret
task4 endp
task5 proc ;mostly taken from class notes from 2D array program
call crlf
mov edx, offset PrintArray5
call writeString
mov esi, offset array2
mov ecx, 5 ;outer loop
task5L1:
push ecx ;push it into the stack
mov ecx, 2 ;inner loop
task5L2:
mov eax, [esi]
call writeDec
add num, eax
mov eax, " "
call writeChar
add esi, type array2
loop task5L2
mov eax, task5count
cmp eax, 1
je check1
cmp eax, 2
je check2
cmp eax, 3
je check3
cmp eax, 4
je check4
cmp eax, 5
je check5
jmp continue
continue :
inc task5count
pop ecx
loop task5L1
jmp finish
check1 : ;calculate the sums
mov eax, num
mov edx, offset rowSum
call writeString
call writeDec
call crlf
mov sum1, eax
mov num, 0
jmp continue
check2:
mov eax, num
mov edx, offset rowSum
call writeString
call writeDec
call crlf
mov sum2, eax
mov num, 0
jmp continue
check3:
mov eax, num
mov edx, offset rowSum
call writeString
call writeDec
call crlf
mov sum3, eax
mov num, 0
jmp continue
check4:
mov eax, num
mov edx, offset rowSum
call writeString
call writeDec
call crlf
mov sum4, eax
mov num, 0
jmp continue
check5:
mov eax, num
mov edx, offset rowSum
call writeString
call writeDec
call crlf
mov sum5, eax
mov num, 0
jmp continue
finish:
mov esi, offset array4
mov eax, sum1 ;move the sums into an array (array4)
mov [esi], eax
add esi, type array4 ;increment the index by its type (for each sum)
mov eax, sum2
mov [esi], eax
add esi, type array4
mov eax, sum3
mov [esi], eax
add esi, type array4
mov eax, sum4
mov [esi], eax
add esi, type array4
mov eax, sum5
mov [esi], eax
call crlf
call task6
ret
task5 endp
task6 proc ;from bsort program in class
while1: ;while loop - set pointer to top of stack
mov esi, offset array4
cmp flag, 0
jne done
mov flag, 1 ;if flag is != 0, while loop stops
mov ecx, task6count ;for loop - ecx is loop counter, this tells how many times to loop
task6Start1:
mov al, [esi] ;the value that esi is pointing to goes into al
cmp al, [esi + 1] ;compare whatever is in esi to whatever is next (cant cmp [esi], [esi+1] mem to mem)
jb doxchg
jmp go
doxchg:
xchg al, [esi + 1] ;exchange numbers if they are not
mov [esi], al
mov flag, 0 ;if there is exchange, the value of flag will be 0 and loop will be executed
go: ;no exchange
add esi, type array4 ;add esi 1 - increment index
loop task6Start1
dec task6count ;decrement counter so we dont keep looping
jmp while1
done:
mov edx, offset PrintArray6
call writeString
mov ecx, 5
mov esi, offset array4
task6L1:
mov eax, 0
mov al, [esi]
call writeDec
mov eax, " " ;space in between numbers
call writeChar
add esi, type array4
loop task6L1
task6Stop1:
call task7
ret
task6 endp
task7 proc
mov edx, offset keyEnter
call writeString
call readDec
mov key, eax ;enter the key
mov first, 0
mov last, 4
task7Start1:
mov eax, first
cmp eax, last
jg notfound ;if first > last, key is not found
add eax, last ;first + last
shr eax, 1 ;divide eax by 2, (first + last)/2
mov mid, eax
mov esi, mid
shl esi, 2 ;scale mid value
mov eax, esi
mov ecx, key
cmp ecx, array4[esi]
je found
jl changefirst
jg changelast
changelast:
mov edx, mid ;last = mid-1
mov last, edx
dec last
jmp task7Start1
changefirst:
mov edx, mid ;first = mid+1
mov first, edx
inc first
jmp task7Start1
found:
mov edx, offset keyFound
call writeString
mov eax, mid ;if the key is found, it is in mid
call writeDec
jmp stop
notfound:
mov edx, offset keyNotFound
call writeString
stop:
ret
task7 endp
end main