Read Input (up,right,down,left). Error: Segmentaion fault to fix...

This commit is contained in:
Afonso Clerigo Mendes de Sousa 2025-03-20 20:36:06 +00:00
parent e56422db89
commit ec320340d9
2 changed files with 67 additions and 5 deletions

BIN
bin/main

Binary file not shown.

View File

@ -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