YES IT DOES NOT SEGFAULT

This commit is contained in:
PoliEcho 2025-07-08 22:50:46 +02:00
parent 9092d1276a
commit 76093db3c9
5 changed files with 53 additions and 20 deletions

View File

@ -8,8 +8,8 @@ DEBUG_LD_FLAGS := -g
DEBUG_NASM_FLAGS := -g -F dwarf DEBUG_NASM_FLAGS := -g -F dwarf
# check for avx2 support # check for avx2 support
ifeq ($(shell grep -o 'avx2[^ ]*' /proc/cpuinfo | head -n 1),avx2) ifeq ($(shell grep -o 'avx512[^ ]*' /proc/cpuinfo | head -n 1),avx512)
NASM_FLAGS += -DAVX2 NASM_FLAGS += -DAVX512
endif endif

View File

@ -16,8 +16,8 @@ global alloc
alloc:; Takes lenght of data in rdi and returns pointer in rax alloc:; Takes lenght of data in rdi and returns pointer in rax
mov rax, SYS_BRK mov rax, SYS_BRK
mov qword rcx, [brk_pointer] mov qword rcx, [brk_pointer]
add rdi, rcx; calculate new BRK address
push rcx push rcx
add rdi, rcx; calculate new BRK address
syscall syscall
mov [brk_pointer], rax mov [brk_pointer], rax
pop rax pop rax

View File

@ -12,7 +12,10 @@ section .rodata
clear: db ESC_CHAR, "[2J", 0 clear: db ESC_CHAR, "[2J", 0
reset: db ESC_CHAR, "[0m", 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 start_str: db "START", 0
stop_str: db "STOP", 0 stop_str: db "STOP", 0
@ -20,39 +23,55 @@ section .rodata
section .text section .text
extern print_str extern print_str
extern string_copy extern string_copy
extern memory_set
global init_gameboard
init_gameboard: init_gameboard:
xor rax, rax
xor rcx, rcx
mov ax, [term_cols] mov ax, [term_cols]
mov cx, [term_rows] mov cx, [term_rows]
mul rcx 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 mov rdx, rax
sub rdx, rcx; get pointer to start of last line 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 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] lea rsi, [reset]
push rdx push rdx
call string_copy call string_copy
pop rdx pop rdx
mov rdi, rdx mov rdi, rdx
add rdi, [gameboard_ptr]
lea rsi, [statusbar] lea rsi, [statusbar]
call string_copy call string_copy
ret ret
global print_game_ui
print_game_ui: print_game_ui:
lea rdi, [clear] lea rdi, [home_cursor]
call print_str
mov qword rdi, [gameboard_ptr]
call print_str call print_str
ret ret

View File

@ -21,6 +21,9 @@ extern unsigned_int_to_ascii
extern init_alloc extern init_alloc
extern alloc extern alloc
extern init_gameboard
extern print_game_ui
global _start global _start
_start: _start:
; get terminal dimensions ; get terminal dimensions
@ -45,12 +48,20 @@ _start:
call init_alloc call init_alloc
xor rax, rax
xor rcx, rcx
mov ax, [term_rows] mov ax, [term_rows]
mov cx, [term_cols] mov cx, [term_cols]
mul rcx mul rcx
mov rdi, rax mov rdi, rax
inc rdi; addition byte for NULL BYTE
call alloc call alloc
mov [gameboard_ptr], rax; stores pointer to gameboard array mov [gameboard_ptr], rax; stores pointer to gameboard array
call init_gameboard
call print_game_ui

View File

@ -64,7 +64,7 @@ global string_copy
string_copy:; takes pointer to destination in rdi and pointer to source in rsi string_copy:; takes pointer to destination in rdi and pointer to source in rsi
xor rax, rax xor rax, rax
xor rcx, rcx
.copy_next_byte: .copy_next_byte:
mov byte cl, [rsi+rax] mov byte cl, [rsi+rax]
test cl, cl test cl, cl
@ -84,17 +84,20 @@ memory_set:; takes destination in rdi, byte in sil and lenght in rdx
mov rax, rdi mov rax, rdi
and rax, 0xF; offset is stored in rax 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 test al, al; check if resault is 0
jz .addr_is_16_Byte_alligned jz .addr_is_16_Byte_alligned
mov rax, r11
mov r8b, 16 mov r8b, 16
sub r8b, al; now offset to first higher 16 byte alligned address is stored in r8 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 sub rdx, r8; we will write these bytes now
movzx rax, sil ;add rdi, rdx
imul rax, 0x01010101; to extend across whoule register
shl rax, 32; to extend across whoule register
;add rdi, rdx
; we know that rdi has initial address and rdx offset so well fill just add to it ; 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 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: .addr_is_16_Byte_alligned:
shr rdx, 4; set it to how many 128bit(16Byte) chunk we need shr rdx, 4; set it to how many 128bit(16Byte) chunk we need
%ifdef AVX2 %ifdef AVX512
vpbroadcastq xmm8, rax vpbroadcastq xmm8, r11
%else %else
movq xmm8, rax movq xmm8, r11
shufpd xmm8, xmm8, 0x00 shufpd xmm8, xmm8, 0x00
%endif %endif