Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- section .data
- grid: times 32 db 0
- turn: db 0
- section .text
- global _start
- _start:
- ; ===== Setup =====
- ; White begins
- mov [turn], byte 1
- ; Setup white pieces
- mov [grid + 0], byte 1
- mov [grid + 1], byte 1
- mov [grid + 2], byte 1
- mov [grid + 3], byte 1
- mov [grid + 4], byte 1
- mov [grid + 5], byte 1
- mov [grid + 6], byte 1
- mov [grid + 7], byte 1
- mov [grid + 8], byte 1
- mov [grid + 9], byte 1
- mov [grid + 10], byte 1
- mov [grid + 11], byte 1
- ; Setup black pieces
- mov [grid + 20], byte 2
- mov [grid + 21], byte 2
- mov [grid + 22], byte 2
- mov [grid + 23], byte 2
- mov [grid + 24], byte 2
- mov [grid + 25], byte 2
- mov [grid + 26], byte 2
- mov [grid + 27], byte 2
- mov [grid + 28], byte 2
- mov [grid + 29], byte 2
- mov [grid + 30], byte 2
- mov [grid + 31], byte 2
- ; =================
- ; TODO: Print inital grid
- .game_loop:
- ; TODO: Get user input
- ; TODO: Check if input is valid
- ; Example:
- mov al, 9 ; From square
- mov bl, 14 ; To square
- ; ===== Check if input is playable and play the move =====
- ; al - Source square index
- ; bl - Destination square index
- ; dl - Difference of al and bl
- ; r8 - Source square pointer
- ; r9 - Destination square pointer
- ; r10 - Source square row
- ; r11 - Source square column
- ; r12 - Square in the middle of source and destination (if applicable)
- ; Check if the target square is not occupied
- movzx r8, al
- movzx r9, bl
- add r8, grid
- add r9, grid
- cmp [r9], byte 0 ; check if destination is empty
- jne .game_loop ; jump if it's occupied
- mov dl, al
- sub dl, bl
- ; These are always valid, no matter where the square is
- cmp dl, -4
- je .move
- cmp dl, 4
- je .move
- ; Now there comes an inconsistency where squares in odd cells
- ; have the valid distance -3 and 5 but not 3 and -5
- ; That's why we check for an even or odd row
- movzx r10, al ; r10 - row index
- shr r10, 2 ; Divide by 4
- test r10, 1
- jz .check_even_row ; Jump if even
- .check_odd_row:
- cmp dl, -3
- je .move
- cmp dl, 5
- je .move
- jmp .check_double
- .check_even_row:
- cmp dl, 3
- je .move
- cmp dl, -5
- je .move
- .check_double:
- movzx r11, al
- ; Note: Modulo can be fully replaced with and only when
- ; the modulo operand (4 in this case) is a power of two.
- and r11, 0x3 ; Modulo with 4
- ; if(row <= 1 || col <= 1)
- cmp r10, 1
- setle ah
- cmp r11, 1
- setle bh
- or ah, bh
- jz .not_check_neg9
- .check_neg9:
- cmp dl, -9
- je .check_move_double
- .not_check_neg9:
- ; if(row <= 1 || col >= 6)
- cmp r10, 1
- setle ah
- cmp r11, 6
- setge bh
- or ah, bh
- jz .not_check_neg7
- .check_neg7:
- cmp dl, -7
- je .check_move_double
- .not_check_neg7:
- ; if(row >= 6 || col <= 1)
- cmp r10, 6
- setge ah
- cmp r11, 1
- setle bh
- or ah, bh
- jz .not_check_neg7
- .check_pos7:
- cmp dl, 7
- je .check_move_double
- .not_check_pos7:
- ; if(row >= 6 || col >= 6)
- cmp r10, 6
- setge ah
- cmp r11, 1
- setle bh
- or ah, bh
- jz .not_check_pos9
- .check_pos9:
- cmp dl, 9
- je .check_move_double
- .not_check_pos9:
- ; If we reach this point, the move is not playable
- jmp .game_loop
- ; Additional check to see if the piece in the middle is an enemy
- .check_move_double:
- mov ch, [turn]
- ; Get the middle piece
- ; r14 = ((row ^ 1) + source index + destination index) / 2
- mov r13, r10
- xor r13, 1
- movzx r14, al
- movzx r15, bl
- add r14, r15
- add r14, r13
- shr r14, 1
- ; cl = grid[r14] & 0x3
- add r14, grid
- mov cl, [r14]
- and cl, 0x3
- jz .game_loop ; The middle is not occupied
- xor ch, cl
- jz .game_loop ; The piece in the middle is of the same color
- mov [r12], byte 0 ; Clear the middle piece
- .move:
- mov al, [r8]
- mov [r9], al
- mov [r8], byte 0
- ; ================================================
- ; TODO: Print grid
- ; TODO: Check for win
- jmp .game_loop
- ; Exit the program
- mov rax, 60 ; syscall number for sys_exit
- xor rdi, rdi ; exit code 0
- syscall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement