Procedure for finding the Check.
© 1997, Leonid Liberman

    Already for few months I was practically enable to reach my game. From one side, my job knew some resuscitation (I work on my chess game only in my spare time), from other, the desire to rewrite everything for Windows make me walk through some strange and distant places. But what the wonder it is! Who knows, maybe I must spell few words about my last wandering as well. Maybe you, like me, would like to write your game through Assembler for Windows. Why not? But where you must start? Response is simple - go to Australia. We have in this marvelous land certain Barry Kauler that wrote: "Windows Assembly Language and System Programming. 16 and 32 Bit Low-Level Programming for the PC and Windows". Edition 1997.

    Probably, after reading this book you will start seeing this exotic continent through my glasses. The land that is now populated not only with jumping kangaroo and lovely birds, but few sophisticated human creatures as well.

    Once again about this article. For some short time I went back to my programming in DOS. While speeding the Position Logic (for 6 plies) and roaming with the ideas to make one billion things more, I have passed through the logic for recognizing the Check. Because of its universality, I came to my mind to put it on the Web site. This procedure, while looking by who the King was attacked, goes through the basic logic for every piece that on the board.

Procedure below:

1) Will look if the White King is under the fire (check).
2) All the enemy pieces, presumably, are still on the board.
3) Procedure receive position of the King. (AH).
4) Response says if King is under attack. (Variable GOLPE).

The position of each piece on the board is indicated by bits: 0,1,2 and 3,4,5.
Bits: 0,1,2 - horizontal position of the board.
Bits: 3,4,5 - vertical position of the board.
Counting of the squares goes from left to right and from up to down (like when
you read the book).
Few positions for more understanding how it is indicated.
Position up-left - 00000000b
Position up-right - 00000111b
Position down-left - 00111000b
Position down-right - 00111111b
Explanation in this procedure will goes with almost each line, so it must be
highly readable. If something is still not clear, or you just want to speak
your mind, you can send me E-mail.
comment # 25/11/97
The procedure looks if the white King is under the fire. If yes, GOLPE will
leave this procedure > 0.
Procedure presume that each enemy piece is still on the board.
The initial position of the white King comes in AH. (Position is indicated by
bits 0-5. Bits 6 and 7 are put to zero.)
#
white proc uses esi edi eax ebx ecx edx
mov golpe,0 ;If, at the end, GOLPE > 0 - white King is under the fire.
xor dx,dx ;DH must be put to 0 for the macros below.
;---------------------------------------------------------------------------
;11111111111 ------ Bishop (Queen) - direction: LOW - UP-LEFT.
;**********************************************************
mov bl,ah ;Put the coordinates in BL.
mov dl,ah ;Put the coordinates in DL.
;------ Direction - LOW - Up-Left. First Square.
cmp ah,00001000b ;See if the Kings line is below the line one. Zero line -
jb sole1 ;the up most line. King is already on the 0 line. Nobody can
 ;stay over his head, so no danger from above.
and bl,00000111b ;Leave only horizontal position.
cmp bl,00000001b ;The leftist square on the board is 0. See if the King is 
jb sole1 ;already on the square 0. Yes. No danger can come from his
 ;left.
;-------------------------------------------------------------------------
;---------------- Limits are respected. ------------------------------
sub dl,00001001b ;Go one line below (add 00001000b) and one square to the
 ;right (add 00000001b). 00001000b+00000001b=00001001b
fencedx ;The macro receive coordinates in DX. In reality, DH is all
 ;the time = 0 (remember xor DX,DX) and only the DL keeps the
 ;coordinates. Description of the square comes in CL.
cmp cl,empty ;This square is empty ?
je repe1 ;Yes. Go to see if the danger come from the next square.
;--------------------------------------------------------------------------
;--- See here the danger from: BISHOP, QUEEN, KING, PAWN. FIRST SQUARE.
cmp cl,00000100b ;Black king?
je sortie ;Yes. Danger.
cmp cl,00000011b ;Black Queen?
je sortie ;Yes. Danger.
cmp cl,00000010b ;Black Bishop?
je sortie ;Yes. Danger.
cmp cl,00000101b ;Black Pawn ?
je sortie ;Yes.
;--- Otherwise, piece is white or black and represent shelter for the King.
jmp sole1 ;Go to see the next part.
;*************************************************************************
;***************************************************************************
repe1: ;AFTER THE 1 SQUARE. UP-LEFT. THE FIRST SQUARE IS EMPTY.
mov bl,dl ;Place the coordinates in BL.
cmp bl,00001000b ;Can go one line up?
jb sole1 ;No.
and bl,00000111b ;Leave the horizontal position.
cmp bl,00000001b ;Already extreme-left?
jb sole1 ;Yes.
;-------------------------------------------------------------------
sub dl,00001001b ;Go one line up and one square at left.
fencedx ;Go to see what we have in this square.
cmp cl,empty ;Empty square ?
je repe1 ;Yes. 
;--- Otherwise, this square is not empty.
cmp cl,00000011b ;Black square ?
je sortie ;Yes. Danger.
cmp cl,00000010b ;Black bishop ?
je sortie ;Yes. Danger.
;-- Otherwise, piece-shelter.
;****************************************************************-
sole1:
;**********************************************************
;222222------ BISHOP (queen) - direction: BELOW - UP-RIGHT.
;**********************************************************
mov bl,ah ;Place coordinates in BL.
mov dl,ah ;Place coordinates in DL.
 
;------ Direction - BELOW - UP-RIGHT. FIRST SQUARE.
cmp bl,00001000b ;If the bishop is already on the line 0 - quit.
jb aa1f ;Quit.
and bl,00000111b ;Leave only horizontal position.
cmp bl,00000110b ;If bishop is already on the square at extreme-right,
ja aa1f ;quit.
;-------------------------------------------------------------------------
;---------------- Limits are respected. ------------------------------
sub dl,00000111b ;Go more up-right.
fencedx ;DX - coordinates of entry. CL-content of the square.
cmp cl,empty ;Empty square ?
je bbbbf ;Yes. Go to see the danger that comes after first square.
;--------------------------------------------------------------------------
;--- Look for danger from: BISHOP, QUEEN, KING, PAWN. FIRST SQUARE.
cmp cl,00000100b ;Black king ?
je sortie ;Yes. Danger.
cmp cl,00000011b ;Black Queen ?
je sortie ;Yes. Danger.
cmp cl,00000010b ;Black bishop ?
je sortie ;Yes. Danger.
cmp cl,00000101b ;Black pawn ?
je sortie ;Yes. Danger.
;-- Otherwise piece-shelter.
jmp aa1f ;Go to the next part.
;*************************************************************************
;***************************************************************************
bbbbf: ;AFTER 1 SQUARE. BELOW - UP-RIGHT. 1 square is empty.
mov bl,dl ;Place the coordinates of the 1 square in BL.
cmp bl,00001000b ;The up most line?
jb aa1f ;Yes.
and bl,00000111b ;Leave only horizontal position.
cmp bl,00000110b ;Already the extreme right?
ja aa1f ;Yes.
;-------------------------------------------------------------------
sub dl,00000111b ;Go one line up and one square to the right.
fencedx ;Go to see the content of this square.
cmp cl,empty ;This square is empty ?
je bbbbf ;Yes. Go to see a little bit farther.
;--- Otherwise, the square is not empty.
cmp cl,00000011b ;Black Queen ?
je sortie ;Yes. Danger.
cmp cl,00000010b ;Black bishop ?
je sortie ;Yes. Danger.
;-- Otherwise, piece-shelter.
;****************************************************************-
aa1f:
;33333333333 ------ Bishop (queen) - direction: UP - LOW-RIGHT.
;**********************************************************
mov bl,ah ;Place the coordinates.
mov dl,ah ;Place the coordinates en DL.
;------ Direction - UP - LOW-RIGHT. FIRST SQUARE.
cmp ah,00110111b ;Bishop on line 7 (the lowest)? 
ja baa1 ;Quit.
and bl,00000111b ;Leave only horizontal position.
cmp bl,00000110b ;If the bishop is already on the square extreme-right,
ja baa1 ;quit.
;-------------------------------------------------------------------------
;---------------- The limits are respected.
add dl,00001001b ;Go more to the right.
fencedx ;DX - coordinates of entry. CL-content of entry square.
cmp cl,empty ;This square is empty ?
je bbbbb ;Yes. Go to see the menace after the first square.
;--------------------------------------------------------------------------
;--- Make revision of danger from: BISHOP, QUEEN, KING. FIRST SQUARE.
cmp cl,00000100b ;Black king?
je sortie ;Yes. Danger.
cmp cl,00000011b ;Black queen ?
je sortie ;Yes. Danger.
cmp cl,00000010b ;Black bishop ?
je sortie ;Yes. Danger.
;-- Otherwise, shelter-piece.
jmp baa1 ;Go to see the next part.
;*************************************************************************
;***************************************************************************
bbbbb: ;AFTER FIRST SQUARE. LOW-RIGHT. The first square is empty.
mov bl,dl ;Place the coordinates in BL.
cmp bl,00110111b ;Already the lowest line ?
ja baa1 ;Yes.
and bl,00000111b ;Leave only horizontal position.
cmp bl,00000110b ;Already the square at extreme right?
ja baa1 ;Yes.
;-------------------------------------------------------------------
add dl,00001001b ;Go low-right.
fencedx ;What we have in this square?
cmp cl,empty ;It is empty?
je bbbbb ;Yes.
;--- Otherwise, this square is not empty.
cmp cl,00000011b ;Black queen?
je sortie ;Yes.
cmp cl,00000010b ;Bishop?
je sortie ;Yes.
;-- Otherwise, shelter-piece.
;****************************************************************-
baa1:
;**********************************************************
;4444444 ------ BISHOP (queen) - direction: UP - LOW-LEFT.
;**********************************************************
mov bl,ah ;Place the coordinates in BL.
mov dl,ah ;Place the coordinates in DL.
;------ Direction - from UP to LOW-LEFT. 1 SQUARE.
cmp bl,00110111b ;Already on the line 7 (the lowest line)?
ja aa12 ;Yes.
and bl,00000111b ;Leave only horizontal position.
cmp bl,00000001b ;If the bishop is already on the square at extreme-left,
jb aa12 ;quit.
;-------------------------------------------------------------------------
;---------------- Limits are respected. ------------------------------
add dl,00000111b ;Go Low-Left.
fencedx 
cmp cl,empty ;Empty ?
je bbbb2 ;Yes. Go to see the menace after first square.
;--------------------------------------------------------------------------
;--- Here look for menace from: BISHOP, QUEEN, KING. FIRST SQUARE.
cmp cl,00000100b ;Black king ?
je sortie ;Yes. 
cmp cl,00000011b ;Black queen ?
je sortie ;Yes.
cmp cl,00000010b ;Black bishop ?
je sortie ;Yes.
;-- Otherwise, shelter-piece.
jmp aa12 ;Go to see the next part.
;*************************************************************************
;***************************************************************************
bbbb2: ;AFTER FIRST SQUARE. LOW-LEFT. FIRST SQUARE IS EMPTY.
mov bl,dl ;Place coordinates.
cmp bl,00110111b ;Already on the most low line?
ja aa12 ;Yes.
and bl,00000111b ;Leave only horizontal position.
cmp bl,00000001b ;Already on the leftist square?
jb aa12 ;Yes.
;-------------------------------------------------------------------
add dl,00000111b ;Go one line below and one square at left.
fencedx ;Go to see what we have there.
cmp cl,empty ;Empty square ?
je bbbb2 ;Yes.
;--- Otherwise, square is not empty.
cmp cl,00000011b ;Black queen ?
je sortie ;Yes.
cmp cl,00000010b ;Black bishop?
je sortie ;Yes. 
;-- Otherwise, shelter-piece.
;****************************************************************-
aa12: ;ROOK ROOK ROOK
;**********************************************************
;1111111111 ------ ROOK (QUEEN) - direction: DOWN.
;**********************************************************
mov dl,ah ;Place the coordinates.
;------ Direction - down. FIRST SQUARE.
cmp dl,00110111b ;If the bishop is already on the line 7 (lowest), quit. 
ja aa1k ;Quit.
;-------------------------------------------------------------------------
;---------------- Limits are respected. ------------------------------
add dl,00001000b ;Go down.
fencedx 
cmp cl,empty ;Empty ?
je bbbbk ;Yes. Go to see if the danger do not come after the 1 square.
;--------------------------------------------------------------------------
;--- Here see danger from: ROOK, QUEEN, KING. FIRST SQUARE.
cmp cl,00000100b ;Black king ?
je sortie ;Yes.
cmp cl,00000011b ;Black queen ?
je sortie ;Yes.
cmp cl,00000000b ;Black rook ?
je sortie ;Yes.
;--- This piece represent shelter.
jmp aa1k ;Go to see the next part.
;*************************************************************************
;***************************************************************************
bbbbk: ;AFTER 1 SQUARE. DIRECTION - DOWN. 1 SQUARE IS EMPTY.
cmp dl,00110111b ;Already on the lowest line ?
ja aa1k ;Yes.
;-------------------------------------------------------------------
add dl,00001000b ;Go one line down.
fencedx ;See what we have in this square.
cmp cl,empty ;Empty ?
je bbbbk ;Yes.
;--- Otherwise, this square is not empty.
cmp cl,00000011b ;Black queen ?
je sortie ;Yes. 
cmp cl,00000000b ;Rook ?
je sortie ;Yes.
;---- Otherwise, shelter-piece.
;****************************************************************-
aa1k: ;ROOK ROOK ROOK UP UP UP
;1111111111 ---- ROOK (QUEEN) - direction: UP.
;**********************************************************
mov dl,ah ;Place the coordinates in DL.
;------ Direction - UP. FIRST SQUARE.
cmp dl,00001000b ;If king is already on the line 0 (up most), no danger can come
jb aa1k0 ;from above. King is on the line 0. Quit. 
;-------------------------------------------------------------------------
;---------------- Limits are respected. ------------------------------
sub dl,00001000b ;Go up.
fencedx 
cmp cl,empty ;Empty ?
je bbbbk0 ;Yes. Go to see the possible danger after 1 square.
;--------------------------------------------------------------------------
;--- Here revision the danger from: ROOK, QUEEN, KING. FIRST SQUARE.
cmp cl,00000100b ;Black king?
je sortie ;Yes.
cmp cl,00000011b ;Black queen ?
je sortie ;Yes.
cmp cl,00000000b ;Black rook ?
je sortie ;Yes.
;-- Otherwise, piece-shelter.
jmp aa1k0 ;Jump this part.
;*************************************************************************
;***************************************************************************
bbbbk0: ;AFTER 1 SQUARE. DIRECTION - UP. First square.
cmp dl,00001000b ;Already on the highest line?
jb aa1k0 ;Yes.
;-------------------------------------------------------------------
sub dl,00001000b ;Go one line up.
fencedx 
cmp cl,empty ;Empty square ?
je bbbbk0 ;Yes.
;--- Otherwise, this square is not empty.
cmp cl,00000011b ;Black queen ?
je sortie ;Yes.
cmp cl,00000000b ;Black rook ?
je sortie ;Yes.
;-- Otherwise, shelter-piece.
;****************************************************************-
aa1k0: ;ROOK ROOK ROOK LEFT LEFT LEFT
;1111111111 ---- ROOK (QUEEN) - direction: LEFT.
;**********************************************************
mov dl,ah ;Place the coordinates in DL.
mov bl,ah ;And also in BL.
;------ Direction - LEFT. FIRST SQUARE.
and bl,00000111b ;Leave only horizontal position.
cmp bl,00000001b ;The king at extreme-left
jb aa1k1 ;Yes.
;-------------------------------------------------------------------------
;---------------- Limits are respected. ------------------------------
dec dl ;Go to your left.
fencedx 
cmp cl,empty ;Empty ?
je bbbbk1 ;Yes. Go to see the danger after first square.
;--------------------------------------------------------------------------
;--- Here revise possibility of danger from: ROOK, QUEEN, KING. 1 SQUARE.
cmp cl,00000100b ;Black king ?
je sortie ;Yes.
cmp cl,00000011b ;Black queen ?
je sortie ;Yes.
cmp cl,00000000b ;Black rook ?
je sortie ;Yes.
;-- Otherwise one shelter.
jmp aa1k1 ;Jump to see the next part.
;*************************************************************************
;***************************************************************************
bbbbk1: ;AFTER 1 SQUARE. DIRECTION - LEFT. 1 SQUARE - EMPTY.
mov bl,dl ;Place the coordinates in BL.
and bl,00000111b ;Leave only horizontal position.
cmp bl,00000001b ;Already extreme-left ?
jb aa1k1 ;Yes.
;-------------------------------------------------------------------
dec dl ;Go one square left
fencedx ;What we have in this square?
cmp cl,empty ;Empty ?
je bbbbk1 ;Yes.
;--- Otherwise, this square is not empty.
cmp cl,00000011b ;Black queen ?
je sortie ;Yes.
cmp cl,00000000b ;Black rook ?
je sortie ;Yes.
;-- Otherwise, it is shelter.
;****************************************************************-
aa1k1: ;ROOK ROOK ROOK RIGHT RIGHT RIGHT.
;1111111111 ---- ROOK (QUEEN) - direction: RIGHT.
;**********************************************************
mov dl,ah ;Place the coordinates in DL.
mov bl,ah ;And also in BL.
;------ Direction - RIGHT. FIRST SQUARE.
and bl,00000111b ;Leave only horizontal position.
cmp bl,00000110b ;Already extreme-right?
ja aa1kb ;Yes.
;-------------------------------------------------------------------------
;---------------- Limits are respected. ------------------------------
inc dl ;Go more to the right.
fencedx 
cmp cl,empty ;Empty ?
je bbbbkb ;Yes. Go look the square after the first one.
;--------------------------------------------------------------------------
;--- Here look the danger from: ROOK, QUEEN, KING. FIRST SQUARE.
cmp cl,00000100b ;Black king ?
je sortie ;Yes.
cmp cl,00000011b ;Black queen ?
je sortie ;Yes.
cmp cl,00000000b ;Black rook ?
je sortie ;Yes.
;-- Otherwise, shelter-piece.
jmp aa1kb ;Jump to the next part.
;*************************************************************************
;***************************************************************************
bbbbkb: ;AFTER FIRST SQUARE. DIRECTION - RIGHT. 
mov bl,dl ;Place the coordinates.
and bl,00000111b ;Leave horizontal position.
cmp bl,00000110b ;Extreme-right?
ja aa1kb ;Yes.
;-------------------------------------------------------------------
inc dl ;Go more to the right.
fencedx ;See what this square have in it.
cmp cl,empty ;It is empty?
je bbbbkb ;Yes. 
;--- Otherwise, square is not empty.
cmp cl,00000011b ;Black queen ?
je sortie ;Yes.
cmp cl,00000000b ;Black rook ?
je sortie ;Yes.
;-- Otherwise it is shelter.
;****************************************************************-
aa1kb: ;KNIGHT KNIGHT KNIGHT.
mov bl,ah ;Put the coordinates in BL. 
and bl,00000111b ;Leave only horizontal position. 
;-----------------------------------------------------------------------
;11111111111111111
;************** 1 line up and left from initial position.*********
 ;Verify the limits.
cmp ah,00001000b ;See if the knight can go up.
jb bajones ;He can't.
cmp bl,00000010b ;Look the horizontal position.
jb ss11 ;He cannot move on his left position.
mov dl,ah ;Otherwise knight can go one line up and to the left.
sub dl,00001010b ;-00001000b - 00000010b
;------------------------------------------
fencedx 
cmp cl,00000001b ;Black knight?
je sortie ;Yes. Danger.
;**********************************************************************
;222222222222222222222222222222222
;** See knight one line up and to the right, from initial position of the king.
ss11:
cmp bl,00000101b ;Position vertical was already verified. Verify only
ja ss22 ;horizontal position. Horizontal is in trouble.
mov dl,ah ;Otherwise, horizontal is OK.
sub dl,00000110b ;-00001000b+00000010b = -00000110b
;-----------------------------
fencedx
cmp cl,00000001b ;Black knight?
je sortie ;Yes. Danger.
;*********************************************************************
;33333333333333333333
;***** 2 line up and to the left from initial position of the king.
ss22: 
cmp ah,00010000b
jb bajones ;Go 2 lines up is impossible.
cmp bl,00000001b
jb ss33
;-------------------------------
mov dl,ah
sub dl,00010001b ;-00010000b - 00000001b
;-------------------------------
fencedx
cmp cl,00000001b ;Black knight ?
je sortie ;Yes. Danger.
;**********************************************************************
;4444444444444444444444444
;****** 2 lines up and to the right.
ss33:
cmp bl,00000110b ;Vertical position is already verified. Look only horizontal
ja bajones ;position.
;-------------------
mov dl,ah
sub dl,00001111b 
fencedx
cmp cl,00000001b ;Black knight ?
je sortie ;Yes. Danger.
;**********************************************************************
;==========================================================================
;========== Look one line below and at left.
bajones: ;555555555555555555555
cmp ah,00110111b ;Look if knight could stay one line below.
ja bala ;No. Quit.
cmp bl,00000010b
jb ss44
;-------- Limits are respected.
mov dl,ah
add dl,00000110b 
;----------------------------------------------------
fencedx
cmp cl,00000001b ;Black knight? 
je sortie ;Yes. Quit.
;***************************************************************************
;666666666666666666
;****** Look for knight one line below and right.
ss44:
cmp bl,00000101b ;Vertical position is already verified. Look only 
ja ss55 ;horizontal position.
;--- Limits are verified.
mov dl,ah
add dl,00001010b ;+00001000b + 00000010b
;----------------------------
fencedx
cmp cl,00000001b ;Black knight ?
je sortie ;Yes. Danger.
;*************************************************************
;77777777777777777777 ;Two lines below and left.
ss55:
;------Look if position vertical and horizontal are respected.
cmp ah,00101111b ;Line 5 ?
ja bala ;Vertical position is not respected. Quit procedure.
cmp bl,00000001b
jb ss66 ;Horizontal position is not respected. Quit procedure.
;--------- Limits are respected.
mov dl,ah
add dl,00001111b ;+00010000b et - 00000001b
fencedx 
cmp cl,00000001b ;Black knight?
je sortie ;Yes.
;*************************************************************************** 
;88888888888888888
;**** Two lines below and right.
ss66: ;Vertical position is already verified.
cmp bl,00000110b ;Verify only horizontal position.
ja bala
mov dl,ah
add dl,00010001b 
fencedx
cmp cl,00000001b ;Black knight ?
jne bala ;No. Quit.
;***************************************************************************
sortie:
mov golpe,1 ;Say that king is under the fire.
;---------------------------------
bala:
ret
white endp

  Verification for the Black King is very similar, I will say symmetrical. Even, if was not the boring work of translation of it, I could give it right now. Probably I came to the moment when I must start writing everything, since the beginning, in English. It will simplify my life.

	© 29/11/97, Leonid Liberman