Advertisement
Guest User

Untitled

a guest
Jan 29th, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. x86 equ 1                         ; x86=1 PC/emu vs. win32b/(DOS)Box
  2. saf equ 0                         ; saf=0 +queening -exotic failsafe
  3. _b equ byte                       ; DEFAULTS=PC FLOPPY BOOT+QUEENING
  4. _w equ word                       ; x86=1 saf=0 512b  inc.  queening
  5. _d equ dword                      ; x86=1 saf=1 500b+ excl. queening*
  6. _s equ short                      ; x86=0 saf=1 506b  inc.  queening  
  7. _n equ near                       ; x86=0 saf=0 487b  excl. queening
  8. _f equ far                        ; *only working version in Bochs
  9.     if x86                        ; beg of boot vs .com preprocessing
  10.     org 7c00h                     ; std start of bootsector after post
  11.     if saf                        ; beg clear any start ambiguous segment
  12.     jmp _f 0:fix                  ; 7c0:0000 vs. 0:7c000 cs para fix-up
  13.     end if                        ; end clear any start ambiguous segment  
  14. fix:push cs                       ; if post int 19h isr bootsrap loader
  15.     pop ds                        ; left any bda or shadow segment values
  16.     push cs                       ; then enforce ds=cs=0  
  17.     pop es                        ; then enforce es=ds=cs=0
  18.     mov aX,13h                    ; function set vga mode 320x200x256
  19.     else                          ; else if 16-bit binary assume ah=0
  20.     org 100h                      ; start of com binary program ip
  21.     mov aL,13h                    ; function set vga mode 320x200x256
  22.     end if                        ; end of boot vs .com preprocessing
  23.     int 10h                       ; standard bios video api
  24.     brd equ bf1+16                ; chess board at end of sector
  25.     mov di,brd                    ; set physical board index
  26.     mov bp,12                     ; set 6x8+8 empty sqr mid board lines
  27.     call in2                      ; pass#1 black "rnbqkbnr" low-caps
  28.     push word opn                 ; pass#2 hi-caps whites & fall-through
  29. rle:lodsb                         ; al='.'/al=null (fixed length rle)
  30.     mov cl,8                      ; empty sqr mid board line length
  31.     rep stosb                     ; set one empty sqr mid board line
  32.     dec bp                        ; all empty sqr mid brd lines inited ?
  33.     jnz rle                       ; if not repeat init else bp=0 assumed
  34.     mov ah,'A'-'a'                ; fall-through pass#2 white hi-caps
  35. in2:mov si,br0                    ; si points to endrank "rnbqkbnr" str
  36.     if x86=0                      ; if .com binary environment ch=0
  37.     mov cL,8                      ; "rnbqkbnr" endrank str length
  38.     else                          ; assume nothing although tempting
  39.     mov cX,8                      ; "rnbqkbnr" endrank str length
  40.     end if                        ; end of register ch startup value
  41. in3:lodsb                         ; read physical board str car
  42.     add al,ah                     ; hi-caps rank 1 / low-caps rank 8
  43.     stosb                         ; write physical board str car
  44.     loop in3                      ; all "rnbqkbnr" str car written ?
  45.     mov cl,8                      ; si-;equiv piece vals di-;0x88 brd
  46.     rep movsb                     ; write logical 0x88 board str vals
  47.     retn                          ; return to callers
  48. ge0:mov bx,di                     ; physical board idx (bx=brd)
  49.     mov dh,'1'                    ; beg white move src rank
  50. ge1:mov dl,'h'                    ; beg white move src file
  51. ge2:mov [si],dx                   ; beg white move src str
  52.     mov ch,'1'                    ; beg white move dst rank
  53. ge3:mov cl,'h'                    ; beg white move dst file
  54. ge4:mov [si+2],cx                 ; beg white move dst str
  55.     pusha                         ; save all values
  56.     call idx                      ; passive chess coords to linear indexes
  57.     jbe  mis                      ; white move src color not conforming
  58.     push bx                       ; save white move dst idx
  59.     call ver                      ; white move legal chess ?
  60.     pop bx                        ; restore white move dst idx
  61.     jc mis                        ; white move not legal chess
  62.     mov di,num+3                  ; compare move destination rank in 7dfeh
  63.     inc si                        ; with move source rank in 7dfch
  64.     cmpsb                         ; is taxi distance to topmost bettered ?
  65.     jnc wor                       ; else not getting closer to black king
  66.     cmp _b [di],'?'               ; does any fallback move exist yet ?
  67.     jz lkj                        ; no, then last valid move good enough
  68. wor:mov aL,_b[si+bx+brd-num-'a'+6]; yes, previous valid legal exist so
  69.     dec aL                        ; only override if it's a capture
  70.     js mis                        ; no, don't want worse taxi distance  
  71.     mov bx,fs                     ; it's a capture with piece value=al
  72.     cmp bL,aL                     ; but hightest capture value yet ?
  73.     jnc mis                       ; no, less important opponent piece
  74. max:mov fs,bx                     ; fs=best move yet in taxi half-ply
  75. lkj:dec si                        ; realign source index
  76.     dec si                        ; to copy dword bst=dword idx
  77.     movsd                         ; after 4096 tries : move=dword bst
  78. mis:popa                          ; restore all values
  79.     cmp cl,'a'                    ; end white move dst file ?
  80.     loopnz ge4                    ; dec white move else next dst file
  81.     inc ch                        ; inc white move dst rank
  82.     cmp ch,'9'                    ; end white move dst rank ?
  83.     jnz ge3                       ; else next move dst rank
  84. cpx:inc bx                        ; inc physical board index
  85.     dec dx                        ; dec white move src file
  86.     cmp dl,'`'                    ; end white move src file ?
  87.     jnz ge2                       ; else next move src file
  88.     inc dh                        ; inc white move src rank
  89.     cmp dh,ch                     ; end white move src rank ? ch=9
  90.     jnz ge1                       ; else next move src rank
  91.     push _d [si+4]                ; get best white move found
  92.     pop _d [si]                   ; set it as final white move
  93. val:mov cl,'.'                    ; valid : empty sqr replaces src piece
  94.     call act                      ; active chess coords to linear indexes
  95.     xor bp,3                      ; player turn and pawn unidir. delta
  96.     jz ge0                        ; white turn to play (case best=0)
  97. bla:mov al,'?'                    ; input str clear pattern
  98.     mov di,si                     ; input str clear pattern (di=num)
  99.     mov cx,8                      ; input str clear pattern
  100.     rep stosb                     ; input str clear pattern (di=brd)
  101.     call key                      ; get user keyboard input
  102.     jbe bla                       ; black move src color not conforming
  103. opn:call ver                      ; di=brd, black move legal chess ?
  104.     jc bla                        ; white move not legal chess
  105.     jmp _s  val                   ; validate black move
  106. ver:call idx                      ; get lin indexes /w implicit passive
  107.     xchg bx,dx                    ; switch bx=dst idx dx=src idx
  108.     mov ah,[si+bx+brd-num-'a'+8]  ; get piece logical 0x88 brd val...
  109.     mov dh,bl                     ; dh=src idx dl=dst idx
  110.     sub dx,"aa"                   ; get move file zero-based indexes
  111.     bsr bx,ax                     ; scan for 1st bit set (si=idx+10)
  112.     movsx bx,[si+bx-10-num+tab]   ; bl=moved piece type idx (bh=0)
  113.     mov cx,_w [si+bx-num+tab]     ; piece type deltas cl=repeats ch=num
  114.     sahf                          ; set piece logical 0x88 brd val  
  115.     jnp sp1                       ; branch if piece not pawn (bit#4!=1)
  116.     jc sp2                        ; branch if pawn prev moved (bit#0=1)
  117. sp1:jns sp3                       ; branch if piece not king (bit#7!=1)
  118. sp2:mov cl,1                      ; override repeat if piece pawn or king
  119. sp3:jnp sp4                       ; branch if piece not pawn (bit#4!=1)
  120.     add bx,bp                     ; pawn player turn unidirection mutex
  121. sp4:inc bx                        ; advance piece type struct field ptr
  122.     and ah,11111100b              ; isolate piece bitmask only
  123. vl1:push cx                       ; save piece type deltas
  124.     mov al,dh                     ; load start dst idx val
  125.     inc bx                        ; advance piece type struct field ptr
  126. vl2:add al,[si+bx-num+tab]        ; add this piece delta to dst idx val
  127.     xchg aL,bL                    ; base reg=dst idx val and preserved
  128.     mov ch,[si+bx+brd-num+8]      ; read projected dst square val
  129.     xchg aL,bL                    ; base reg=piece type struct field ptr
  130.     cmp al,dl                     ; wanted move found (src+delta(s)=dst) ?
  131.     jnz dif                       ; different than requested move
  132. sam:sahf                          ; get piece logical 0x88 brd val in flgs
  133.     jnp yes                       ; branch if piece is not pawn (bit#2=0)
  134.     test [si+bx-num+tab],1        ; pawn piece delta parity=diag vs. vert
  135.     jz ord                        ; branch if pawn piece moving vert
  136.     test ch,ch                    ; pawn piece vert move=;eating ?
  137.     jnz yes                       ; capturing verify dst sqr not empty
  138.     jmp _s bad                    ; else illegal chess move is a miss
  139. ord:test ch,ch                    ; pawn piece vert move=;no eating ?
  140.     jz yes                        ; no eating=;empty dst sqr else illegal
  141. dif:sahf                          ; store piece nature in flags register
  142.     jnp skp                       ; not pawn piece so skip direction test
  143.     test [si+bx-num+tab],1        ; pawn piece delta parity=diag vs. vert
  144.     jnz bad                       ; diagonal pawn move is illegal
  145. skp:test ch,ch                    ; else skipping over dst square val ?
  146.     jnz bad                       ; projected dst sqr val is not empty
  147.     sahf                          ; get piece logical 0x88 brd val in flgs
  148.     jz x88                        ; branch if piece is queen (bit#6=1)
  149.     jna bad                       ; branch if piece is not knight(bit#4=0)
  150. x88:test al,88h                   ; ch=0 dst out of physical board limits?
  151.     loopz vl2                     ; else cont if delta repeats remain
  152. bad:pop cx                        ; restore piece type deltas
  153.     dec ch                        ; all possible delta nums verified ?
  154.     jnz vl1                       ; if not then cont next delta type
  155. nok:stc                           ; else return /w no match flg set
  156.     retn                          ; return to caller
  157. yes:pop cx                        ; correct entry sp and disregard count
  158.     retn                          ; return to caller(s)
  159. key:call prt                      ; refresh screen to account input echo
  160.     xor bx,bx                     ; bx=str idx=odd/even/alpha/num mutex
  161. kbd:cbw                           ; fun blocking wait for keystroke (ah=0)
  162.     int 16h                       ; std bios keybd api (ah=scan al=ascii)
  163. esc:dec ah                        ; was esc key pressed to quit ?
  164.     jnz car                       ; else default process key input
  165. xit:if x86                        ; if x86 boot context environment
  166.     int 19h                       ; exit through bootstrap to reboot cpu
  167.     else                          ; else if .com 16-bit binary
  168.     int 20h                       ; dos 1+ - terminate program
  169.     end if                        ; end of exit methods (os load or shell)
  170. car:mov [bx+si],al                ; sav ascii val to move string (si=num)
  171. prt:pusha                         ; save game state snapshot
  172.     cwd                           ; curs location dx=(0,0)=(row,column)
  173.     mov ax,1301h                  ; function ega write str write mode 1
  174.     mov bl,7                      ; page 0 grey car attrib matching tty
  175.     mov cl,8                      ; src str lngth (curs updated horiz)
  176.     mov bp,bf1                    ; es:bp is "abcdefgh" ptr
  177. lns:int 10h                       ; standard bios video api
  178.     add bp,16                     ; bp=para step siz separating strings
  179.     push ax                       ; save old bios video api func params
  180.     mov ax,0e39h                  ; function teletype outp car=rank '9'
  181.     sub al,dh                     ; decrement right handside rank value
  182.     int 10h                       ; standard bios video api
  183.     pop ax                        ; restore old bios video api fx params
  184.     cmp dh,cl                     ; src str total (curs updated vert)
  185.     inc dh                        ; preemptive off-by-one allows 9 verts
  186.     jc lns                        ; all 9 brd gui row strings printed ?
  187.     mov bp,si                     ; 10th row tail bp=move coords, cl=8
  188.     int 10h                       ; standard bios video api
  189.     popa                          ; restore game state snapshot
  190.     inc bx                        ; test if any more keys ?
  191.     cmp bl,4                      ; frFR format input string
  192.     jc kbd                        ; else continue input
  193. idx:loop idx                      ; ch=0 passive call load src/dst lin idx
  194. act:mov si,num                    ; reinit si to point to coord input str.
  195.     mov bx,[si]                   ; bx=src coord (pass#1)
  196.     cbw                           ; empty sqr val in logical 0x88 board
  197.     call put                      ; place param passed as fun pass#1
  198.     mov dx,[si+2]                 ; bx=dst idx dx=src idx
  199.     xchg bx,dx                    ; fall-through for second pass
  200.     push word mat                 ; test for checkmate and conforming
  201. put:xchg ax,bx                    ; bx|dx=[num+di]+16*((8-'0')-[num+di+1])
  202.     aad -10h                      ; shl ah,4/sub al,ah/xor ah,ah
  203.     add al,80h                    ; bx|dx=al-640%256-16*ah
  204.     xchg ax,bx                    ; bx|dx=al+128-16*ah
  205.     jcxz sim                      ; active call request or simulation ?
  206.     if saf=0                      ; standard non-failsafe queening
  207.     cmp _b [si+3],'8'             ; validated dst rank is top-most ?    
  208.     jz qq                         ; if so then promote pawn to queen
  209.     cmp _b [si+3],'1'             ; validated dst rank is bottom-most ?
  210.     jnz prm                       ; if not no pawn queening promotion
  211. qq: sahf                          ; store piece nature in flag register
  212.     jnp prm                       ; no pawn queening promotion  
  213.     xor ah,01000110b              ; transform p to promoted queen
  214.     inc cx                        ; queen promotion p2q or P2Q
  215.     end if                        ; end of conditional queening
  216. prm:xchg ah,[si+bx+brd-num-'a'+8] ; update piece logical 0x88 board val
  217.     xchg cl,[si+bx+brd-num-'a']   ; update piece physical board ascii val
  218.     or ah,1                       ; update piece moved once (bit#0)
  219. sim:retn                          ; return to caller(s)
  220. mat:sahf                          ; catured piece king and mate ?
  221.     js xit                        ; if piece is king then game is over
  222.     call chk                      ; move src color conforming ?
  223.     jnz nok                       ; move src color not conforming
  224. chk:xchg bx,dx                    ; src idx <- dst idx
  225.     mov al,[si+bx+brd-num-'a']    ; pass#1:src idx pass#2:dst idx di=brd
  226.     xor _b [si+len-num],8         ; self-modif 8/26 val=[1;8]/[a(A);z(Z)]
  227.     mov cl,-'a'                   ; assert black piece car interval
  228.     test bp,bp                    ; test whose turn it is to play
  229.     jnz lim                       ; assert white piece car interval
  230.     mov cl,-'A'                   ; al=ascii value cl=-(lower boundery)
  231. lim:xadd al,cl                    ; tmp=al+cl cl=al al=tmp +fall-trough
  232.     db 0d4h                       ; aam <self-modified value>
  233. len:db 12h                        ; ah=al/8 al%=8
  234.     mov al,cl                     ; al=restored ascii value
  235.     test ah,ah                    ; set/clear zf=0 success zf=1 failure
  236.     retn                          ; return to caller(s) nb: destroys ah
  237. tab db p-tab,r-tab,n-tab,b-tab    ; piece type mov offset array
  238.     db q-tab,q-tab                ; note original 1K ZX Chess q=k trick
  239. br0 db "rnbqkbnr",8,16,32,64,128  ; end rank pattern + beg piece values
  240.     db 32,16,8,'p',4,'.',0,'.',0  ; end piece values + beg mid board reps
  241.     db '.',0,'.',0,'P',4          ; ... end mid board reps
  242. p   db 2,3,-10h,-15,-17,10h,15    ; bit#2 pf=04 p[6]=r[0] overlay
  243. r   db 17,4,10h,-1h,-10h          ; bit#3 ??=08 r[5]=n[0] overlay
  244. n   db 1,8,1fh,21h,12h,-0eh,-1fh  ; bit#4 af=16 n[9]=b[0] overlay
  245.     db -21h,-12h                  ; ... end of knight moves list
  246. b   db 0eh,4,-0fh,11h,-11h        ; bit#5 ??=32 b[5]=q[0] overlay
  247. q   db 0fh,8,10h,11h,0fh,1h,-10h  ; bit#6 zf=64 k=q except k[0]=1
  248.     db -11h,-0fh,-1h              ; ... end of queen/king moves list
  249. bf1 db "abcdefgh"                 ; gui file index string
  250. num db "e2e4"                     ; hardcoded Ruy Lopez opening
  251.     if saf and x86                ; x86 failsafe exotic boot environment
  252.     times 510-($-$$) db 0         ; nul padding if necessary
  253.     org 7df0h                     ; boot signature vbr/mbr standard offset
  254.     sig db 55h,0aah               ; magic number no-endian boot signature
  255.     end if                        ; end of conditional failsafe signature
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement