diff options
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | Makefile | 11 | ||||
| -rw-r--r-- | border.asm | 64 | ||||
| -rw-r--r-- | main.asm | 142 | ||||
| -rw-r--r-- | utils.asm | 38 |
5 files changed, 180 insertions, 77 deletions
diff --git a/.gitignore b/.gitignore index 8e68213..d6f61dc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ main -main.o +build/* diff --git a/Makefile b/Makefile index 3398b6a..2d93a73 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,9 @@ -main: main.asm - nasm -f elf64 -g main.asm -o main.o - ld main.o -o main +main: main.asm border.asm utils.asm + mkdir -p build/ + 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 clean: - rm main main.o + rm -r main build/ diff --git a/border.asm b/border.asm new file mode 100644 index 0000000..12774b6 --- /dev/null +++ b/border.asm @@ -0,0 +1,64 @@ +section .text + extern height + extern width + extern write_byte + global draw_border + +draw_upper_lower_line: + 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 + +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 + + call draw_upper_lower_line + + pop rbx + ret 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 diff --git a/utils.asm b/utils.asm new file mode 100644 index 0000000..39a2df3 --- /dev/null +++ b/utils.asm @@ -0,0 +1,38 @@ +section .data + ; escape code for clearing the screen + clear db 0x1B, '[2J', 0x1B, '[H', 0 + clear_len equ $ - clear + +section .bss + ; adress for syscall for write_byte wrapper + byte_to_write resb 1 + +section .text + global write_byte + global clear_screen + +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 + |