add draw condition and fix not-so good but also not terrible issue with rbx register not being reserved in makeAIMove

This commit is contained in:
2025-02-05 23:05:00 +00:00
parent 0a4c0d9824
commit c358285b4f
2 changed files with 25 additions and 9 deletions

2
.vscode/tasks.json vendored
View File

@@ -4,7 +4,7 @@
{ {
"label": "asm64", "label": "asm64",
"type": "shell", "type": "shell",
"command": "mkdir -p out; mkdir -p bin; nasm -F dwarf -g -f elf64 game.s -o out/game.o; ld -m elf_x86_64 -o bin/game out/game.o;", "command": "mkdir -p out && mkdir -p bin; nasm -F dwarf -g -f elf64 game.s -o out/game.o && ld -m elf_x86_64 -o bin/game out/game.o",
"group": { "group": {
"kind": "build", "kind": "build",
"isDefault": true "isDefault": true

32
game.s
View File

@@ -1,11 +1,9 @@
; TODO out of moves check
global _start global _start
section .text section .text
checkVictory: checkVictory:
mov rax, 0 mov eax, 0
mov cl, [fieldBuffer + 12] mov cl, [fieldBuffer + 12]
and cl, [fieldBuffer + 14] and cl, [fieldBuffer + 14]
@@ -64,25 +62,26 @@ checkVictory:
makeAIMove: makeAIMove:
rdtsc rdtsc
mov rbx, rax mov r8, rax
.loop: .loop:
; RAND from wiki https://en.wikipedia.org/wiki/Linear_congruential_generator. Numerical Recipes ranqd1, Chapter 7.1, §An Even Quicker Generator, Eq. 7.1.6 parameters from Knuth and H. W. Lewis ; RAND from wiki https://en.wikipedia.org/wiki/Linear_congruential_generator. Numerical Recipes ranqd1, Chapter 7.1, §An Even Quicker Generator, Eq. 7.1.6 parameters from Knuth and H. W. Lewis
mov rdx, 1664525 mov rdx, 1664525
mul rdx mul rdx
add rax, 1013904223 add rax, 1013904223
mov rbx, rax mov r8, rax
; clamping to 1-3 range and moving to rdi ; clamping to 1-3 range and moving to rdi
mov rsi, 3 mov rsi, 3
mov rax, 0 mov rax, 0
mov ah, bh mov al, r8b
mov dx, 0 mov dx, 0
div si ;TODO for some reason div sil causes division by zero div si ;TODO for some reason div sil causes division by zero
inc dx inc dx
mov di, dx mov di, dx
shl di, 8 shl di, 8
mov rax, 0 mov rax, 0
mov ah, bl mov ax, r8w
shr ax, 8
mov dx, 0 mov dx, 0
div si div si
inc dx inc dx
@@ -91,7 +90,7 @@ makeAIMove:
call placeChar call placeChar
cmp rax, 0 cmp rax, 0
je .return je .return
mov rax, rbx mov rax, r8
jmp .loop jmp .loop
.return: .return:
@@ -175,6 +174,7 @@ _start:
mov rdx, readInputsMessageLen ; size mov rdx, readInputsMessageLen ; size
syscall ; syscall ;
mov rbp, 0 ; number of moves done
.loop: .loop:
;reading input ;reading input
call readXY call readXY
@@ -196,12 +196,17 @@ _start:
; failed to place a char ; failed to place a char
jne .printOccupiedError jne .printOccupiedError
inc rbp
;check win condition ;check win condition
mov rdi, 'x' mov rdi, 'x'
call checkVictory call checkVictory
cmp rax, 0 cmp rax, 0
jne .printYouWon jne .printYouWon
cmp rbp, 5 ; player did 5 moves, time to go
je .printDraw
;drawing field again ;drawing field again
call drawField call drawField
@@ -274,6 +279,15 @@ _start:
syscall ; syscall ;
jmp .exit jmp .exit
.printDraw:
call drawField
mov rax, 1 ; sys_write
mov rdi, 1 ; out
mov rsi, drawMessage ; str
mov rdx, drawMessageLen ; size
syscall ;
jmp .exit
.printYouLost: .printYouLost:
call drawField call drawField
mov rax, 1 ; sys_write mov rax, 1 ; sys_write
@@ -301,6 +315,8 @@ section .rodata
youWonMessageLen: equ $ - youWonMessage youWonMessageLen: equ $ - youWonMessage
youLostMessage: db "you lost T_T", 0xA youLostMessage: db "you lost T_T", 0xA
youLostMessageLen: equ $ - youLostMessage youLostMessageLen: equ $ - youLostMessage
drawMessage: db "it's a draw", 0xA
drawMessageLen: equ $ - drawMessage
aiThinkingMessage: db "AI thinking" aiThinkingMessage: db "AI thinking"
aiThinkingMessageLen: equ $ - aiThinkingMessage aiThinkingMessageLen: equ $ - aiThinkingMessage
aiThinkingDotMessage: db "." aiThinkingDotMessage: db "."