From af6d36281b364a044746b5f805b7f6d7522655f8 Mon Sep 17 00:00:00 2001 From: AfonsoCMSousa Date: Wed, 19 Mar 2025 22:34:07 +0000 Subject: [PATCH 1/6] Create grid. --- bin/main | Bin 16536 -> 33040 bytes src/main.asm | 49 +++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/bin/main b/bin/main index 5830395c3e7e11c6fc84cffb2420355518dabae6..a2cec381f7c24b23d9667a6e1f8ef0badc212275 100755 GIT binary patch delta 766 zcmbQy$T*>iN$mWuxBu7~7#Nuu7#Nsgabq&1CRy-G#X?y%y^J|e0)k`Ng{{^0l3U#gDG%;F&HMlW)jtmk54Kt zh6`c3hYhG6%a{%cb zKso|Qa|3Yz5Q6|K6ob^VPjpnDyn#_*vH_EZ=F8J7^?VhKuP$5{lPAKH+^PapAOOS+ z>|9{1!NAY}GTdl#BU6B21H=YRpa4jbMZ@NgOc~bozJ`}}1C=xQ^s?>;QjR-WfTU;h zuasgt&;N&BIDn)&FL)SU^7#H4D9liz>H#E6<$>xagVlqa^+FP)te3SGECMp&^`0HT z0Acbl{Ljz211P|>9~3@5oyUASPw}%J261G7m|^lpdtqKsygL9nAUt*QMSFeLXoXk? zE}#R~+H7L>XBJ?LXAlC)frE>gVN=~?K?nKzO?4n41_oUq=3``F>V&2q3n+gol^2YHB}5gd@C=BFr0Wk>7oA|JJvJI1{ zqyt2p0Z4-Zl%BybIg82O0?0D}3WLm&0AdgyWOfFS28nY6aR3k_1CUy#$q$+01?KjC zl>63aUAZnTbIp&dwVx&jFiSAbo1DoUAn3rrzyVaJ3B&?GY~ir^B6Fhk<_FBtj9fc_ z?qu>X{Ljxid4;_UFUaLU number of bombs around + lea r8, [rel tile] ; Buffer address + mov r9, tile_len ; Length of the string +loop_start: + cmp r10, 100 + jae loop_end + + inc r10 ; Increment the counter + + call print_String + + 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 + jne no_newline + lea r8, [rel newline] ; Newline character + mov r9, newline_len ; Length of the newline + 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 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 + syscall + ret \ No newline at end of file From 17b693b71d517e41b17ab019b4ce4a47ff57cf01 Mon Sep 17 00:00:00 2001 From: AfonsoCMSousa Date: Wed, 19 Mar 2025 23:16:18 +0000 Subject: [PATCH 2/6] Distributed bombs over the feild --- bin/main | Bin 33040 -> 33552 bytes src/main.asm | 107 +++++++++++++++++++++++++++++++++++---------------- 2 files changed, 74 insertions(+), 33 deletions(-) diff --git a/bin/main b/bin/main index a2cec381f7c24b23d9667a6e1f8ef0badc212275..12741b79038139c0ef2b6e802ada7e8782ab4136 100755 GIT binary patch delta 1041 zcmY+@O=uHA6ae72vk_XWCMKd$tX7ENp;gl+n}o_njbJJ%l!_qKvZgeZ#BCDd7RjM* zOz@vTbP+sw5Dy-_#zT7$wFe8HVox5V)LvYupx~hp5Betau>oeuX>wyJpO!hNdZRX@#!{%skB~|Y|hl?yu;~Po$@~>oVO-kH% z`9}UOt^XAH0OnKmZQrrt%%ThXMCFtZ3_pKJM&AZce)^x>ID~w+MK^L`i(rQksRylO za3zr3{GF_|yXU-E^R`pN4{rOg!~I8U?dej*dK8c0fvt~mH8^p65+;sdT*e4u3}Osn zgfPMw3PuDYiV>TH?aj|}dbqhJUw3&V4ZOH6@Ev5Z=dIn9!=^pYrvL^pc1gfC>GzM2 zd*b4IoF$(iUm&aGOXN|qM!rJMk*nl6VUszZCD6rotk<_$5-7c?WQXCO0MG|FN)o6Tp$NYr)mSeMV-)G!$YtTvZPQw-Sa>v??o)#I6uO;U4W+q+IrpiI0(4a#@4uu>FJES-iaVY9g4CvfD NVXBZW@qZz2{{R;u=R*Jh delta 369 zcmbQx#x$XcX~G2|BYOr0Mg|6kG9U&4Bm0Su?lH1WKFH_}BsrN97#SyLF$Jk|0OfWx zFfc>_X>K460Adheg<>Wk%|7`dQ@p^-(<}9S6^ySgTo;ok!js%OIe=M$$;f_l7IU5T zKIg7BGGp!^^Z lA80?@ Date: Thu, 20 Mar 2025 19:00:03 +0000 Subject: [PATCH 3/6] Distributed b --- ASM_CHEATSHEET.md | 229 ++++++++++++++++++++++++++++++++++++++++++++++ bin/main | Bin 33552 -> 33656 bytes src/main.asm | 44 ++++++--- 3 files changed, 262 insertions(+), 11 deletions(-) create mode 100644 ASM_CHEATSHEET.md diff --git a/ASM_CHEATSHEET.md b/ASM_CHEATSHEET.md new file mode 100644 index 0000000..25745d7 --- /dev/null +++ b/ASM_CHEATSHEET.md @@ -0,0 +1,229 @@ +# Registers + +## General Purpose + +| Register | Usage | +|----------|--------------------------------------------| +| rax | Accumulator for general use and syscall number | +| eax | Lower 32 bits of rax | +| ax | Lower 16 bits of rax | +| al | Lower 8 bits of ax | +| rbx | Base register | +| ebx | Lower 32 bits of rbx | +| bx | Lower 16 bits of rbx | +| bl | Lower 8 bits of bx | +| rcx | Counter register (loops) | +| ecx | Lower 32 bits of rcx | +| cx | Lower 16 bits of rcx | +| cl | Lower 8 bits of cx | +| rdx | Data register | +| edx | Lower 32 bits of rdx | +| dx | Lower 16 bits of rdx | +| dl | Lower 8 bits of dx | + +## Index and Pointer + +| Register | Usage | +|----------|--------------------------------------------| +| rsi | Source for data (string operations) | +| esi | Lower 32 bits of rsi | +| si | Lower 16 bits of rsi | +| sil | Lower 8 bits of si | +| rdi | Destination for data | +| edi | Lower 32 bits of rdi | +| di | Lower 16 bits of rdi | +| dil | Lower 8 bits of di | +| rsp | Stack pointer | +| esp | Lower 32 bits of rsp | +| sp | Lower 16 bits of rsp | +| spl | Lower 8 bits of sp | +| rbp | Base pointer for stack frames | +| ebp | Lower 32 bits of rbp | +| bp | Lower 16 bits of rbp | +| bpl | Lower 8 bits of bp | + +## Extended + +| Register | Usage | +|----------|--------------------------------------------| +| r8–r15 | Extra general-purpose registers | +| r8d–r15d | Lower 32 bits of r8–r15 | +| r8w–r15w | Lower 16 bits of r8–r15 | +| r8b–r15b | Lower 8 bits of r8–r15 | + +# Data Movement + +| Command | Description | +|-------------------|------------------------------------------| +| mov dest, src | Copy data from src to dest | +| movzx dest, src | Zero-extend src into dest | +| movsx dest, src | Sign-extend src into dest | +| lea dest, [addr] | Load effective address into dest | +| rel addr | Relative address | +| push src | Push value of src onto the stack | +| pop dest | Pop value from the stack into dest | +| pusa | Pushes all registers to the stack | +| popa | Pops all registers from the stack | +| xchg dest, src | Exchange values of dest and src | +| cmovcc dest, src | Conditional move | +| movs dest, src | Move string | +| movsb | Move byte from [rsi] to [rdi] | +| movsw | Move word from [rsi] to [rdi] | +| movsd | Move doubleword from [rsi] to [rdi] | +| movsq | Move quadword from [rsi] to [rdi] | + +# Arithmetic + +| Command | Description | +|-------------------|------------------------------------------| +| add dest, src | Add src to dest | +| sub dest, src | Subtract src from dest | +| imul dest, src | Multiply dest by src (signed) | +| mul src | Multiply rax by src (result in rdx:rax) | +| idiv src | Divide rdx:rax by src (quotient in rax, remainder in rdx) | +| xor dest, src | XOR (useful for clearing, e.g., xor rax, rax) | +| inc reg | Increment reg by 1 | +| dec reg | Decrement reg by 1 | +| adc dest, src | Add with carry | +| sbb dest, src | Subtract with borrow | +| neg dest | Negate (two's complement) | +| div src | Unsigned divide rdx:rax by src | +| cbw | Convert byte to word | +| cwd | Convert word to doubleword | +| cdq | Convert doubleword to quadword | +| cqo | Convert quadword to octword | + +# Logical + +| Command | Description | +|-------------------|------------------------------------------| +| and dest, src | Bitwise AND | +| or dest, src | Bitwise OR | +| xor dest, src | Bitwise XOR | +| not dest | Bitwise NOT | +| shl dest, imm | Shift bits in dest left by imm | +| shr dest, imm | Shift bits in dest right by imm | +| sal dest, imm | Arithmetic shift left | +| sar dest, imm | Arithmetic shift right | +| rol dest, imm | Rotate bits left | +| ror dest, imm | Rotate bits right | +| rcl dest, imm | Rotate through carry left | +| rcr dest, imm | Rotate through carry right | +| test dest, src | Test bits (AND without storing result) | + +# Control Flow + +| Command | Description | +|-------------------|------------------------------------------| +| jmp label | Unconditional jump to label | +| cmp op1, op2 | Compare op1 and op2 | +| je label | Jump if equal | +| jne label | Jump if not equal | +| jg label | Jump if greater (signed) | +| jl label | Jump if less (signed) | +| jge label | Jump if greater or equal (signed) | +| jle label | Jump if less or equal (signed) | +| ja label | Jump if above (unsigned) | +| jb label | Jump if below (unsigned) | +| jae label | Jump if above or equal (unsigned) | +| jbe label | Jump if below or equal (unsigned) | +| call label | Call a function at label | +| ret | Return from a function | +| loop label | Loop to label | +| loope label | Loop while equal | +| loopne label | Loop while not equal | +| jecxz label | Jump if ecx is zero | +| jrcxz label | Jump if rcx is zero | + +# Syscall Interface (Linux/macOS) + +| Register | Description | +|----------|--------------------------------------------------| +| rax | Syscall number | +| rdi | First argument | +| rsi | Second argument | +| rdx | Third argument | +| r10 | Fourth argument | +| r8 | Fifth argument | +| r9 | Sixth argument | + +# Common Syscall Numbers + +| Syscall | rax | Description | +|-----------|-----------|--------------------------------------| +| sys_exit | 0x2000001 | Exit program | +| sys_write | 0x2000004 | Write to file descriptor | +| sys_read | 0x2000003 | Read from file descriptor | + +# Stack Manipulation + +| Command | Description | +|-----------|--------------------------------------------------| +| push reg | Push register value onto the stack | +| pop reg | Pop top of stack into register | +| call addr | Call a subroutine at addr | +| ret | Return from subroutine | +| enter imm, imm | Create stack frame | +| leave | Destroy stack frame | + +# String Operations + +| Command | Description | +|-----------|--------------------------------------------------| +| movsb | Move byte from [rsi] to [rdi] | +| movsw | Move word from [rsi] to [rdi] | +| movsd | Move doubleword from [rsi] to [rdi] | +| movsq | Move quadword from [rsi] to [rdi] | +| stosb | Store byte from al to [rdi] | +| stosw | Store word from ax to [rdi] | +| stosd | Store doubleword from eax to [rdi] | +| stosq | Store quadword from rax to [rdi] | +| lodsb | Load byte from [rsi] into al | +| lodsw | Load word from [rsi] into ax | +| lodsd | Load doubleword from [rsi] into eax | +| lodsq | Load quadword from [rsi] into rax | +| scasb | Scan byte in al against [rdi] | +| scasw | Scan word in ax against [rdi] | +| scasd | Scan doubleword in eax against [rdi] | +| scasq | Scan quadword in rax against [rdi] | +| cmpsb | Compare byte at [rsi] with byte at [rdi] | +| cmpsw | Compare word at [rsi] with word at [rdi] | +| cmpsd | Compare doubleword at [rsi] with doubleword at [rdi] | +| cmpsq | Compare quadword at [rsi] with quadword at [rdi] | + +# Data Definition + +| Command | Description | +|-----------|--------------------------------------------------| +| db value | Define byte (1 byte) | +| dw value | Define word (2 bytes) | +| dd value | Define doubleword (4 bytes) | +| dq value | Define quadword (8 bytes) | +| dt value | Define ten bytes (80 bits) | + +# Examples: + + assembly: + + section .data + byteVar db 0x1 ; Define a byte variable + wordVar dw 0x1234 ; Define a word variable + dwordVar dd 0x12345678 ; Define a doubleword variable + qwordVar dq 0x123456789ABCDEF0 ; Define a quadword variable + + section .text + global main + + main: + mov al, [byteVar] ; Move byteVar into al + mov ax, [wordVar] ; Move wordVar into ax + mov eax, [dwordVar] ; Move dwordVar into eax + mov rax, [qwordVar] ; Move qwordVar into rax + + +# Tips for Debugging + +- Use xor rax, rax or mov rax, 0 to zero out rax. +- Use comments liberally to track which registers hold what values! +- Step through your code with gdb or an equivalent debugger to watch registers change. +- If things seem off, double-check your mov, cmp, and syscall logic. diff --git a/bin/main b/bin/main index 12741b79038139c0ef2b6e802ada7e8782ab4136..579c55695fcdc4653f92094357fc2024f227e38d 100755 GIT binary patch delta 910 zcmY+@O=uHA6ae72vxzoUo7h@fYNM4_(bB3BODM5TVih+v!Gb7-Qr6lASqQr!-Do{% z+60O}1S(?@k9rpmDM;|*Mzr2cb5~nI4CEqsu=Wz)xbKpMnR(yLo5$=N z(H5zfzE|zEi|{dIS?&Fs|yDS389& zi{Kyz>mq2y;3Hhd$c51lF-1kIBZ%n3vf`_}ZN^af^IOU7xYA-r2^}tqSF3hlhi8lA zcIMpK8})jfKMoE{7XJ_oIgd=k)B?045gXGiXm~bB!JWCTQB@B^o3FA-A&tEJZ#@n@)ZuhI{`FKiA7_&2`X6TSi rX}YPJS|Sd*(YVILSudZ>KKC{5XpovT=0T6&NoaZ;ZW=Qw@jrlnpYpTl delta 776 zcmYMyO=uHA6bJA(vk{t9Ol*oqF_J?3IJ9aYjn+yNt>9KtC>Eihva~dZl@cYUdXjvh z^@Dt!8wbYIePC-kQ-iqsM4Iw71jM@pK#Q>WXgN!5?$!xI;MQ7{lZuMXsjQv zHjKK*FD&ZdNu;M6er{KfzS$F_G4-dl@d=b73 zm*HvG1^EaAWj$bUlX4fFaV6=I{2h+M zP52<(#s|(Dmfi3U_z0{ogpSAIZf(9830M-5UwtYC`P_b%uKTX$>}=l3+fx}eR}QNe W<)@w5%#A5Ko1uv-<2ilxbomDahK&dS diff --git a/src/main.asm b/src/main.asm index 146128d..f89082c 100644 --- a/src/main.asm +++ b/src/main.asm @@ -1,10 +1,9 @@ section .data - tile db "[ ]", 0 - tile_len equ $ - tile + tile_emply db "[ ]", 0 + tile_len equ $ - tile_emply - tile_emply db "[.]", 0 tile_flag db "[!]", 0 - tile_mine db "[X]", 0 + tile_mine db "[x]", 0 tile_near_by1 db "[1]", 0 tile_near_by2 db "[2]", 0 @@ -16,6 +15,10 @@ section .data tile_near_by8 db "[8]", 0 tile_near_by9 db "[9]", 0 + user_location db "[*]", 0 + + user_location_pos db 0 + newline db 0xA, 0 newline_len equ $ - newline @@ -33,31 +36,40 @@ _main: mov rcx, 100 ; Loop 100 times fill_loop: rdrand rax - and rax, 1 + and rax, 13 ; Reduce probability of mine (1 in 16 chance) + cmp rax, 1 + sete al ; Set al to 1 if rax == 1, otherwise 0 mov [r11 + r10], al ; Store at board[r10] inc r10 loop fill_loop - ; 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 + lea r8, [rel tile_emply] ; Default tile mov r9, tile_len ; Add logic here to select tile based on board[r10] if needed + ; if (user_location_x == r10) && (user_location_y == r10) { + ; lea r8, [rel user_location] + ; mov r9, user_location_len + ; call print_String + ; } + + + cmp r10, [rel user_location] + je has_user_location + 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] + lea r8, [rel tile_emply] jmp print_tile empty_tile: @@ -68,14 +80,21 @@ mine_tile: lea r8, [rel tile_mine] jmp print_tile +_user_location: + lea r8, [rel user_location] + jmp print_tile + print_tile: mov r9, tile_len call print_String +no_user_location: + ; Check if newline is needed (every 10th element) mov rax, r10 xor rdx, rdx - div rbx + mov rbx, 10 ; Initialize rbx to 10 + div rbx ; Divide rax by 10 cmp rdx, 9 ; After 0-9, print newline jne no_newline lea r8, [rel newline] @@ -87,6 +106,9 @@ no_newline: cmp r10, 100 jb print_loop + ; Print a cursor to show what tile the user is on + + ; Exit mov rax, 0x2000001 xor rdi, rdi From e56422db894bf3f6ace4f35d42dc2820aeedc995 Mon Sep 17 00:00:00 2001 From: AfonsoCMSousa Date: Thu, 20 Mar 2025 19:01:19 +0000 Subject: [PATCH 4/6] Created player and player position --- src/main.asm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.asm b/src/main.asm index f89082c..2481e99 100644 --- a/src/main.asm +++ b/src/main.asm @@ -61,7 +61,7 @@ print_loop: cmp r10, [rel user_location] - je has_user_location + je has_user_location_pos cmp byte [r11 + r10], 0 je empty_tile From ec320340d954ffb5265524ba2101eac041cd8aef Mon Sep 17 00:00:00 2001 From: AfonsoCMSousa Date: Thu, 20 Mar 2025 20:36:06 +0000 Subject: [PATCH 5/6] Read Input (up,right,down,left). Error: Segmentaion fault to fix... --- bin/main | Bin 33656 -> 33848 bytes src/main.asm | 72 +++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 67 insertions(+), 5 deletions(-) diff --git a/bin/main b/bin/main index 579c55695fcdc4653f92094357fc2024f227e38d..1cdc1c553acb43d4c84f031cd8c3815579b0bfa4 100755 GIT binary patch delta 1160 zcmY+?Ur19?90%}o&$dd-oK_%N`6rbM3xy=Nnx+O$P>_Ni(z+}+b+UFZcXzG_Vb>Xw zgq7WVvWK903Vmp3t{y5W=s{>oL~kZNq!f`vC_=8^*}gAbxI5p^@BGd=m)*ND%x7=% zyU9F99b*z>Oc6hHImVWkB&YdCYNp>TF7sNe*~ib9m(kqy7-O%gZztt;(M(qfZcgK2 zbD7KbXXAg5ooKEddh{nS)qj6miP>%`kQ{NpCF$JWR=wCddT`x*Usn-L(oU_TwmUR5 zB&@XDojYTjCbc-`^rKD2nLSLmi$T)!sjr7-z86xsB7&$~79oqmfC$)RQGPLQDx<11EHZ%M8~h6&mzn!?i}CQ7<)ID-@b^np&c8POn%kprf{O zJ}_3!R&zwQy``B|o{k9-*SAE+l!!mJ%#r%jlEZvguZn*tJHBA~<&1x^e74#pndMW7 ziHEST#DJM+6h#vQqQuy7x_3*AyYY5U!Ci189Dy&u_uvkA0`7(%!zw%p4`&?ZGboIq z;T!w_zEq$2+@8QPJPG%~&*3mU2OID_d<*`Rc9ijP6uycAy;62IbKuX6xfR}kcfjHu zQa|4Xix(_xhY!Jf;Yzpwb_z#n!2uN9Xefp+!ey`*J_2iSC2YV|@NM`c{1C2zpOW{9 zC$2>yE*j{oaheN*Mf44=4R)G0VtHm;Y`?eH6OzLrpC$*CE|2b4g0iZFn5y}Mx~%&H zJ{DBupf3{e2h-7`X-b4$Q~G^!Sj~)HB@)ae&EM0jXJWwDt&63;P?twFCt~x(=|lWM PHB5EvcUAFdUWV%*gcRyE delta 822 zcmZ9~&ubGw6bJCPvkh%kH0cj4Yik9oDDlPaPeM_r*$z83sdmkEq8<+zA|wA zo9Y~k$+sV0Y{{oD<+RylsET9O#^tuT)|?*MN|k$Zaw>Lw{sMPPl|#+EB{rJ4#krL; z?d0|A&F}^r)}?ajFdrsQSC=Naa6iX&m+KlsteOrEaqvM0Z5+JUK`RG8bby}qN%w&C-z{*$_Y8on2}ByJ$_2nF}y zb$A_qrfu>v6f?Hq(?}IntF#PqOOC&mwXrQ1A{tb7-`la#(#1Xg??t_oQ zC*TpdACAMra9X>M8;&AjqhJi4*XEBXDieM$zt4<+Q-1cXQG4ljwrJ<_vl-9HyHqGT ju4j8rE=z9yzcWs_**iJMm8sQFks!>{Og>YbBOZSNi{O)2 diff --git a/src/main.asm b/src/main.asm index 2481e99..1aa3f40 100644 --- a/src/main.asm +++ b/src/main.asm @@ -19,6 +19,10 @@ section .data user_location_pos db 0 + user_input times 3 db 0 + user_input_len equ $ - user_input + + newline db 0xA, 0 newline_len equ $ - newline @@ -59,8 +63,12 @@ print_loop: ; call print_String ; } - - cmp r10, [rel user_location] +no_user_location: + ; Replace this: + ; cmp r10, byte [rel user_location_pos] + ; je has_user_location_pos + movzx rax, byte [rel user_location_pos] + cmp r10, rax je has_user_location_pos cmp byte [r11 + r10], 0 @@ -80,7 +88,7 @@ mine_tile: lea r8, [rel tile_mine] jmp print_tile -_user_location: +has_user_location_pos: lea r8, [rel user_location] jmp print_tile @@ -88,8 +96,6 @@ print_tile: mov r9, tile_len call print_String -no_user_location: - ; Check if newline is needed (every 10th element) mov rax, r10 xor rdx, rdx @@ -108,7 +114,63 @@ no_newline: ; Print a cursor to show what tile the user is on + ; read user input, arrow keys + ; if up arrow, decrement user_location_pos + ; if down arrow, increment user_location_pos + ; if left arrow, decrement user_location_pos + ; if right arrow, increment user_location_pos + ; Read user input + mov rax, 0x2000003 ; syscall: read + mov rdi, 0 ; file descriptor: stdin + lea rsi, [rel user_input] ; buffer to store input + mov rdx, 1 ; number of bytes to read + syscall + + ; Check the input and update user_location_pos accordingly + cmp byte [rel user_input], 0x1B ; Escape sequence + jne no_escape + + ; Read the next two bytes for arrow keys + mov rax, 0x2000003 ; syscall: read + mov rdi, 0 ; file descriptor: stdin + lea rsi, [rel user_input + 1] ; buffer to store input + mov rdx, 2 ; number of bytes to read + syscall + + ; Check for arrow keys + cmp byte [rel user_input + 1], 0x5B + jne no_arrow + + cmp byte [rel user_input + 2], 0x41 ; Up arrow + je move_up + cmp byte [rel user_input + 2], 0x42 ; Down arrow + je move_down + cmp byte [rel user_input + 2], 0x43 ; Right arrow + je move_right + cmp byte [rel user_input + 2], 0x44 ; Left arrow + je move_left + +no_arrow: + jmp no_user_location + +move_up: + dec byte [rel user_location_pos] + jmp no_user_location + +move_down: + inc byte [rel user_location_pos] + jmp no_user_location + +move_right: + inc byte [rel user_location_pos] + jmp no_user_location + +move_left: + dec byte [rel user_location_pos] + jmp no_user_location + +no_escape: ; Exit mov rax, 0x2000001 xor rdi, rdi From de5707f28002016efd0575850bff5b32c16cd9f9 Mon Sep 17 00:00:00 2001 From: AfonsoCMSousa Date: Tue, 25 Mar 2025 20:15:02 +0000 Subject: [PATCH 6/6] Refactor tile printing logic and improve mine probability calculation (FINNALY WORKS) --- bin/main | Bin 33848 -> 33864 bytes src/main.asm | 271 +++++++++++++++++++++++++++++---------------------- 2 files changed, 156 insertions(+), 115 deletions(-) diff --git a/bin/main b/bin/main index 1cdc1c553acb43d4c84f031cd8c3815579b0bfa4..a2765ae63dd13d7c769896aaafe88713f7714680 100755 GIT binary patch literal 33864 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 diff --git a/src/main.asm b/src/main.asm index 1aa3f40..01f2526 100644 --- a/src/main.asm +++ b/src/main.asm @@ -2,19 +2,11 @@ section .data tile_emply db "[ ]", 0 tile_len equ $ - tile_emply + tile_emply_dummie 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 - user_location db "[*]", 0 user_location_pos db 0 @@ -25,6 +17,7 @@ section .data newline db 0xA, 0 newline_len equ $ - newline + digit_buffer db 0 section .bss board resb 100 @@ -40,7 +33,7 @@ _main: mov rcx, 100 ; Loop 100 times fill_loop: rdrand rax - and rax, 13 ; Reduce probability of mine (1 in 16 chance) + and rax, 15 ; Reduce probability of mine (1 in 16 chance) cmp rax, 1 sete al ; Set al to 1 if rax == 1, otherwise 0 mov [r11 + r10], al ; Store at board[r10] @@ -51,58 +44,65 @@ fill_loop: 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_emply] ; Default tile - mov r9, tile_len - ; Add logic here to select tile based on board[r10] if needed + xor rcx , rcx ; Reset mine count + lea r11, [rel board] + movzx rax, byte [r11 + r10] ; Load board[r10] - ; if (user_location_x == r10) && (user_location_y == r10) { - ; lea r8, [rel user_location] - ; mov r9, user_location_len - ; call print_String - ; } - -no_user_location: - ; Replace this: - ; cmp r10, byte [rel user_location_pos] - ; je has_user_location_pos - movzx rax, byte [rel user_location_pos] - cmp r10, rax - je has_user_location_pos - - cmp byte [r11 + r10], 0 - je empty_tile - cmp byte [r11 + r10], 1 - je mine_tile - -no_tile: - lea r8, [rel tile_emply] - jmp print_tile - -empty_tile: - lea r8, [rel tile_emply] - jmp print_tile - -mine_tile: - lea r8, [rel tile_mine] - jmp print_tile - -has_user_location_pos: + ; Print user location + cmp r10, [rel user_location_pos] + jne not_user_location lea r8, [rel user_location] - jmp print_tile - -print_tile: mov r9, tile_len call print_String + jmp next_tile +not_user_location: + + ; Check if the tile is a mine + cmp rax, 1 + jne not_mine + + ; Print mine + lea r8, [rel tile_mine] + mov r9, tile_len + call print_String + jmp next_tile + +not_mine: + ; Check if the tile is near a mine + ; int3 ; Insert a breakpoint for GDB + call count_adjacent_mines + cmp dl, 0 + je print_empty + + ; Print number of adjacent mines properly using tile_emply. + lea rdi, [rel tile_emply_dummie] ; Load address of "[ ]" + add dl, '0' ; Convert count to ASCII + mov byte [rdi+1], dl ; Overwrite middle character with digit + mov r8, rdi + mov r9, tile_len + call print_String + jmp next_tile + +print_empty: + ; Print empty tile + lea r8, [rel tile_emply] + mov r9, tile_len + call print_String + jmp next_tile + +next_tile: ; Check if newline is needed (every 10th element) + cmp r10, 0 + je no_newline + mov rax, r10 + inc rax xor rdx, rdx - mov rbx, 10 ; Initialize rbx to 10 - div rbx ; Divide rax by 10 - cmp rdx, 9 ; After 0-9, print newline - jne no_newline + mov rbx, 10 + div rbx + test rdx, rdx ; Check if remainder is 0 + jnz no_newline ; If not, don't print newline lea r8, [rel newline] mov r9, newline_len call print_String @@ -112,74 +112,115 @@ no_newline: cmp r10, 100 jb print_loop - ; Print a cursor to show what tile the user is on - - ; read user input, arrow keys - ; if up arrow, decrement user_location_pos - ; if down arrow, increment user_location_pos - ; if left arrow, decrement user_location_pos - ; if right arrow, increment user_location_pos - - ; Read user input - mov rax, 0x2000003 ; syscall: read - mov rdi, 0 ; file descriptor: stdin - lea rsi, [rel user_input] ; buffer to store input - mov rdx, 1 ; number of bytes to read - syscall - - ; Check the input and update user_location_pos accordingly - cmp byte [rel user_input], 0x1B ; Escape sequence - jne no_escape - - ; Read the next two bytes for arrow keys - mov rax, 0x2000003 ; syscall: read - mov rdi, 0 ; file descriptor: stdin - lea rsi, [rel user_input + 1] ; buffer to store input - mov rdx, 2 ; number of bytes to read - syscall - - ; Check for arrow keys - cmp byte [rel user_input + 1], 0x5B - jne no_arrow - - cmp byte [rel user_input + 2], 0x41 ; Up arrow - je move_up - cmp byte [rel user_input + 2], 0x42 ; Down arrow - je move_down - cmp byte [rel user_input + 2], 0x43 ; Right arrow - je move_right - cmp byte [rel user_input + 2], 0x44 ; Left arrow - je move_left - -no_arrow: - jmp no_user_location - -move_up: - dec byte [rel user_location_pos] - jmp no_user_location - -move_down: - inc byte [rel user_location_pos] - jmp no_user_location - -move_right: - inc byte [rel user_location_pos] - jmp no_user_location - -move_left: - dec byte [rel user_location_pos] - jmp no_user_location - -no_escape: + ; Read arrow keys ; Exit mov rax, 0x2000001 xor rdi, rdi syscall +; r8 = string +; r9 = length print_String: mov rax, 0x2000004 mov rdi, 1 mov rsi, r8 mov rdx, r9 syscall - ret \ No newline at end of file + ret + +; r10 = current tile index +; r11 = board +; rdx = mine count +; r8 = offset +count_adjacent_mines: + xor rdx, rdx + mov r8, 0 + + ; Check top left + mov rax, r10 + sub rax, 11 + cmp rax, 0 + jl .skip_top_left + movzx rax, byte [r11 + rax] + cmp rax, 1 + sete al + add dl, al +.skip_top_left: + + ; Check top + mov rax, r10 + sub rax, 10 + cmp rax, 0 + jl .skip_top + movzx rax, byte [r11 + rax] + cmp rax, 1 + sete al + add dl, al +.skip_top: + + ; Check top right + mov rax, r10 + sub rax, 9 + cmp rax, 0 + jl .skip_top_right + movzx rax, byte [r11 + rax] + cmp rax, 1 + sete al + add dl, al +.skip_top_right: + + ; Check left + mov rax, r10 + sub rax, 1 + cmp rax, 0 + jl .skip_left + movzx rax, byte [r11 + rax] + cmp rax, 1 + sete al + add dl, al +.skip_left: + + ; Check right + mov rax, r10 + add rax, 1 + cmp rax, 100 + jge .skip_right + movzx rax, byte [r11 + rax] + cmp rax, 1 + sete al + add dl, al +.skip_right: + + ; Check bottom left + mov rax, r10 + add rax, 9 + cmp rax, 100 + jge .skip_bottom_left + movzx rax, byte [r11 + rax] + cmp rax, 1 + sete al + add dl, al +.skip_bottom_left: + + ; Check bottom + mov rax, r10 + add rax, 10 + cmp rax, 100 + jge .skip_bottom + movzx rax, byte [r11 + rax] + cmp rax, 1 + sete al + add dl, al +.skip_bottom: + + ; Check bottom right + mov rax, r10 + add rax, 11 + cmp rax, 100 + jge .skip_bottom_right + movzx rax, byte [r11 + rax] + cmp rax, 1 + sete al + add dl, al +.skip_bottom_right: + ret