From ec320340d954ffb5265524ba2101eac041cd8aef Mon Sep 17 00:00:00 2001 From: AfonsoCMSousa Date: Thu, 20 Mar 2025 20:36:06 +0000 Subject: [PATCH] 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