diff options
| author | Christian Krinitsin <code@krinitsin.xyz> | 2024-11-20 21:55:06 +0100 |
|---|---|---|
| committer | Christian Krinitsin <code@krinitsin.xyz> | 2024-11-20 21:55:06 +0100 |
| commit | 816561455ca037530d67e2606ffd7f887ef742de (patch) | |
| tree | 0ce80c6602e5ee443b3a4e7d0c4242ed464f0c8c | |
| parent | dc19aa8fcd07ddc798d9ad31be18b14a34b6aa30 (diff) | |
| download | x86_64-Snake-816561455ca037530d67e2606ffd7f887ef742de.tar.gz x86_64-Snake-816561455ca037530d67e2606ffd7f887ef742de.zip | |
add input
| -rw-r--r-- | Makefile | 3 | ||||
| -rw-r--r-- | input.asm | 47 | ||||
| -rw-r--r-- | main.asm | 54 |
3 files changed, 81 insertions, 23 deletions
diff --git a/Makefile b/Makefile index 2d93a73..981bed2 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,8 @@ main: main.asm border.asm utils.asm nasm -f elf64 -g main.asm -o build/main.o nasm -f elf64 -g border.asm -o build/border.o nasm -f elf64 -g utils.asm -o build/utils.o - ld build/main.o build/border.o build/utils.o -o main + nasm -f elf64 -g input.asm -o build/input.o + ld build/main.o build/border.o build/utils.o build/input.o -o main clean: rm -r main build/ diff --git a/input.asm b/input.asm new file mode 100644 index 0000000..b80b509 --- /dev/null +++ b/input.asm @@ -0,0 +1,47 @@ +section .bss + input_buffer resb 1 + +section .text + global input + extern direction + extern exit + +input: + call read_input + call handle_input + ret + +handle_input: + mov al, byte [input_buffer] + cmp al, 'w' + je _input_up + cmp al, 's' + je _input_down + cmp al, 'a' + je _input_left + cmp al, 'd' + je _input_right + cmp al, 27 ; escape + je exit + ret + +_input_up: + mov byte [direction], 3 + ret +_input_down: + mov byte [direction], 1 + ret +_input_left: + mov byte [direction], 2 + ret +_input_right: + mov byte [direction], 0 + ret + +read_input: + mov rax, 0 + mov rdi, 0 + mov rsi, input_buffer + mov rdx, 1 + syscall + ret diff --git a/main.asm b/main.asm index 1ba1eb5..d0e1e3a 100644 --- a/main.asm +++ b/main.asm @@ -15,14 +15,12 @@ section .data ; 3 ; 2 x 0 ; 1 + global direction direction db 0 timespec dq 0 dq 500000000 -section .bss - buffer resb 1 - section .rodata ; width and height of the playable area ; (without the borders) @@ -32,6 +30,7 @@ section .rodata height db 30 section .text + extern input extern draw_border extern write_byte extern clear_screen @@ -39,6 +38,7 @@ section .text extern move_cursor_right extern move_cursor_down global _start + global exit draw_snake: push rbx @@ -62,6 +62,30 @@ _11:call move_cursor_down call reset_cursor ret +move_snake: + cmp byte [direction], 0 + je _move_right + cmp byte [direction], 1 + je _move_down + cmp byte [direction], 2 + je _move_left + cmp byte [direction], 3 + je _move_up + ret + +_move_right: + inc byte [snake] + ret +_move_down: + inc byte [snake+1] + ret +_move_left: + dec byte [snake] + ret +_move_up: + dec byte [snake+1] + ret + _start: mov byte [snake], 5 mov byte [snake+1], 5 @@ -71,30 +95,16 @@ main_loop: call draw_border call draw_snake - lea rdi, [timespec] ; Pointer to timespec struct (rdi) - xor rsi, rsi ; NULL (no remaining time) - mov rax, 35 ; Syscall number for nanosleep + mov rax, 35 + lea rdi, [timespec] + xor rsi, rsi syscall - inc byte [snake] + call input + call move_snake jmp main_loop -; mov rax, 0 ; Syscall for read -; mov rdi, 0 ; stdin -; mov rsi, buffer ; Address of the buffer -; mov rdx, 1 ; Read 1 byte -; syscall - - ; If a key was pressed, store it in `input` -; test rax, rax ; Check if any input was received -; jz exit ; If no input, skip - - -; mov al, byte [buffer]; Load the input character - ;mov rax, 'x' -; call write_byte - exit: ; exit syscall with return code 0 mov rax, 60 xor rdi, rdi |