在装配x86中将棋盘输出到控制台?

时间:2014-02-27 20:22:16

标签: assembly x86 masm chess irvine32

我正在深入研究Assembly for x86并试图在我的控制台中“绘制”或输出一个8x8白色和灰色的棋盘。仍然是装配新手,我没有太多运气:/想知道是否有任何人在x86汇编编程方面有更多经验可以帮助并指出我正确的方向?

我之前做了一个程序,我在多个背面/前景中输出了一个'char',这就是我拼凑下面的地方:

修改: 当我发现颜色问题时更新了代码。

EDIT2 : 更新了代码以使用Loop作为枪手建议。

TITLE Chess Board


INCLUDE Irvine32.inc

COUNT = 7
ROWCNT = 7

.data
    text BYTE "__", 0
    text2 BYTE "00", 0
.code
main PROC
L1:
    mov eax,gray+(gray*16)
    call SetTextColor
    mov edx, offset text
    call writeString
    mov ebx, COUNT
    dec ebx

    mov eax,white+(white*16)
    call SetTextColor
    mov edxecx, offset text3
    call writeStringL1:
    decpush ebxecx

    mov eax,gray+(gray*16)
    call SetTextColor
    mov edx, offset text
    call writeString
    dec ebxWriteString

    mov eax ,white+(white*16)
    call SetTextColor
    mov edx, offset text
    call writeString
    dec ebxWriteString

    mov eax,gray+(gray*16)
    call SetTextColor
    movcmp edxecx, offset text
    call writeString0
    decje ebxfourthSetTiles

    mov eax,white+(white*16)
    call SetTextColor
    mov edx, offset text
    callpop writeString
ecx

    decloop ebxL1

    fourthSetTiles:

mov eax,gray+(gray*16)
    call SetTextColor
    mov edx, offset texttext2
    call writeString
    dec ebx

    cmp ebx, 0
    je eigthRowTile


    jmp L1WriteString

eigthRowTile:
    mov eax,green+(white*16)
    call SetTextColor
    mov edx, offset text2
    call writeStringWriteString

exit
exit
main ENDP
END main

我目前的输出如下。我正在获得我的8个不同的“瓷砖”,但我想现在正试图在更大的整体循环中找到如果有人可以提供帮助的方法吗?生成此输出8次(使用不匹配的图块颜色)

ConsoleOutput

我正在尝试在我的控制台中创建一个8x8网格。任何人都有建议,提示或想法?一如既往的帮助表示赞赏! :)

EDIT3 : 最终守则

   TITLE Chess Board                (ChessBoard.asm)

INCLUDE Irvine32.inc
; procedure prototypes:
SetColor PROTO forecolor:BYTE, backcolor: BYTE
WriteColorChar PROTO char:BYTE, forecolor:BYTE, backcolor:BYTE
PrintRowOdd PROTO color:BYTE
PrintRowEven PROTO color:BYTE

.data
rows = 8
columns = 8
horizCharsPerSquare = 2

.code
main PROC
    mov ecx, rows / horizCharsPerSquare
L1:
    INVOKE PrintRowOdd, gray
    call Crlf
    INVOKE PrintRowEven, gray
    call Crlf
    loop L1

    INVOKE SetColor, lightGray, black ; return to normal color
    call Crlf

    exit
main ENDP

PrintRowOdd PROC uses ecx, color:BYTE
    mov ecx, columns / horizCharsPerSquare
L1:
    INVOKE WriteColorChar, ' ', color, color
    INVOKE WriteColorChar, ' ', color, color
    INVOKE WriteColorChar, ' ', white, white
    INVOKE WriteColorChar, ' ', white, white
    loop L1

    ret
PrintRowOdd ENDP

PrintRowEven PROC uses ecx, color:BYTE
    mov ecx, columns / horizCharsPerSquare
L1:
    INVOKE WriteColorChar, ' ', white, white
    INVOKE WriteColorChar, ' ', white, white
    INVOKE WriteColorChar, ' ', color, color
    INVOKE WriteColorChar, ' ', color, color
    loop L1

    ret
PrintRowEven ENDP

WriteColorChar PROC USES eax, char:BYTE, forecolor:BYTE, backcolor:BYTE 
    INVOKE SetColor, forecolor, backcolor
    mov al, char
    call WriteChar

    ret
WriteColorChar ENDP

SetColor PROC, forecolor:BYTE, backcolor:BYTE
    movzx eax, backcolor
    shl eax, 4
    or al, forecolor
    call SetTextColor       ; from Irvine32.lib
    ret
SetColor ENDP
END MAIN

1 个答案:

答案 0 :(得分:1)

我不知道函数“setTextColor”,但是可以做出有根据的猜测,它想要一个eax中的颜色​​(默认位置为被调用的子程序放置一个参数)。

在这种情况下,序列

   mov eax, gray
   mov eax, ecx

似乎将一个颜色指示常量加载到eax中,然后用ecx中的随机垃圾替换它。这无法产生有用的结果。你用黑色做同样的错误。

虽然颜色编码很好,但你真正想要的是指示棋盘上的棋子。我在70年代完成了这个汇编程序(哎哟!),使用类似下面的内容(OP填写必要的汇编程序详细信息):

  White equ 0  ; tags a piece as "white"
  Black equ 8  ; tags a piece as "black"
  Empty equ 0  ; empty square
  Pawn equ 1    ; piece codes
  Knight equ 2
  Bishop equ 3
  Rook equ 4
  Queen equ 5
  King equ 6
  CastledKing equ 7 ; you need to distinguish this from an uncasteled king!

  ChessBoard equ $
      byte White+Rook, White+Knight, White+Bishop, White+Queen, White+King, White+Bishop, White+Knight, White+Rook
      byte Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty
      byte Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty
      byte Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty
      byte Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty
      byte Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty
      byte Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty
      byte Black+Rook, Black+Knight, Black+Bishop, Black+Queen, Black+King, Black+Bishop, Black+Knight, Black+Rook

   PieceCharacter byte ".PNBRQKk"


   main:
       call PrintBoard
       xor  eax, eax
       call Exit

  PutSpace:
        mov  eax, 0x20
        call  PutCharacter
        ret

  PutNewline:
        mov   eax, 0x0d
        call  PutCharacter
        mov   eax, 0x0a
        call  PutCharacter
        ret

   PrintBoard:
        call  PutNewline
        push  7 ; row count
    printrow:
        push  7 ; column count
    printsquarecontent:
        mov   eax, 4[esp] ; row number
        shl   eax, 3
        add   eax, 0[esp] ; column number
        mov   eax, ChessBoard[eax] ; get piece on board
        test  eax, eax
        jne   printpiece
        call   PutSpace
        mov    eax, '.'
        call   PutCharacter
        jmp    printnextpiece

    printpiece:
        push   eax   ; save piece
        test   eax, Black
        mov    eax, 'w'
        je     printpiececolor
        mov    eax, 'b'
    printpiececolor:
        call    PutCharacter
        pop     eax
        and     eax, 0x7   ; extract just the piece number
        mov     eax, PieceCharacter[eax]
        call    PutCharacter

   printnextpiece:
        call    PutSpace
        dec     0[esp]
        jns     printsquarecontent
        lea     esp, 4[esp] ; pop useless column count
        dec     0[esp]
        jnz     printrow
        lea     esp, [esp]
        ret

这应该打印如下:

        bR bN bB bQ bK bB bN bR
         .  .  .  .  .  .  .  .
         .  .  .  .  .  .  .  .
         .  .  .  .  .  .  .  .
         .  .  .  .  .  .  .  .
         .  .  .  .  .  .  .  .
         .  .  .  .  .  .  .  .
        wR wN wB wQ wK wB wN wR

不是特别优雅,但可读性足以下棋。 OP可以使用颜色更改而不是“b”或“w”。