[英]Output Chessboard to Console in Assembly x86?
我正在研究x86的Assembly,并试图在控制台中“绘制”或输出8x8白色和灰色棋盘。 我还是汇编语言的新手,我运气不佳:/想知道是否有在x86汇编编程方面有更多经验的人可以帮助并指出正确的方向?
我做了一个程序,然后以多种背景色/原色输出了一组“ char”,这就是我将以下内容拼凑在一起的地方:
编辑 :当我发现颜色问题时更新了代码。
EDIT2 :更新了代码以按照炮手的建议使用循环。
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次(不匹配的图块颜色)
我正在尝试在控制台中创建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
我不知道函数“ 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”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.