博客文章

Assembly Language For x86 Processors 8.11.2 Chess Board

作者: andy.      时间: 2021-05-09 22:39:28

Write a program that draws an 8  8 chess board, with alternating gray and white squares. You can use the SetTextColor and Gotoxy procedures from the Irvine32 library. Avoid the use of global variables, and use declared parameters in all procedures. Use short procedures that are focused on a single task.

After I finished this work, I think this implementations pretty good, so recorded. I have add some comments, if any other ideas pls add comment and dicuss together.


INCLUDE Irvine32.inc

;.386
;.model flat, stdcall
;.stack 4096
;ExitProcess proto, dwExitCode: dword

WriteBlock proto
.data
ChessBoardSize dword 6
.code
main PROC
	CellSize EQU 4
	mov ecx, ChessBoardSize
	LRow:
		mov eax, ChessBoardSize
		sub eax, ecx
		push ecx
		mov ecx, ChessBoardSize
		LColumn:
			push eax
			mov ebx, ChessBoardSize
			sub ebx, ecx
			push ebx
			push CellSize
			call WriteBlock
		loop LColumn
		pop ecx
	loop LRow
	
	mov dl, 0
	mov eax, CellSize
	mov ebx, ChessBoardSize
	mul ebx
	mov dh, al
	call Gotoxy

	mov eax, 0Fh
	call SetTextColor
exit
main ENDP

WriteBlock proc
	CellX EQU [ebp + 16]
	CellY EQU [ebp + 12]
	InCellSize EQU [ebp + 8]
	push ebp
	mov ebp, esp
	; maybe local variables
	push eax
	push ebx
	push ecx
	
	mov eax, CellX
	xor eax, CellY
	shr eax, 1
	jc SetColor1
	mov eax, 070h
	jmp ContinueWriteBlock
	SetColor1:
	mov eax, 0F0h
	ContinueWriteBlock:
	call SetTextColor

	shl dword ptr CellX, 1

	; start y
	mov eax, InCellSize
	mul dword ptr CellY
	mov ebx, eax
	
	; start x
	mov eax, InCellSize
	mul dword ptr CellX

	mov ecx, InCellSize
	LWriteBlockRow:
		; set y of cursor
		mov dh, cl
		dec dh
		add dh, bl
		push ecx
		mov ecx, InCellSize
		LWriteBlockColumn:
			; set x of cursor
			mov dl, cl
			dec dl
			shl dl, 1
			add dl, al
			call Gotoxy
			push eax
			mov al, 20h
			call WriteChar
			call WriteChar
			pop eax
		loop LWriteBlockColumn
		pop ecx
	loop LWriteBlockRow

	pop ecx
	pop ebx
	pop eax
	pop ebp
ret 12
WriteBlock endp

END main

image.png