From 76093db3c93f7e40e4968e1a28c1cea855f9f5de Mon Sep 17 00:00:00 2001 From: PoliEcho Date: Tue, 8 Jul 2025 22:50:46 +0200 Subject: [PATCH] YES IT DOES NOT SEGFAULT --- Makefile | 4 ++-- src/alloc.asm | 2 +- src/drawing.asm | 35 +++++++++++++++++++++++++++-------- src/main.asm | 11 +++++++++++ src/str.asm | 21 ++++++++++++--------- 5 files changed, 53 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index 53c031f..f9571ad 100644 --- a/Makefile +++ b/Makefile @@ -8,8 +8,8 @@ DEBUG_LD_FLAGS := -g DEBUG_NASM_FLAGS := -g -F dwarf # check for avx2 support -ifeq ($(shell grep -o 'avx2[^ ]*' /proc/cpuinfo | head -n 1),avx2) - NASM_FLAGS += -DAVX2 +ifeq ($(shell grep -o 'avx512[^ ]*' /proc/cpuinfo | head -n 1),avx512) + NASM_FLAGS += -DAVX512 endif diff --git a/src/alloc.asm b/src/alloc.asm index ca081a8..7f17276 100644 --- a/src/alloc.asm +++ b/src/alloc.asm @@ -16,8 +16,8 @@ global alloc alloc:; Takes lenght of data in rdi and returns pointer in rax mov rax, SYS_BRK mov qword rcx, [brk_pointer] - add rdi, rcx; calculate new BRK address push rcx + add rdi, rcx; calculate new BRK address syscall mov [brk_pointer], rax pop rax diff --git a/src/drawing.asm b/src/drawing.asm index 5d98496..5b4e19b 100644 --- a/src/drawing.asm +++ b/src/drawing.asm @@ -12,7 +12,10 @@ section .rodata clear: db ESC_CHAR, "[2J", 0 reset: db ESC_CHAR, "[0m", 0 - statusbar: db ESC_CHAR, "[100m", "Use arrow keys to move cursor, enter to invert cell, p to simulation", 0 + home_cursor: db ESC_CHAR, "[H", 0 + + statusbar: db ESC_CHAR, "[100m", "Use arrow keys to move cursor, enter to invert cell h/j to change simulation speed, p to simulation", 0 + %define START_STOP_position $-statusbar-16 start_str: db "START", 0 stop_str: db "STOP", 0 @@ -20,39 +23,55 @@ section .rodata section .text extern print_str extern string_copy +extern memory_set +global init_gameboard init_gameboard: + xor rax, rax + xor rcx, rcx + mov ax, [term_cols] mov cx, [term_rows] mul rcx + + push rax + push rcx + mov rdi, [gameboard_ptr] + mov rsi, 0x20; set rsi to SPACE character + mov rdx, rax + call memory_set + pop rcx + pop rax mov rdx, rax sub rdx, rcx; get pointer to start of last line - lea rdi, [gameboard_ptr] + mov rdi, [gameboard_ptr] add rdi, rax; get end of gameboard - sub rdi, 5; get space for reset sequence + sub rdi, 4; get space for reset sequence lea rsi, [reset] push rdx call string_copy + pop rdx mov rdi, rdx + add rdi, [gameboard_ptr] lea rsi, [statusbar] call string_copy ret - +global print_game_ui print_game_ui: - lea rdi, [clear] + lea rdi, [home_cursor] + call print_str + + mov qword rdi, [gameboard_ptr] call print_str - - - ret diff --git a/src/main.asm b/src/main.asm index 82955a5..e085029 100644 --- a/src/main.asm +++ b/src/main.asm @@ -21,6 +21,9 @@ extern unsigned_int_to_ascii extern init_alloc extern alloc +extern init_gameboard +extern print_game_ui + global _start _start: ; get terminal dimensions @@ -45,12 +48,20 @@ _start: call init_alloc + xor rax, rax + xor rcx, rcx + mov ax, [term_rows] mov cx, [term_cols] mul rcx mov rdi, rax + inc rdi; addition byte for NULL BYTE call alloc mov [gameboard_ptr], rax; stores pointer to gameboard array + call init_gameboard + + call print_game_ui + diff --git a/src/str.asm b/src/str.asm index eefb936..5d7727b 100644 --- a/src/str.asm +++ b/src/str.asm @@ -64,7 +64,7 @@ global string_copy string_copy:; takes pointer to destination in rdi and pointer to source in rsi xor rax, rax - + xor rcx, rcx .copy_next_byte: mov byte cl, [rsi+rax] test cl, cl @@ -84,17 +84,20 @@ memory_set:; takes destination in rdi, byte in sil and lenght in rdx mov rax, rdi and rax, 0xF; offset is stored in rax + + mov r11, 0x0101010101010101; to extend across whoule register + movzx rsi, sil + imul r11, rsi; to extend across whoule register + test al, al; check if resault is 0 jz .addr_is_16_Byte_alligned + mov rax, r11 + mov r8b, 16 sub r8b, al; now offset to first higher 16 byte alligned address is stored in r8 sub rdx, r8; we will write these bytes now - movzx rax, sil - imul rax, 0x01010101; to extend across whoule register - shl rax, 32; to extend across whoule register - - ;add rdi, rdx + ;add rdi, rdx ; we know that rdi has initial address and rdx offset so well fill just add to it mov rcx, 1; we will allwais copy only once @@ -123,10 +126,10 @@ memory_set:; takes destination in rdi, byte in sil and lenght in rdx .addr_is_16_Byte_alligned: shr rdx, 4; set it to how many 128bit(16Byte) chunk we need - %ifdef AVX2 - vpbroadcastq xmm8, rax + %ifdef AVX512 + vpbroadcastq xmm8, r11 %else - movq xmm8, rax + movq xmm8, r11 shufpd xmm8, xmm8, 0x00 %endif