forked from nlanglo1/CPE310-final-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CpE.asm
773 lines (699 loc) · 10.3 KB
/
CpE.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
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
Assume cs:code, ds:data, ss:stack
data segment
; All variables defined within the data segment
exitmessage1 db 'You have arrived at floor $'
exitmessage2 db '. Have a nice day!$'
entrymessage db 'Please enter a floor: $'
addfloor db 'Enter another (y/n)? $'
invalidinput db 'Invalid input.$'
errormessage1 db 'Begin$'
errormessage2 db 'Idle$'
direction db ?
location db ?
upfloorcalls db ?
downfloorcalls db ?
insideelevator db ?
elevatorcalled db ?
portB db ?
portC db ?
data ends
stack segment
;Stack size and the top of the stack defined in the stack segment
dw 1000 dup(?)
stacktop:
stack ends
code segment
begin:
mov ax, data
mov ds,ax
mov ax, stack
mov ss,ax
mov sp, offset stacktop
; insert code
;;Possible ERROR?
;push si
;Mov si, offset errormessage1
;Call printmessage
;Call printnewline
;pop si
;;End error check message
;set i/o mode for A
mov Dx,143h
mov Al,02h
out DX,Al
;set all in
mov Dx,140h
mov Al,00h
out DX,Al
;sets Port A to input mode
mov DX, 143h
mov Al,03h
out DX,Al
;set i/o mode for B
mov Dx,143h
mov Al,02h
out DX,Al
;set all out
mov Dx,141h
mov Al,0FFh
out DX,Al
;sets Port B to control mode
mov DX, 143h
mov Al,03h
out DX,Al
mov DX,141h
;clears lights
mov AL,0FFh
out DX,AL
;set i/o mode for C
mov Dx,143h
mov Al,02h
out DX,Al
;set all out
mov Dx,142h
mov Al,0FFh
out DX,Al
;sets Port C to control mode
mov DX, 143h
mov Al,03h
out DX,Al
mov DX,142h
;clears lights
mov AL,0FFh
out DX,AL
;define initial floor at 1 and direction to up
mov direction, 00000001b
mov location, 00000100b
;define initial floor calls to be none
mov upfloorcalls, 11111111b
mov downfloorcalls, 11111111b
mov insideelevator, 00000000b
call updatePortC
call updatePortB
call changefloorlights
;start main program
body:
Jmp idle
endidlemethod:
;;Possible ERROR?
;push si
;Mov si, offset errormessage2
;Call printmessage
;Call printnewline
;pop si
;;End error check message
jmp arrivefloor
endarrivefloormethod:
jmp body
;end main program
;start arrivefloor
arrivefloor:
push ax
push bx
push cx
push dx
mov al, location
mov cl, 02h
ror al, cl
and al, insideelevator
cmp al, 0
jz noone
call exitmessage
noone:
cmp direction, 01b
jnz downcalls
mov al, location
mov cl, 02h
ror al, cl
not al
or al, upfloorcalls
cmp al, 11111111b
je morefloors
call pickuppersonup
jmp morefloors
downcalls:
mov al, location
mov cl, 03h
ror al, cl
not al
or al, downfloorcalls
cmp al, 11111111b
je morefloors
call pickuppersondown
morefloors:
cmp direction, 01b
jnz goingdown
cmp location,1000000b
je top
mov al, location
ror al, 1
mov bl, upfloorcalls
not bl
or bl, insideelevator
and al,bl
cmp al,0
jne moveup
mov al, location
and al,bl
cmp al,0
jne moveup
mov al, location
rol al,1
and al,bl
cmp al,0
jne moveup
mov al, location
mov cl,2h
rol al,cl
and al,bl
cmp al,0
jne moveup
top:
cmp location,100b
je bottom2
mov al, location
mov cl, 03h
ror al, cl
mov bl, upfloorcalls
not bl
or bl, insideelevator
and al,bl
cmp al,0
jne movedown
mov al, location
mov cl, 04h
ror al, cl
and al,bl
cmp al,0
jne movedown
mov al, location
mov cl, 05h
ror al, cl
and al,bl
cmp al,0
jne movedown
mov al, location
mov cl, 06h
ror al, cl
and al,bl
cmp al,0
jne movedown
bottom2:
mov al, location
mov cl, 02h
ror al, cl
not al
or al, downfloorcalls
cmp al, 11111111b
je nodown
mov direction, 00000010b
call updatePortC
call changefloorlights
;;Possible ERROR?
;push si
;Mov si, offset entrymessage
;Call printmessage
;Call printnewline
;pop si
;;End error check message
jmp noone
;;Infinite LOOP cause?
nodown:
mov al, location
mov cl, 03h
ror al, cl
mov bl, downfloorcalls
not bl
rol bl,01h
or bl, insideelevator
and al,bl
cmp al,0
jne godown
mov al, location
mov cl, 04h
ror al, cl
and al,bl
cmp al,0
jne godown
mov al, location
mov cl, 05h
ror al, cl
and al,bl
cmp al,0
jne godown
mov al, location
mov cl, 06h
ror al, cl
and al,bl
cmp al,0
jne godown
pop dx
pop cx
pop bx
pop ax
jmp endarrivefloormethod
godown:
mov direction, 00000010b
call updatePortC
call changefloorlights
jmp noone
goingdown:
cmp location,100b
je bottom
mov al, location
mov cl, 03h
ror al, cl
mov bl, downfloorcalls
not bl
rol bl,01h
or bl, insideelevator
and al,bl
cmp al,0
jne movedown
mov al, location
mov cl, 04h
ror al, cl
and al,bl
cmp al,0
jne movedown
mov al, location
mov cl, 05h
ror al, cl
and al,bl
cmp al,0
jne movedown
mov al, location
mov cl, 06h
ror al, cl
and al,bl
cmp al,0
jne movedown
bottom:
cmp location,1000000b
je top2
mov al, location
ror al, 1
mov bl, downfloorcalls
not bl
rol bl,01h
or bl, insideelevator
and al,bl
cmp al,0
jne moveup
mov al, location
and al,bl
cmp al,0
jne moveup
mov al, location
rol al,1
and al,bl
cmp al,0
jne moveup
mov al, location
mov cl,2h
rol al,cl
and al,bl
cmp al,0
jne moveup
top2:
mov al, location
mov cl, 02h
ror al, cl
not al
or al, upfloorcalls
cmp al, 11111111b
je noup
mov direction, 00000001b
call updatePortC
call changefloorlights
;;Possible ERROR?
;push si
;Mov si, offset entrymessage
;Call printmessage
;Call printnewline
;pop si
;;End error check message
jmp noone
;;Infinite LOOP cause?
noup:
mov al, location
ror al, 1
mov bl, upfloorcalls
not bl
or bl, insideelevator
and al,bl
cmp al,0
jne goup
mov al, location
and al,bl
cmp al,0
jne goup
mov al, location
rol al,1
and al,bl
cmp al,0
jne goup
mov al, location
mov cl, 02h
rol al, cl
and al,bl
cmp al,0
jne goup
pop dx
pop cx
pop bx
pop ax
jmp endarrivefloormethod
goup:
mov direction, 00000001b
call updatePortC
call changefloorlights
jmp noone
moveup:
mov al, direction
not al
mov portC, al
call changefloorlights
mov al, location
rol al,01h
mov location,al
call delay
call delay
call updatePortC
call changefloorlights
call delay
jmp arrivefloor
movedown:
mov al, direction
not al
mov portC, al
call changefloorlights
mov al, location
ror al,01h
mov location,al
call delay
call delay
call updatePortC
call changefloorlights
call delay
jmp arrivefloor
;end arrivefloor
;start pickuppersonup
pickuppersonup:
push ax
push cx
mov al, location
mov cl, 02h
ror al, cl
or upfloorcalls, al
call updatePortB
call changefloorlights
call additionalfloor
pop cx
pop ax
ret
;end pickuppersonup
;start pickuppersondown
pickuppersondown:
push ax
push cx
mov al, location
mov cl, 03h
ror al, cl
or downfloorcalls, al
call updatePortB
call changefloorlights
call additionalfloor
pop cx
pop ax
ret
;end pickuppersondown
;start additionalfloor
additionalfloor:
push ax
push cx
push dx
push si
Mov ah,2
Mov cx,0h
Mov si, offset entrymessage
bac:
Call printmessage
Call printnewline
infloor:
Mov ah,1
Int 21h
call printnewline
call verifyfloorinput
cmp al, "1"
JNE floor2
or insideelevator, 1b
floor2:
cmp al, "2"
JNE floor3
or insideelevator, 10b
floor3:
cmp al, "3"
JNE floor4
or insideelevator, 100b
floor4:
cmp al, "4"
JNE floor5
or insideelevator, 1000b
floor5:
cmp al, "5"
JNE floor6
or insideelevator, 10000b
floor6:
inc cx
cmp cx, 3h
je endaddfloor
askfloor:
Mov si, offset addfloor
mov ah, 2
Call printmessage
Call printnewline
ask:
Mov ah,1
Int 21h
call printnewline
cmp al, "n"
je endaddfloor
cmp al, "y"
mov si, offset entrymessage
mov ah, 2
je bac
Mov si, offset invalidinput
Call printmessage
Call printnewline
break:
jmp askfloor
endaddfloor:
pop si
pop dx
pop cx
pop ax
ret
;end additionalfloor
;start verifyfloorinput
verifyfloorinput:
push ax
push dx
cmp al, "1"
je verified
cmp al, "2"
je verified
cmp al, "3"
je verified
cmp al, "4"
je verified
cmp al, "5"
je verified
Mov si, offset invalidinput
Mov ah, 2
Call printmessage
Call printnewline
notverified:
dec cx
verified:
pop dx
pop ax
ret
;end verifyfloorinput
;start exitmessage
exitmessage:
push ax
push dx
push cx
push si
Mov ah,2
Mov si, offset exitmessage1
Call printmessage
next1:
cmp location, 100b
mov dl,"1"
JE prnt
cmp location, 1000b
mov dl,"2"
JE prnt
cmp location, 10000b
mov dl,"3"
JE prnt
cmp location, 100000b
mov dl,"4"
JE prnt
mov dl,"5"
prnt:
int 21h
Mov si, offset exitmessage2
Call printmessage
Call printnewline
done:
mov al, location
mov cl, 02h
ror al, cl
sub insideelevator, al
pop si
pop cx
pop dx
pop ax
ret
;end exitmessage
;start idle
idle:
push cx
mov cl, 00000000b
mov elevatorcalled, cl
Call delay
mov cl, elevatorcalled
cmp cl, 00000001b
jne idle
pop cx
jmp endidlemethod
;end idle
;start delay
delay:
push BX
push CX
mov ch, 00h
mov cx,32h
mov BX, 0B5Dh
outer:
inner:
Call Poll
pushf
popf
dec BX
cmp BX, 00h
JNE inner
mov BX, 0B5Dh
dec cx
cmp cl, ch
JNE outer
pop CX
pop BX
ret
;end delay
;start Poll
Poll:
push DX
push AX
push BX
push CX
mov DX, 140h
in AL, DX
cmp AL, 11111111b
je continue
mov cl, 00000001b
mov elevatorcalled, cl
mov AH, AL
or AL,11110000b
mov BL, upfloorcalls
and BL, AL
mov upfloorcalls, BL
mov cl, 04h
ror ah, cl
or AH,11110000b
mov BL, downfloorcalls
and BL, AH
mov downfloorcalls, BL
continue:
call updatePortB
call changefloorlights
pop CX
pop BX
pop AX
pop DX
ret
;end Poll
;start changefloorlights
changefloorlights:
push DX
push AX
mov DX, 142h
mov AL, portC
out DX, AL
mov DX, 141h
mov AL, portB
out DX, AL
pop AX
pop DX
ret
;end changefloorlights
;start updatePortC
updatePortC:
push ax
mov al, direction
mov ah, location
add al, ah
not al
mov portC, al
pop ax
ret
;end updatePortC
;start updatePortB
updatePortB:
push ax
push cx
mov ax, 0FFFFh
mov al, downfloorcalls
mov cl, 04h
rol ax, cl
and al, upfloorcalls
mov portB, al
pop cx
pop ax
ret
;end updatePortB
printmessage:
push AX
push DX
mov ah,2h
abc:
Mov dl,[si]
Cmp dl,"$"
je return
INT 21h
Inc si
Jmp abc
return:
pop DX
pop AX
;the above abc loop simply copies the contents of memory
;byte by byte, at the address that si points to.
;It prints these bytes until it reaches the dollar sign and exits
ret
printnewline:
push AX
push DX
mov AH,2h
mov dl,0Dh
int 21h
mov DL,0Ah
int 21h
pop DX
pop AX
ret
; end inserted code
code ends
end begin