diff options
| author | Christian Krinitsin <code@krinitsin.xyz> | 2024-11-20 20:39:32 +0100 |
|---|---|---|
| committer | Christian Krinitsin <code@krinitsin.xyz> | 2024-11-20 20:39:32 +0100 |
| commit | 1dc5b526c19f179f6e78db6bfe8d3cc50881b978 (patch) | |
| tree | b0c25a96aba3583befa3444bed0100c0e8a7d209 /main.asm | |
| parent | 4646adf613615a4a24d1683306f3e13f0c628a6a (diff) | |
| download | x86_64-Snake-1dc5b526c19f179f6e78db6bfe8d3cc50881b978.tar.gz x86_64-Snake-1dc5b526c19f179f6e78db6bfe8d3cc50881b978.zip | |
split into multiple files, .o files in build/
Diffstat (limited to 'main.asm')
| -rw-r--r-- | main.asm | 142 |
1 files changed, 70 insertions, 72 deletions
diff --git a/main.asm b/main.asm index d1f424c..f20699a 100644 --- a/main.asm +++ b/main.asm @@ -1,99 +1,97 @@ section .data - clear db 0x1B, '[2J', 0x1B, '[H', 0 - clear_len equ $ - clear + + ; cursor management + reset_cursor db 0x1B , '[H', 0 + reset_cursor_len equ $ - reset_cursor + up_cursor db 0x1B , '[A', 0 + up_cursor_len equ $ - up_cursor + bottom_cursor db 0x1B , '[B', 0 + bottom_cursor_len equ $ - bottom_cursor + right_cursor db 0x1B , '[C', 0 + right_cursor_len equ $ - right_cursor + left_cursor db 0x1B , '[D', 0 + left_cursor_len equ $ - left_cursor + + ; example messages for later message1 db "Hello, World!", 0xA msg_len1 equ $ - message1 message2 db "Hello 2!", 0xA msg_len2 equ $ - message2 + + ; array of snake - 1000 thousand bytes means max length is 500 + ; head is at snake[0] + snake TIMES 1000 db 0 + + ; direction of the snake: + ; 3 + ; 2 x 0 + ; 1 + direction db 0 + +section .bss + buffer resb 1 section .rodata + ; width and height of the playable area + ; (without the borders) + global width + global height width db 70 height db 30 -section .bss - byte_to_write resb 1 - section .text + extern draw_border + extern write_byte + extern clear_screen global _start -write_byte: - push rdi - mov byte [byte_to_write], al - mov rax, 1 - mov rdi, 1 - mov rsi, byte_to_write - mov rdx, 1 - syscall - pop rdi - ret - -clear_screen: - push rdi - mov rax, 1 - mov rdi, 1 - mov rsi, clear - mov rdx, clear_len - syscall - pop rdi - ret - -draw_upper_lower_line: +draw_snake: push rbx - mov rax, '+' - call write_byte - mov bl, byte [width] -_01:mov rax, '-' - call write_byte - dec bl - cmp bl, 0 - jnz _01 - mov rax, '+' - call write_byte - mov rax, 10 ;newline - call write_byte - pop rbx - ret + mov bh, byte [snake] + mov bl, byte [snake+1] +_10: -draw_left_right_lines: - push rbx - mov rax, '|' - call write_byte - mov bl, byte [width] -_02:mov rax, 32 ;space - call write_byte - dec bl - cmp bl, 0 - jnz _02 - mov rax, '|' - call write_byte - mov rax, 10;newline - call write_byte pop rbx ret -draw_border: - push rbx - - call draw_upper_lower_line - mov bl, byte [height] -_03:call draw_left_right_lines - dec bl - cmp bl, 0 - jnz _03 +_start: + call clear_screen + call draw_border - call draw_upper_lower_line + mov byte [snake], 5 + mov byte [snake+1], 5 - pop rbx - ret + mov rax, 1 + mov rdi, 1 + mov rsi, reset_cursor + mov rdx, reset_cursor_len + syscall + mov rax, 1 + mov rdi, 1 + mov rsi, bottom_cursor + mov rdx, bottom_cursor_len + syscall + mov rax, 1 + mov rdi, 1 + mov rsi, right_cursor + mov rdx, right_cursor_len + syscall + mov rax, 0 ; Syscall for read + mov rdi, 0 ; stdin + mov rsi, buffer ; Address of the buffer + mov rdx, 1 ; Read 1 byte + syscall -_start: - call clear_screen - call draw_border + ; 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 +exit: ; exit syscall with return code 0 mov rax, 60 xor rdi, rdi syscall |