-
Notifications
You must be signed in to change notification settings - Fork 1
/
CHESS.ASM
2307 lines (2254 loc) · 71.9 KB
/
CHESS.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
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;***********************************************************
;
; SARGON
;
; Sargon is a computer chess playing program designed
; and coded by Dan and Kathe Spracklen. Copyright 1978. All
; rights reserved. No part of this publication may be
; reproduced without the prior written permission.
;***********************************************************
;***********************************************************
; SYSTEM EQUATES
;***********************************************************
INCHAR EQU 1
PRINTBUF EQU 9
INBUF EQU 10
SYSTEM EQU 5
;***********************************************************
; EQUATES
;***********************************************************
;
PAWN: EQU 1
KNIGHT: EQU 2
BISHOP: EQU 3
ROOK: EQU 4
QUEEN: EQU 5
KING: EQU 6
WHITE: EQU 0
BLACK: EQU 80H
BPAWN: EQU BLACK+PAWN
ORG 100H
PUT 100H
;***********************************************************
; MAIN PROGRAM DRIVER
;***********************************************************
; FUNCTION: -- To coordinate the game moves.
;
; CALLED BY: -- None
;
; CALLS: -- INITBD
; CPTRMV
; PLYRMV
;
; ARGUMENTS: None
;***********************************************************
DRIVER: MOV SP,STACK ; Set stack pointer
MOV DX,CLRMSG ; Request color choice
MOV CL,PRINTBUF
CALL SYSTEM
MOV CL,INCHAR ; Accept response
CALL SYSTEM
CMP AL,57H ; Did player request white ?
JZ DR05 ; Yes - branch
CMP AL,42H ; Did player request black ?
JNZ DRIVER ; No - error
XOR AL,AL
JP DR10 ; Jump
DR05: MOV AL,BLACK ; Set computers color to black
DR10: MOV [KOLOR],AL
DR15: MOV DX,PLYDEP ; Request depth of search
MOV CL,PRINTBUF
CALL SYSTEM
MOV CL,INCHAR
CALL SYSTEM
SUB AL,31H ; Subtract Ascii constant
JB DR15 ; Under minimum - prompt again
CMP AL,6 ; Over maximum of 6 ?
JNB DR15 ; Yes - prompt again
INC AL ; Increment by one
MOV [PLYMAX],AL ; Set desired depth
MOV DX,CRLF ; Output CRLF
MOV CL,PRINTBUF
CALL SYSTEM
XOR AL,AL
MOV [COLOR],AL ; Set color to white
INC AL
MOV [MOVENO],AL ; Set move number to 1
CALL INITBD ; Init board
MOV AL,[KOLOR] ; Get color
OR AL,AL ; Computer move ?
JZ DR25 ; Yes - jump
DR20: CALL PLYRMV ; Do player move
DR25: CALL CPTRMV ; Do computer move
MOV BX,MOVENO
INC B,[BX] ; Increment move number
JP DR20 ; Back to player move
;***********************************************************
; TABLES SECTION
;***********************************************************
ORG 200H ; Start at 200H
START:
ORG START+80H
PUT START+80H
TBASE: EQU START+100H
;**********************************************************
; DIRECT -- Direction Table. Used to determine the dir-
; ection of movement of each piece.
;***********************************************************
DIRECT: EQU $-TBASE
DB +09,+11,-11,-09
DB +10,-10,+01,-01
DB -21,-12,+08,+19
DB +21,+12,-08,-19
DB +10,+10,+11,+09
DB -10,-10,-11,-09
;***********************************************************
; DPOINT -- Direction Table Pointer. Used to determine
; where to begin in the direction table for any
; given piece.
;***********************************************************
DPOINT: EQU $-TBASE
DB 20,16,8,0,4,0,0
;***********************************************************
; DCOUNT -- Direction Table Counter. Used to determine
; the number of directions of movement for any
; given piece.
;***********************************************************
DCOUNT: EQU $-TBASE
DB 4,4,8,4,4,8,8
;***********************************************************
; PVALUE -- Point Value. Gives the point value of each
; piece, or the worth of each piece.
;***********************************************************
PVALUE: EQU $-TBASE-1
DB 1,3,3,5,9,10
;***********************************************************
; PIECES -- The initial arrangement of the first rank of
; pieces on the board. Use to set up the board
; for the start of the game.
;***********************************************************
PIECES: EQU $-TBASE
DB 4,2,3,5,6,3,2,4
;***********************************************************
; BOARD -- Board Array. Used to hold the current position
; of the board during play. The board itself
; looks like:
; FFFFFFFFFFFFFFFFFFFF
; FFFFFFFFFFFFFFFFFFFF
; FF0402030506030204FF
; FF0101010101010101FF
; FF0000000000000000FF
; FF0000000000000000FF
; FF0000000000000060FF
; FF0000000000000000FF
; FF8181818181818181FF
; FF8482838586838284FF
; FFFFFFFFFFFFFFFFFFFF
; FFFFFFFFFFFFFFFFFFFF
; The values of FF form the border of the
; board, and are used to indicate when a piece
; moves off the board. The individual bits of
; the other bytes in the board array are as
; follows:
; Bit 7 -- Color of the piece
; 1 -- Black
; 0 -- White
; Bit 6 -- Not used
; Bit 5 -- Not used
; Bit 4 --Castle flag for Kings only
; Bit 3 -- Piece has moved flag
; Bits 2-0 Piece type
; 1 -- Pawn
; 2 -- Knight
; 3 -- Bishop
; 4 -- Rook
; 5 -- Queen
; 6 -- King
; 7 -- Not used
; 0 -- Empty Square
;***********************************************************
BOARD: EQU $-TBASE
BOARDA: DS 120
;***********************************************************
; ATKLIST -- Attack List. A two part array, the first
; half for white and the second half for black.
; It is used to hold the attackers of any given
; square in the order of their value.
;
; WACT -- White Attack Count. This is the first
; byte of the array and tells how many pieces are
; in the white portion of the attack list.
;
; BACT -- Black Attack Count. This is the eighth byte of
; the array and does the same for black.
;***********************************************************
ATKLST: DW 0,0,0,0,0,0,0
WACT: EQU ATKLST
BACT: EQU ATKLST+7
;***********************************************************
; PLIST -- Pinned Piece Array. This is a two part array.
; PLISTA contains the pinned piece position.
; PLISTD contains the direction from the pinned
; piece to the attacker.
;***********************************************************
PLIST: EQU $-TBASE-1
PLISTD: EQU PLIST+10
PLISTA: DW 0,0,0,0,0,0,0,0,0,0
;***********************************************************
; POSK -- Position of Kings. A two byte area, the first
; byte of which hold the position of the white
; king and the second holding the position of
; the black king.
;
; POSQ -- Position of Queens. Like POSK,but for queens.
;***********************************************************
POSK: DB 24,95
POSQ: DB 14,94
DB -1
;***********************************************************
; SCORE -- Score Array. Used during Alpha-Beta pruning to
; hold the scores at each ply. It includes two
; "dummy" entries for ply -1 and ply 0.
;***********************************************************
SCORE: DW 0,0,0,0,0,0
;***********************************************************
; PLYIX -- Ply Table. Contains pairs of pointers, a pair
; for each ply. The first pointer points to the
; top of the list of possible moves at that ply.
; The second pointer points to which move in the
; list is the one currently being considered.
;***********************************************************
PLYIX: DW 0,0,0,0,0,0,0,0,0,0
DW 0,0,0,0,0,0,0,0,0,0
;***********************************************************
; STACK -- Contains the stack for the program.
;***********************************************************
ORG START+2FFH
STACK:
;***********************************************************
; TABLE INDICES SECTION
;
; M1-M4 -- Working indices used to index into
; the board array.
;
; T1-T3 -- Working indices used to index into Direction
; Count, Direction Value, and Piece Value tables.
;
; INDX1 -- General working indices. Used for various
; INDX2 purposes.
;
; NPINS -- Number of Pins. Count and pointer into the
; pinned piece list.
;
; MLPTRI -- Pointer into the ply table which tells
; which pair of pointers are in current use.
;
; MLPTRJ -- Pointer into the move list to the move that is
; currently being processed.
;
; SCRIX -- Score Index. Pointer to the score table for
; the ply being examined.
;
; BESTM -- Pointer into the move list for the move that
; is currently considered the best by the
; Alpha-Beta pruning process.
;
; MLLST -- Pointer to the previous move placed in the move
; list. Used during generation of the move list.
;
; MLNXT -- Pointer to the next available space in the move
; list.
;
;***********************************************************
ORG START+0
PUT START+0
M1: DW TBASE
M2: DW TBASE
M3: DW TBASE
M4: DW TBASE
T1: DW TBASE
T2: DW TBASE
T3: DW TBASE
INDX1: DW TBASE
INDX2: DW TBASE
NPINS: DW TBASE
MLPTRI: DW PLYIX
MLPTRJ: DW 0
SCRIX: DW 0
BESTM: DW 0
MLLST: DW 0
MLNXT: DW MLIST
;***********************************************************
; VARIABLES SECTION
;
; KOLOR -- Indicates computer's color. White is 0, and
; Black is 80H.
;
; COLOR -- Indicates color of the side with the move.
;
; P1-P3 -- Working area to hold the contents of the board
; array for a given square.
;
; PMATE -- The move number at which a checkmate is
; discovered during look ahead.
;
; MOVENO -- Current move number.
;
; PLYMAX -- Maximum depth of search using Alpha-Beta
; pruning.
;
; NPLY -- Current ply number during Alpha-Beta
; pruning.
;
; CKFLG -- A non-zero value indicates the king is in check.
;
; MATEF -- A zero value indicates no legal moves.
;
; VALM -- The score of the current move being examined.
;
; BRDC -- A measure of mobility equal to the total number
; of squares white can move to minus the number
; black can move to.
;
; PTSL -- The maximum number of points which could be lost
; through an exchange by the player not on the
; move.
;
; PTSW1 -- The maximum number of points which could be won
; through an exchange by the player not on the
; move.
;
; PTSW2 -- The second highest number of points which could
; be won through a different exchange by the player
; not on the move.
;
; MTRL -- A measure of the difference in material
; currently on the board. It is the total value of
; the white pieces minus the total value of the
; black pieces.
;
; BC0 -- The value of board control(BRDC) at ply 0.
;
; MV0 -- The value of material(MTRL) at ply 0.
;
; PTSCK -- A non-zero value indicates that the piece has
; just moved itself into a losing exchange of
; material.
;
; BMOVES -- Our very tiny book of openings. Determines
; the first move for the computer.
;
;***********************************************************
KOLOR: DB 0
COLOR: DB 0
P1: DB 0
P2: DB 0
P3: DB 0
PMATE: DB 0
MOVENO: DB 0
PLYMAX: DB 2
NPLY: DB 0
CKFLG: DB 0
MATEF: DB 0
VALM: DB 0
BRDC: DB 0
PTSL: DB 0
PTSW1: DB 0
PTSW2: DB 0
MTRL: DB 0
BC0: DB 0
MV0: DB 0
PTSCK: DB 0
BMOVES: DB 35,55,10H
DB 34,54,10H
DB 85,65,10H
DB 84,64,10H
;***********************************************************
; MOVE LIST SECTION
;
; MLIST -- A 2048 byte storage area for generated moves.
; This area must be large enough to hold all
; the moves for a single leg of the move tree.
;
; MLEND -- The address of the last available location
; in the move list.
;
; MLPTR -- The Move List is a linked list of individual
; moves each of which is 6 bytes in length. The
; move list pointer(MLPTR) is the link field
; within a move.
;
; MLFRP -- The field in the move entry which gives the
; board position from which the piece is moving.
;
; MLTOP -- The field in the move entry which gives the
; board position to which the piece is moving.
;
; MLFLG -- A field in the move entry which contains flag
; information. The meaning of each bit is as
; follows:
; Bit 7 -- The color of any captured piece
; 0 -- White
; 1 -- Black
; Bit 6 -- Double move flag (set for castling and
; en passant pawn captures)
; Bit 5 -- Pawn Promotion flag; set when pawn
; promotes.
; Bit 4 -- When set, this flag indicates that
; this is the first move for the
; piece on the move.
; Bit 3 -- This flag is set is there is a piece
; captured, and that piece has moved at
; least once.
; Bits 2-0 Describe the captured piece. A
; zero value indicates no capture.
;
; MLVAL -- The field in the move entry which contains the
; score assigned to the move.
;
;***********************************************************
ORG START+300H
PUT START+300H
MLIST: DS 2048
MLEND: EQU MLIST+2040
MLPTR: EQU 0
MLFRP: EQU 2
MLTOP: EQU 3
MLFLG: EQU 4
MLVAL: EQU 5
;***********************************************************
; PROGRAM CODE SECTION
;***********************************************************
; BOARD SETUP ROUTINE
;***********************************************************
; FUNCTION: To initialize the board array, setting the
; pieces in their initial positions for the
; start of the game.
;
; CALLED BY: DRIVER
;
; CALLS: None
;
; ARGUMENTS: None
;***********************************************************
INITBD: MOV CX,60 ; Pre-fill board with -1's
MOV DI,BOARDA
MOV AX,-1
REP
STOW
MOV CH,8
MOV SI,BOARDA
IB2: MOV AL,[SI-8] ; Fill non-border squares
MOV [SI+21],AL ; White pieces
LAHF
OR AL,080H
SAHF ; Change to black
MOV [SI+91],AL ; Black pieces
MOV B,[SI+31],PAWN ; White Pawns
MOV B,[SI+81],BPAWN ; Black Pawns
MOV B,[SI+41],0 ; Empty squares
MOV B,[SI+51],0
MOV B,[SI+61],0
MOV B,[SI+71],0
INC SI
DEC CH
JNZ IB2
MOV SI,POSK ; Init King/Queen position list
MOV B,[SI+0],25
MOV B,[SI+1],95
MOV B,[SI+2],24
MOV B,[SI+3],94
RET
;***********************************************************
; PATH ROUTINE
;***********************************************************
; FUNCTION: To generate a single possible move for a given
; piece along its current path of motion including:
; Fetching the contents of the board at the new
; position, and setting a flag describing the
; contents:
; 0 -- New position is empty
; 1 -- Encountered a piece of the
; opposite color
; 2 -- Encountered a piece of the
; same color
; 3 -- New position is off the
; board
;
; CALLED BY: MPIECE
; ATTACK
; PINFND
;
; CALLS: None
;
; ARGUMENTS: Direction from the direction array giving the
; constant to be added for the new position.
;***********************************************************
PATH: ADD [M2],CL ; Add direction constant to previous position
MOV SI,[M2] ; Load board index
MOV AL,[SI+BOARD] ; Get contents of board
CMP AL,-1 ; In border area ?
JZ PA2 ; Yes - jump
MOV [P2],AL ; Save piece
MOV AH,AL ; Save piece in register
AND AL,7 ; Clear flags
MOV [T2],AL ; Save piece type
JZ RET ; Return if empty
XOR AH,[P1] ; Compare with moving piece
SHL AH ; Set flag
MOV AL,2
SBB AL,0
RET ; Return
PA2: MOV AL,3 ; Set off board flag
RET
;***********************************************************
; PIECE MOVER ROUTINE
;***********************************************************
; FUNCTION: To generate all the possible legal moves for a
; given piece.
;
; CALLED BY: GENMOV
;
; CALLS: PATH
; ADMOVE
; CASTLE
; ENPSNT
;
; ARGUMENTS: The piece to be moved.
;***********************************************************
MPIECE: XOR AL,[BX] ; Piece to move
AND AL,87H ; Clear flag bit
CMP AL,BPAWN ; Is it a black Pawn ?
JNZ MP1 ; No-Skip
DEC AL ; Decrement for black Pawns
MP1: AND AL,7 ; Get piece type
MOV [T1],AL ; Save piece type
MOV DI,[T1] ; Load index to DCOUNT/DPOINT
MOV CH,[DI+DCOUNT] ; Get direction count
MOV AL,[DI+DPOINT] ; Get direction pointer
MOV [INDX2],AL ; Save as index to direct
MOV DI,[INDX2] ; Load index
MP5: MOV CL,[DI+DIRECT] ; Get move direction
MOV AL,[M1] ; From position
MOV [M2],AL ; Initialize to position
MP10: CALL PATH ; Calculate next position
CMP AL,2 ; Ready for new direction ?
JNC MP15 ; Yes - Jump
AND AL,AL ; Test for empty square
PUSHF
MOV AL,[T1] ; Get piece moved
CMP AL,PAWN+1 ; Is it a Pawn ?
JC MP20 ; Yes - Jump
CALL ADMOVE ; Add move to list
POPF
JNZ MP15 ; No - Jump
MOV AL,[T1] ; Piece type
CMP AL,KING ; King ?
JZ MP15 ; Yes - Jump
CMP AL,BISHOP ; Bishop, Rook, or Queen ?
JNC MP10 ; Yes - Jump
MP15: INC DI ; Increment direction index
DEC CH
JNZ MP5 ; Decr. count-jump if non-zerc
MOV AL,[T1] ; Piece type
CMP AL,KING ; King ?
JNZ RET
JMP CASTLE ; Yes - Try Castling
; ***** PAWN LOGIC *****
MP20: MOV AL,CH ; Counter for direction
CMP AL,3 ; On diagonal moves ?
JC MP35 ; Yes - Jump
JZ MP30 ; -or-jump if on 2 square move
POPF
JNZ MP15 ; No - jump
MOV AL,[M2] ; Get "to" position
CMP AL,91 ; Promote white Pawn ?
JNC MP25 ; Yes - Jump
CMP AL,29 ; Promote black Pawn ?
JNC MP26 ; No - Jump
MP25: OR B,[P2],020H ; Set promote flag
MP26: CALL ADMOVE ; Add to move list
INC DI ; Adjust to two square move
DEC CH
TEST B,[P1],008H ; Check Pawn moved flag, has it moved before ?
JZ MP10 ; No - Jump
JMP MP15 ; Jump
MP30: POPF
JNZ MP15 ; No - Jump
MP31: CALL ADMOVE ; Add to move list
JMP MP15 ; Jump
MP35: POPF
JZ MP36 ; Yes - Jump
MOV AL,[M2] ; Get "to" position
CMP AL,91 ; Promote white Pawn ?
JNC MP37 ; Yes - Jump
CMP AL,29 ; Black Pawn promotion ?
JNC MP31 ; No- Jump
MP37: OR B,[P2],020H ; Set promote flag
JP MP31 ; Jump
MP36: CALL ENPSNT ; Try en passant capture
JMP MP15 ; Jump
;***********************************************************
; EN PASSANT ROUTINE
;***********************************************************
; FUNCTION: -- To test for en passant Pawn capture and
; to add it to the move list if it is
; legal.
;
; CALLED BY: -- MPIECE
;
; CALLS: -- ADMOVE
; ADJPTR
;
; ARGUMENTS: -- None
;***********************************************************
ENPSNT: MOV AL,[M1] ; Set position of Pawn
TEST B,[P1],080H ; Check color, is it white ?
JZ EP5 ; Yes - skip
ADD AL,10 ; Add 10 for black
EP5: CMP AL,61 ; On en passant capture rank ?
JC RET ; No - return
CMP AL,69 ; On en passant capture rank ?
JNC RET ; No - return
MOV SI,[MLPTRJ] ; Get pointer to previous move
TEST B,[SI+MLFLG],010H ; First move for that piece ?
JZ RET ; No - return
MOV AL,[SI+MLTOP] ; Get "to" position
MOV [M4],AL ; Store as index to board
MOV SI,[M4] ; Load board index
MOV AL,[SI+BOARD] ; Get piece moved
MOV [P3],AL ; Save it
AND AL,7 ; Get piece type
CMP AL,PAWN ; Is it a Pawn ?
JNZ RET ; No - return
MOV AL,[M4] ; Get "to" position
MOV BX,M2 ; Get present "to" position
SUB AL,[BX] ; Find difference
JNS EP10 ; Positive ? Yes - Jump
NEG AL ; Else take absolute value
EP10: CMP AL,10 ; Is difference 10 ?
JNZ RET ; No - return
OR B,[P2],040H ; Set double move flag
CALL ADMOVE ; Add Pawn move to move list
MOV AL,[M1] ; Save initial Pawn position
MOV [M3],AL
MOV AL,[M4] ; Set "from" and "to" positions
; for dummy move
MOV [M1],AL
MOV [M2],AL
MOV AL,[P3] ; Save captured Pawn
MOV [P2],AL
CALL ADMOVE ; Add Pawn capture to move list
MOV AL,[M3] ; Restore "from" position
MOV [M1],AL
;***********************************************************
; ADJUST MOVE LIST POINTER FOR DOUBLE MOVE
;***********************************************************
; FUNCTION: -- To adjust move list pointer to link around
; second move in double move.
;
; CALLED BY: -- ENPSNT
; CASTLE
; (This mini-routine is not really called,
; but is jumped to to save time.)
;
; CALLS: -- None
;
; ARGUMENTS: -- None
;***********************************************************
ADJPTR: MOV BX,[MLLST] ; Get list pointer
SUB BX,6 ; Back up list pointer
MOV [MLLST],BX ; Save list pointer
MOV [BX],0 ; Zero out link
RET ; Return
;***********************************************************
; CASTLE ROUTINE
;***********************************************************
; FUNCTION: -- To determine whether castling is legal
; (Queen side, King side, or both) and add it
; to the move list if it is.
;
; CALLED BY: -- MPIECE
;
; CALLS: -- ATTACK
; ADMOVE
; ADJPTR
;
; ARGUMENTS: -- None
;***********************************************************
CASTLE:
TEST B,[P1],008H ; Has king moved ?
JNZ RET ; Yes - return
MOV AL,[CKFLG] ; Fetch Check Flag
AND AL,AL ; Is the King in check ?
JNZ RET ; Yes - Return
MOV CX,0FF03H ; Initialize King-side values
CA5: ADD CL,[M1] ; Rook position
MOV AL,CL
MOV [M3],AL ; Store as board index
MOV SI,[M3] ; Load board index
MOV AL,[SI+BOARD] ; Get contents of board
AND AL,7FH ; Clear color bit
CMP AL,ROOK ; Has Rook ever moved ?
JNZ CA20 ; Yes - Jump
MOV AL,CL ; Restore Rook position
JP CA15 ; Jump
CA10: MOV SI,[M3] ; Load board index
MOV AL,[SI+BOARD] ; Get contents of board
AND AL,AL ; Empty ?
JNZ CA20 ; No - Jump
MOV AL,[M3] ; Current position
CMP AL,22 ; White Queen Knight square ?
JZ CA15 ; Yes - Jump
CMP AL,92 ; Black Queen Knight square ?
JZ CA15 ; Yes - Jump
CALL ATTACK ; Look for attack on square
AND AL,AL ; Any attackers ?
JNZ CA20 ; Yes - Jump
MOV AL,[M3] ; Current position
CA15: ADD AL,CH ; Next position
MOV [M3],AL ; Save as board index
MOV BX,M1 ; King position
CMP AL,[BX] ; Reached King ?
JNZ CA10 ; No - jump
SUB AL,CH ; Determine King's position
SUB AL,CH
MOV [M2],AL ; Save it
MOV BX,P2 ; Address of flags
MOV B,[BX],40H ; Set double move flag
CALL ADMOVE ; Put king move in list
MOV BX,M1 ; Addr of King "from" position
MOV AL,[BX] ; Get King's "from" position
MOV [BX],CL ; Store Rook "from" position
SUB AL,CH ; Get Rook "to" position
MOV [M2],AL ; Store Rook "to" position
XOR AL,AL ; Zero
MOV [P2],AL ; Zero move flags
CALL ADMOVE ; Put Rook move in list
CALL ADJPTR ; Re-adjust move list pointer
MOV AL,[M3] ; Restore King position
MOV [M1],AL ; Store
CA20: MOV AL,CH ; Scan Index
CMP AL,1 ; Done ?
JZ RET ; Yes - return
MOV CX,01FCH ; Set Queen-side initial values
JMP CA5 ; Jump
;***********************************************************
; ADMOVE ROUTINE
;***********************************************************
; FUNCTION: -- To add a move to the move list
;
; CALLED BY: -- MPIECE
; ENPSNT
; CASTLE
;
; CALLS: -- None
;
; ARGUMENT: -- None
;***********************************************************
ADMOVE: MOV DX,[MLNXT] ; Addr of next loc in move list
MOV BX,MLEND ; Address of list end
SUB BX,DX ; Calculate difference
JC AM10 ; Jump if out of space
MOV BX,[MLLST] ; Addr of prev. list area
MOV [MLLST],DX ; Save next as previous
MOV [BX],DX ; Store link address
TEST B,[P1],008H ; Has moved piece moved before ?
JNZ AM5 ; Yes - jump
OR B,[P2],010H ; Set first move flag
AM5: XCHG DX,DI ; Address of move area
XOR AX,AX ; Store zero in link address
CLD
STOW
MOV AL,[M1] ; Store "from" move position
STOB
MOV AL,[M2] ; Store "to" move position
STOB
MOV AL,[P2] ; Store move flags/capt. piece
STOB
XOR AL,AL ; Store initial move value
STOB
MOV [MLNXT],DI ; Save address for next move
MOV DI,DX
RET ; Return
AM10: MOV [BX],0 ; Abort entry on table ovflow
RET
;***********************************************************
; GENERATE MOVE ROUTINE
;***********************************************************
; FUNCTION: -- To generate the move set for all of the
; pieces of a given color.
;
; CALLED BY: -- FNDMOV
;
; CALLS: -- MPIECE
; INCHK
;
; ARGUMENTS: -- None
;***********************************************************
GENMOV: CALL INCHK ; Test for King in check
MOV [CKFLG],AL ; Save attack count as flag
MOV DX,[MLNXT] ; Addr of next avail list space
MOV BX,[MLPTRI] ; Ply list pointer index
INC BX ; Increment to next ply
INC BX
MOV [BX],DX ; Save move list pointer
INC BX
INC BX
MOV [MLPTRI],BX ; Save new index
MOV [MLLST],BX ; Last pointer for chain init.
MOV AL,21 ; First position on board
GM5: MOV [M1],AL ; Save as index
MOV SI,[M1] ; Load board index
MOV AL,[SI+BOARD] ; Fetch board contents
AND AL,AL ; Is it empty ?
JZ GM10 ; Yes - Jump
CMP AL,-1 ; Is it a border square ?
JZ GM10 ; Yes - Jump
MOV [P1],AL ; Save piece
MOV BX,COLOR ; Address of color of piece
XOR AL,[BX] ; Test color of piece
TEST AL,080H ; Match ?
JNZ GM10
CALL MPIECE ; Yes - call Move Piece
GM10: MOV AL,[M1] ; Fetch current board position
INC AL ; Incr to next board position
CMP AL,99 ; End of board array ?
JNZ GM5 ; No - Jump
RET ; Return
;***********************************************************
; CHECK ROUTINE
;***********************************************************
; FUNCTION: -- To determine whether or not the
; King is in check.
;
; CALLED BY: -- GENMOV
; FNDMOV
; EVAL
;
; CALLS: -- ATTACK
;
; ARGUMENTS: -- Color of King
;***********************************************************
INCHK: MOV AL,[COLOR] ; Get color
INCHK1: MOV BX,POSK ; Addr of white King position
AND AL,AL ; White ?
JZ IC5 ; Yes - Skip
INC BX ; Addr of black King position
IC5: MOV AL,[BX] ; Fetch King position
MOV [M3],AL ; Save
MOV SI,[M3] ; Load board index
MOV AL,[SI+BOARD] ; Fetch board contents
MOV [P1],AL ; Save
AND AL,7 ; Get piece type
MOV [T1],AL ; Save
;***********************************************************
; ATTACK ROUTINE
;***********************************************************
; FUNCTION: -- To find all attackers on a given square
; by scanning outward from the square
; until a piece is found that attacks
; that square, or a piece is found that
; doesn't attack that square, or the edge
; of the board is reached.
;
; In determining which pieces attack
; a square, this routine also takes into
; account the ability of certain pieces to
; attack through another attacking piece. (For
; example a queen lined up behind a bishop
; of her same color along a diagonal.) The
; bishop is then said to be transparent to the
; queen, since both participate in the
; attack.
;
; In the case where this routine is called
; by CASTLE or INCHK, the routine is
; terminated as soon as an attacker of the
; opposite color is encountered.
;
; CALLED BY: -- POINTS
; PINFND
; CASTLE
; INCHK
;
; CALLS: -- ATKADJ
; ATKOUT
;
; ARGUMENTS: -- None
;***********************************************************
ATTACK: PUSH CX ; Save Register CX
MOV BP,[M3] ; Get piece index
ADD BP,BOARD ; Add board pointer to it
MOV DH,0 ; Init. scan count/flags
MOV SI,DIRECT+TBASE ; Load direction table
MOV AH,KING ; Check for a King
CALL ATKADJ ; Scan adjacent squares
MOV AH,KNIGHT ; Check for a Knight
CALL ATKADJ ; Scan adjacent squares
MOV AH,20H ; Bit 5 set
MOV AL,[BP+11] ; Get piece at +11
AND AL,87H ; Clear flag bit
CMP AL,BPAWN ; Black Pawn ?
JNZ AT5 ; No - jump
OR [BP+11],AH ; Turn on bit 5
AT5: MOV AL,[BP+9] ; Get piece at +9
AND AL,87H ; Clear flag bit
CMP AL,BPAWN ; Black Pawn ?
JNZ AT10 ; No - jump
OR [BP+9],AH ; Turn on bit 5
AT10: MOV AL,[BP-11] ; Get piece at -11
AND AL,87H ; Clear flag bit
CMP AL,PAWN ; White Pawn ?
JNZ AT15 ; No - jump
OR [BP-11],AH ; Turn on bit 5
AT15: MOV AL,[BP-9] ; Get piece at -9
AND AL,87H ; Clear flag bit
CMP AL,PAWN ; White Pawn ?
JNZ AT20 ; No - jump
OR [BP-9],AH ; Turn on bit 5
AT20: MOV SI,DIRECT+TBASE ; Load direction table
MOV AH,BISHOP ; Check for a Bishop
CALL ATKOUT ; Scan outwards
MOV AH,ROOK ; Check for a Rook
CALL ATKOUT ; Scan outwards
XOR AL,AL ; No attackers
POP CX ; Restore CX reg
RET ; Return
;***********************************************************
; ATTACK ADJACENT SCAN ROUTINE
;***********************************************************
; FUNCTION: -- To scan for a specified attacker on adjacent
; squares.
;
; CALLED BY: -- ATTACK
;
; CALLS: -- ATKCHK
;
; ARGUMENTS: -- The piece under attack. The attacker.
;***********************************************************
ATKADJ: MOV CX,8 ; 8 directions to scan
AA5: XCHG AX,BX ; Save AX
LODB ; Get direction byte
CBW ; Convert to word
XCHG AX,BX ; Save direction and restore AX
MOV DI,BP ; Load pointer to piece
ADD DI,BX ; Add direction constant
MOV AL,[DI] ; Get attacking piece
AND AL,7 ; Get piece type
CMP AL,AH ; Same as what we want ?
JNZ AA10 ; No - jump
CALL ATKCHK ; Check and save attacker
AA10: LOOP AA5 ; Go check for next direction
RET ; Return
;***********************************************************
; ATTACK OUTWARD SCAN ROUTINE
;***********************************************************
; FUNCTION: -- To scan for a specified attacker outwards
; from the piece under attack, until found
; or edge of the board is reached.
;
; CALLED BY: -- ATTACK
;
; CALLS: -- ATKCHK
;
; ARGUMENTS: -- The piece under attack. The attacker.
;***********************************************************
ATKOUT: MOV CL,4 ; 4 directions to scan
AO5: MOV DX,0C0H ; Bitmask for color flags
XCHG AX,BX ; Save AX
LODB ; Get direction byte
CBW ; Convert to word
XCHG AX,BX ; Save direction and restore AX
MOV DI,BP ; Load pointer to piece
AO10: ADD DI,BX ; Add direction constant
MOV AL,[DI] ; Get attacking piece
AND AL,27H ; Get piece type and bit 5
JZ AO10 ; No piece, keep going
CMP AL,AH ; Same as what we want ?
JZ AO22 ; Yes - jump