Skip to content

Instantly share code, notes, and snippets.

@yescallop
Last active June 2, 2022 08:31
Show Gist options
  • Save yescallop/343893d43ad5339bb559f79f30dc5df7 to your computer and use it in GitHub Desktop.
Save yescallop/343893d43ad5339bb559f79f30dc5df7 to your computer and use it in GitHub Desktop.
Connect6 implementation (partial) in 8086 assembly.
.8086
.model small
.stack 100h
.data
board_size equ 19
board_len equ (board_size * board_size)
board db (board_len / 2) dup (0), 1, (board_len / 2) dup (0)
stone db 2
header db 0dh, 0ah, ' A B C D E F G H I J K L M N O P Q R S', 0dh, 0ah, '$'
prompt_black db '[Black] Please move: $'
prompt_white db '[White] Please move: $'
invalid_msg db 0dh, 0ah, "Invalid point reference!", 0dh, 0ah, '$'
occupied_msg db 0dh, 0ah, "This slot is occupied!", 0dh, 0ah, '$'
buffer db 6 dup (?)
.code
main proc far
push ds
xor ax, ax
push ax
mov ax, @data
mov ds, ax
turn:
rept 2
; Print the board and prompt for input.
call print_board
call input
mov al, [stone]
mov byte ptr [bx + board], al
endm
xor [stone], 3
jmp turn
ret
main endp
print_board proc
; Print the header.
lea dx, header
mov ah, 9
int 21h
; Print the rows.
mov ah, 2
lea bx, board
xor cl, cl
next_row:
cmp cl, 9
jb less
; Row 10-19
mov dl, '1'
int 21h
mov dl, ('0' - 9)
jmp print_slots
less:
; Row 1-9
mov dl, ' '
int 21h
mov dl, '1'
print_slots:
add dl, cl
int 21h
; Print the slots in the row.
mov ch, board_size
next_slot:
mov dl, ' '
int 21h
mov dl, '-'
; 0='-',1='X',2='0'
cmp byte ptr [bx], 1
jb print_slot
je black
mov dl, '0'
jmp print_slot
black:
mov dl, 'X'
print_slot:
int 21h
inc bx
dec ch
jnz next_slot
; Print a new line.
mov dl, 0dh
int 21h
mov dl, 0ah
int 21h
inc cl
cmp cl, board_size
jne next_row
ret
print_board endp
input proc ; out(bx)
lea dx, prompt_black
cmp [stone], 1
je print_prompt
lea dx, prompt_white
print_prompt:
mov ah, 9
int 21h
; Read user input for at most 6 bytes (`\r\n` included).
xor bx, bx
mov cx, 6
lea dx, buffer
mov ah, 3fh
int 21h
; Check if the input is valid (`ax` bytes read).
sub ax, 4
cmp ax, 1
ja invalid
mov ah, [buffer + 1]
jne parse_y ; al = 0
cmp ah, '1'
jne invalid
mov ah, [buffer + 2]
mov al, 10
parse_y:
; Parse y coordinate.
sub ah, '0'
cmp ah, 10
jae invalid
add al, ah
jz invalid
dec al
mov ah, board_size
mul ah
; Parse x coordinate.
xor bh, bh
mov bl, [buffer]
or bl, 20h
sub bl, 'a'
cmp bl, board_size
jae invalid
add bx, ax
lea dx, occupied_msg
cmp byte ptr [bx + board], 0
jne occupied
ret
invalid:
lea dx, invalid_msg
occupied:
mov ah, 9
int 21h
jmp input
input endp
end main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment