Distributed bombs over the feild

This commit is contained in:
Afonso Clerigo Mendes de Sousa 2025-03-19 23:16:18 +00:00
parent af6d36281b
commit 17b693b71d
2 changed files with 74 additions and 33 deletions

BIN
bin/main

Binary file not shown.

View File

@ -1,9 +1,24 @@
section .data
tile db "[ ]", 0
tile_len equ $ - tile
tile_emply db "[.]", 0
tile_flag db "[!]", 0
tile_mine db "[X]", 0
tile_near_by1 db "[1]", 0
tile_near_by2 db "[2]", 0
tile_near_by3 db "[3]", 0
tile_near_by4 db "[4]", 0
tile_near_by5 db "[5]", 0
tile_near_by6 db "[6]", 0
tile_near_by7 db "[7]", 0
tile_near_by8 db "[8]", 0
tile_near_by9 db "[9]", 0
newline db 0xA, 0
newline_len equ $ - newline
section .bss
board resb 100
@ -11,50 +26,76 @@ section .text
global _main
_main:
xor r10, r10 ; Clear r10 for use as a counter
mov rbx, 10 ; Set divisor to 10
; Fill the board with 0s and 1s
xor r10, r10 ; Counter
lea r11, [rel board] ; Load address of board
lea r8, [rel tile] ; Buffer address
mov r9, tile_len ; Length of the string
mov rcx, 100 ; Loop 100 times
fill_loop:
rdrand rax
and rax, 1
mov [r11 + r10], al ; Store at board[r10]
inc r10
loop fill_loop
loop_start:
cmp r10, 100
jae loop_end
inc r10 ; Increment the counter
; Print the board
xor r10, r10 ; Reset counter
print_loop:
lea r11, [rel board] ; Load address of board
; Determine which tile to print based on board[r10]
lea r8, [rel tile] ; Default tile
mov r9, tile_len
; Add logic here to select tile based on board[r10] if needed
cmp byte [r11 + r10], 0
je empty_tile
cmp byte [r11 + r10], 1
je mine_tile
; If nothign else, print the tile
jmp no_tile
no_tile:
lea r8, [rel tile]
jmp print_tile
empty_tile:
lea r8, [rel tile_emply]
jmp print_tile
mine_tile:
lea r8, [rel tile_mine]
jmp print_tile
print_tile:
mov r9, tile_len
call print_String
; Check if newline is needed (every 10th element)
mov rax, r10
xor rdx, rdx ; Clear rdx for division
div rbx ; Divide rax by rbx (unsigned division)
cmp r10, 0
je no_newline
cmp rdx, 0
xor rdx, rdx
div rbx
cmp rdx, 9 ; After 0-9, print newline
jne no_newline
lea r8, [rel newline] ; Newline character
mov r9, newline_len ; Length of the newline
lea r8, [rel newline]
mov r9, newline_len
call print_String
lea r8, [rel tile] ; Buffer address
mov r9, tile_len ; Length of the string
no_newline:
jmp loop_start
loop_end:
mov rax, 0x2000001 ; syscall: exit
xor rdi, rdi ; status: 0
no_newline:
inc r10
cmp r10, 100
jb print_loop
; Exit
mov rax, 0x2000001
xor rdi, rdi
syscall
; Function to print a string
; Arguments:
; r8 - pointer to the string
; r9 - length of the string
print_String:
mov rax, 0x2000004 ; syscall: write
mov rdi, 1 ; file descriptor: stdout
mov rsi, r8 ; pointer to the string
mov rdx, r9 ; length of the string
mov rax, 0x2000004
mov rdi, 1
mov rsi, r8
mov rdx, r9
syscall
ret