Updated CHESS

Properly labelled and commented ATTACK and its subroutines. There is no guarantee of the correctness of the comments, because I don't know how to play chess.
This commit is contained in:
Broken Pipe
2024-03-17 12:21:08 +00:00
parent 0d2baf7567
commit 8f9e0a2984
+232 -160
View File
@@ -870,180 +870,252 @@ IC5: MOV AL,[BX] ; Fetch King position
AND AL,7 ; Get piece type AND AL,7 ; Get piece type
MOV [T1],AL ; Save 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.
; ;
; *** NOTE FROM BROKEN PIPE *** ; 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.
; ;
; THIS ATTACK ROUTINE DOES NOT MATCH SARGON'S ATTACK ROUTINE, ; In the case where this routine is called
; IT IS MOST LIKELY TIM PATERSON'S OWN IMPLEMENTATION. ; by CASTLE or INCHK, the routine is
; terminated as soon as an attacker of the
; opposite color is encountered.
; ;
; I DO NOT KNOW HOW TO PLAY CHESS, SO I CANNOT UNDERSTAND ANY ; CALLED BY: -- POINTS
; OF THE CODE. I NEED SOMEONE EXPERIENCED IN CHESS AND CHESS ; PINFND
; PROGRAMMING TO DISASSEMBLE THIS ROUTINE AND ITS SUBROUTINES. ; CASTLE
; INCHK
; ;
ATTACK: PUSH CX ; CALLS: -- ATKADJ
MOV BP,[M3] ; ATKOUT
ADD BP,BOARD ;
MOV DH,0 ; ARGUMENTS: -- None
MOV SI,DIRECT+TBASE ;***********************************************************
MOV AH,6 ATTACK: PUSH CX ; Save Register CX
CALL L102D MOV BP,[M3] ; Get piece index
MOV AH,2 ADD BP,BOARD ; Add board pointer to it
CALL L102D MOV DH,0 ; Init. scan count/flags
MOV AH,32 MOV SI,DIRECT+TBASE ; Load direction table
MOV AL,[BP+11] MOV AH,KING ; Check for a King
AND AL,87H CALL ATKADJ ; Scan adjacent squares
CMP AL,81H MOV AH,KNIGHT ; Check for a Knight
JNZ L0FF8 CALL ATKADJ ; Scan adjacent squares
OR [BP+11],AH MOV AH,20H ; Bit 5 set
L0FF8: MOV AL,[BP+9] MOV AL,[BP+11] ; Get piece at +11
AND AL,87H AND AL,87H ; Clear flag bit
CMP AL,81H CMP AL,BPAWN ; Black Pawn ?
JNZ L1004 JNZ AT5 ; No - jump
OR [BP+9],AH OR [BP+11],AH ; Turn on bit 5
L1004: MOV AL,[BP-11] AT5: MOV AL,[BP+9] ; Get piece at +9
AND AL,87H AND AL,87H ; Clear flag bit
CMP AL,1 CMP AL,BPAWN ; Black Pawn ?
JNZ L1010 JNZ AT10 ; No - jump
OR [BP-11],AH OR [BP+9],AH ; Turn on bit 5
L1010: MOV AL,[BP-9] AT10: MOV AL,[BP-11] ; Get piece at -11
AND AL,87H AND AL,87H ; Clear flag bit
CMP AL,1 CMP AL,PAWN ; White Pawn ?
JNZ L101C JNZ AT15 ; No - jump
OR [BP-9],AH OR [BP-11],AH ; Turn on bit 5
L101C: MOV SI,DIRECT+TBASE AT15: MOV AL,[BP-9] ; Get piece at -9
MOV AH,3 AND AL,87H ; Clear flag bit
CALL L1046 CMP AL,PAWN ; White Pawn ?
MOV AH,4 JNZ AT20 ; No - jump
CALL L1046 OR [BP-9],AH ; Turn on bit 5
XOR AL,AL AT20: MOV SI,DIRECT+TBASE ; Load direction table
POP CX MOV AH,BISHOP ; Check for a Bishop
RET 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
L102D: MOV CX,8 ;***********************************************************
L1030: XCHG AX,BX ; ATTACK ADJACENT SCAN ROUTINE
LODB ;***********************************************************
CBW ; FUNCTION: -- To scan for a specified attacker on adjacent
XCHG AX,BX ; squares.
MOV DI,BP ;
ADD DI,BX ; CALLED BY: -- ATTACK
MOV AL,[DI] ;
AND AL,7 ; CALLS: -- ATKCHK
CMP AL,AH ;
JNZ L1043 ; ARGUMENTS: -- The piece under attack. The attacker.
CALL L107C ;***********************************************************
L1043: LOOP L1030 ATKADJ: MOV CX,8 ; 8 directions to scan
RET 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
L1046: MOV CL,4 ;***********************************************************
L1048: MOV DX,0C0H ; ATTACK OUTWARD SCAN ROUTINE
XCHG AX,BX ;***********************************************************
LODB ; FUNCTION: -- To scan for a specified attacker outwards
CBW ; from the piece under attack, until found
XCHG AX,BX ; or edge of the board is reached.
MOV DI,BP ;
L1051: ADD DI,BX ; CALLED BY: -- ATTACK
MOV AL,[DI] ;
AND AL,27H ; CALLS: -- ATKCHK
JZ L1051 ;
CMP AL,AH ; ARGUMENTS: -- The piece under attack. The attacker.
JZ L106D ;***********************************************************
CMP AL,5 ATKOUT: MOV CL,4 ; 4 directions to scan
JZ L1068 AO5: MOV DX,0C0H ; Bitmask for color flags
CMP AL,33 XCHG AX,BX ; Save AX
JZ L106A LODB ; Get direction byte
L1065: LOOP L1048 CBW ; Convert to word
RET XCHG AX,BX ; Save direction and restore AX
L1068: INC DH MOV DI,BP ; Load pointer to piece
L106A: AND B,[DI],0DFH AO10: ADD DI,BX ; Add direction constant
L106D: MOV AL,[DI] MOV AL,[DI] ; Get attacking piece
ADD AL,128 AND AL,27H ; Get piece type and bit 5
RCR AL JZ AO10 ; No piece, keep going
AND DL,AL CMP AL,AH ; Same as what we want ?
JZ L1065 JZ AO22 ; Yes - jump
CALL L107C CMP AL,QUEEN ; Queen ?
JP L1051 JZ AO20 ; Yes - jump
CMP AL,PAWN+20H ; Pawn with bit 5 ?
JZ AO21 ; Yes - jump
AO15: LOOP AO5 ; Go check for next direction
RET ; Return
AO20: INC DH ; Set Queen found flag
AO21: AND B,[DI],0DFH ; Remove bit 5 for piece
AO22: MOV AL,[DI] ; Get piece
ADD AL,80H ; CY if back, NC if white
RCR AL ; Bit 7 set if black, bit 6 if white
AND DL,AL ; Keep only bits 6 and 7
JZ AO15 ; Nothing, next direction
CALL ATKCHK ; Check and save attack
JP AO10 ; Keeping going that direction
L107C: MOV AL,[DI] ;***********************************************************
CMP B,[T1],7 ; ATTACK CHECK AND SAVE ROUTINE
JZ L10B3 ;***********************************************************
XOR AL,[BP] ; FUNCTION: -- To check if a piece can attack the target
JNS RET ; square and save the attacking piece value
; in the attack list.
;
; The pin piece list is checked for the
; attacking piece, and if found there, the
; piece is not included in the attack list.
;
; In the case where ATTACK is called by
; CASTLE or INCHK, control is returned to
; the aforementioned routines as soon as an
; attacker of the opposite color is encountered.
;
; CALLED BY: -- ATTACK
;
; CALLS: -- None
;
; ARGUMENTS: -- The attacking piece. The piece under attack.
;***********************************************************
ATKCHK: MOV AL,[DI] ; Get piece
CMP B,[T1],7 ; Call from POINTS ?
JZ ACK10 ; Yes - jump
XOR AL,[BP] ; Attacker and target same color ?
JNS RET ; Yes - return
POP AX POP AX
POP AX POP AX ; Set SP level to ATTACK
MOV AX,0DFFFH MOV AX,0DFFFH ; FF (border) in AL and DF in AH
CMP [BP+9],AL CMP [BP+9],AL ; Is +9 border ?
JZ L1097 JZ ACK5 ; Yes, jump
AND [BP+9],AH AND [BP+9],AH ; Remove bit 5 for piece
L1097: CMP [BP+11],AL ACK5: CMP [BP+11],AL ; Is +11 border ?
JZ L109F JZ ACK6 ; Yes - jump
AND [BP+11],AH AND [BP+11],AH ; Remove bit 5 for piece
L109F: CMP [BP-9],AL ACK6: CMP [BP-9],AL ; Is -9 border ?
JZ L10A7 JZ ACK7 ; Yes - jump
AND [BP-9],AH AND [BP-9],AH ; Remove bit 5 for piece
L10A7: CMP [BP-11],AL ACK7: CMP [BP-11],AL ; Is -11 border ?
JZ L10AF JZ ACK8 ; Yes - jump
AND [BP-11],AH AND [BP-11],AH ; Remove bit 5 for piece
L10AF: MOV AL,1 ACK8: MOV AL,1 ; Set attacker found flag
POP CX POP CX ; Restore CX reg
RET RET ; Return from ATTACK
L10B3: PUSH CX ACK10: PUSH CX ; Save regs
PUSH DI PUSH DI
MOV CL,[NPINS] MOV CL,[NPINS] ; Number of pinned pieces
JCXZ L10E0 JCXZ ACK26 ; No pins - jump
PUSH AX PUSH AX ; Save AX
MOV AX,DI MOV AX,DI ; Load piece pointer in AX
SUB AX,BOARDA SUB AX,BOARDA ; Minus BOARDA for position
MOV DI,PLISTA MOV DI,PLISTA ; Pin list address
L10C4: OR AL,AL ACK15: OR AL,AL ; Set flags for REP
REPE REPZ
SCAB SCAB ; Search list for position
JNZ L10DF JNZ ACK25 ; Continue if not found
OR AH,AH OR AH,AH ; Is this the first find ?
JNZ L10DB JNZ ACK20 ; No - jump
MOV AH,[DI+9] MOV AH,[DI+9] ; Get direction
CMP AH,BL CMP AH,BL ; Same as attacking direction ?
JZ L10C4 JZ ACK15 ; Yes - jump
NEG AH NEG AH ; Opposite direction ?
CMP AH,BL CMP AH,BL ; Same as attacking direction ?
JZ L10C4 JZ ACK15 ; Yes - jump
L10DB: POP AX ACK20: POP AX ; Restore regs.
POP DI POP DI
POP CX POP CX
RET RET ; Return to ATTACK
L10DF: POP AX ACK25: POP AX ; Restore AX
L10E0: MOV CL,AL ACK26: MOV CL,AL ; Put piece in CL
AND CL,7 AND CL,7 ; Get piece type
MOV DI,PVALUE+TBASE MOV DI,PVALUE+TBASE ; PVALUE list address
ADD DI,CX ADD DI,CX ; Point to piece value
MOV CL,[DI] MOV CL,[DI] ; Get point value of piece
MOV DI,ATKLST MOV DI,ATKLST ; Init address of attack list
TEST AL,80H TEST AL,80H ; Is it white ?
JZ L10F6 JZ ACK30 ; Yes - jump
ADD DI,7 ADD DI,7 ; Attack list address
L10F6: OR DH,DH ACK30: OR DH,DH ; Queen found this scan ?
JZ L10FC JZ ACK31 ; No - jump
MOV AL,5 MOV AL,QUEEN ; Use Queen slot in attack list
L10FC: AND AL,7 ACK31: AND AL,7 ; Attacking piece type
INC B,[DI] INC B,[DI] ; Increment list count
XCHG AL,CL XCHG AL,CL ; Save point value in AL
ADD DI,CX ADD DI,CX ; Attack list slot address
MOV CH,[DI] MOV CH,[DI] ; Get data already there
TEST CH,0FH TEST CH,0FH ; Is first slot empty ?
JZ L111F JZ ACK40 ; Yes - jump
TEST CH,0F0H TEST CH,0F0H ; Is second slot empty ?
JZ L1115 JZ ACK35 ; Yes - jump
INC DI INC DI ; Increment to King slot
MOV CH,[DI] MOV CH,[DI] ; Get byte
XCHG AL,CH XCHG AL,CH ; Byte to AL, point value to CH
L1115: SHL AL ACK35: SHL AL ; Lower bits to upper
SHL AL SHL AL
SHL AL SHL AL
SHL AL SHL AL
OR AL,CH OR AL,CH ; Combine with point value
L111F: MOV [DI],AL ACK40: MOV [DI],AL ; Put back into attack list
POP DI POP DI ; Restore regs
POP CX POP CX
RET RET ; Return
;*********************************************************** ;***********************************************************
; PIN FIND ROUTINE ; PIN FIND ROUTINE
@@ -2231,4 +2303,4 @@ IY: DS 2
BC: DS 2 BC: DS 2
DE: DS 2 DE: DS 2
HL: DS 2 HL: DS 2