Compare commits

...

8 Commits

Author SHA1 Message Date
c5e404f1dd FINALY fix inicializing gameboard 2025-07-09 21:33:36 +02:00
564a6b88c1 fix aligment 2025-07-09 20:34:24 +02:00
d6613ac341 fix memory_set number 2 2025-07-09 20:34:09 +02:00
2b8236ee91 fix memory_set function 2025-07-09 19:23:13 +02:00
c0bd3fcbc3 fix pointer aritmetic 2025-07-09 13:26:06 +02:00
76093db3c9 YES IT DOES NOT SEGFAULT 2025-07-08 22:50:46 +02:00
9092d1276a added memory_set'function 2025-07-08 15:04:23 +02:00
35a6894d9f fix allocator 2025-07-08 10:00:24 +02:00
6 changed files with 156 additions and 32 deletions

View File

@ -7,6 +7,11 @@ LD_FLAGS := --strip-all
DEBUG_LD_FLAGS := -g DEBUG_LD_FLAGS := -g
DEBUG_NASM_FLAGS := -g -F dwarf DEBUG_NASM_FLAGS := -g -F dwarf
# check for avx2 support
ifeq ($(shell grep -o 'avx512[^ ]*' /proc/cpuinfo | head -n 1),avx512)
NASM_FLAGS += -DAVX512
endif
SRC_PATH := src SRC_PATH := src
OBJ_PATH := build/obj OBJ_PATH := build/obj

View File

@ -7,7 +7,7 @@ section .text
global init_alloc global init_alloc
init_alloc:; initialize allocator, optionaly return brk pointer in rax init_alloc:; initialize allocator, optionaly return brk pointer in rax
mov rax, SYS_BRK mov rax, SYS_BRK
mov rdi, 0 xor rdi, rdi
syscall syscall
mov [brk_pointer], rax mov [brk_pointer], rax
ret ret
@ -15,9 +15,10 @@ init_alloc:; initialize allocator, optionaly return brk pointer in rax
global alloc 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 rcx, [brk_pointer] mov qword rcx, [brk_pointer]
push rcx push rcx
syscall; size already in rdi add rdi, rcx; calculate new BRK address
syscall
mov [brk_pointer], rax mov [brk_pointer], rax
pop rax pop rax
ret ret

View File

@ -1,54 +1,76 @@
%include "symbols.asm" %include "symbols.asm"
section .bss section .bss
extern gameboard_ptr extern gameboard_ptr
extern term_rows extern term_rows
extern term_cols extern term_cols
section .data extern gameboard_size
simulation_running: RESB 1
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
resetLen: equ $-reset-1
global resetLen
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, "[30;100m", "Use arrow keys to move cursor, enter to invert cell h/j to change simulation speed, p to simulation", 0
START_STOP_pos: equ $-statusbar-16
start_str: db "START", 0
stop_str: db "STOP ", 0
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:
mov ax, [term_cols] xor rax, rax
mov cx, [term_rows] xor rcx, rcx
mul rcx
mov rdx, rax
sub rdx, rcx; get pointer to start of last line
lea rdi, [gameboard_ptr]
add rdi, rax; get end of gameboard
sub rdi, 5; get space for reset sequence
lea rsi, [reset]
mov rdi, [gameboard_ptr]
push rdi
mov rsi, 0x20; set rsi to SPACE character
mov rdx, [gameboard_size]
push rdx push rdx
call string_copy add rdx, ESC_chars_compensation_Len; I dont know how this work but it works so i wont touch it
call memory_set
pop rdx pop rdx
mov rdi, rdx pop rdi
add rdi, rdx; get pointer to last char on screen
push rdi
add rdi, ESC_chars_compensation_Len
lea rsi, [reset]
call string_copy
pop rdi
xor rax, rax
mov ax, [term_cols]
sub rdi, rax
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

@ -1,17 +1,27 @@
%include "symbols.asm" %include "symbols.asm"
section .bss section .bss
multipurpuse_buf: RESB 8 multipurpuse_buf: RESB 8
global term_rows
term_rows: RESW 1 term_rows: RESW 1
global term_cols
term_cols: RESW 1 term_cols: RESW 1
global gameboard_ptr
gameboard_ptr: RESQ 1 gameboard_ptr: RESQ 1
global gameboard_size
gameboard_size: RESQ 1
extern cursor_rows extern cursor_rows
extern cursor_cols extern cursor_cols
section .data
section .rodata
extern resetLen
section .text section .text
extern print_str extern print_str
@ -19,6 +29,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
@ -43,12 +56,24 @@ _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
mov qword [gameboard_size], rax
inc rdi; addition byte for NULL BYTE
lea rax, [resetLen]
add rdi, rax
add rdi, ESC_chars_compensation_Len
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

@ -60,15 +60,84 @@ unsigned_int_to_ascii: ; takes pointer to array in rdi and value stored in rsi D
ret ret
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 al, [rdi+rax] mov byte cl, [rsi+rax]
mov [rsi+rax], al test cl, cl
jz .exit
mov [rdi+rax], cl
inc rax inc rax
test rax,rax jmp .copy_next_byte
jnz .copy_next_byte
.exit:
ret ret
global memory_set:
memory_set:; takes destination in rdi, byte in sil and lenght in rdx
; first check if value is 16 byte alligned
mov r9, rdi; move destination to r9
mov r11, 0x0101010101010101; to extend across whoule register
movzx rsi, sil
imul r11, rsi; to extend across whoule register
cmp rdx, 16
jnl .write_16_or_more_bytes
mov rcx, rdx
jmp .write_less_than_16_bytes
.write_16_or_more_bytes:
mov rax, rdi; move destination to rax
and rax, 0xF; offset is stored in rax
test al, al; check if resault is 0
jz .addr_is_16_Byte_alligned
mov cl, 16
sub cl, al; now offset to first higher 16 byte alligned address is stored in r8
movzx rcx, cl; remove ani posible garbage
.write_less_than_16_bytes:
mov rax, r11
sub rdx, rcx; we will write these bytes now
rep stosb
.addr_is_16_Byte_alligned:
mov r10, rdx
shr r10, 4; set it to how many 128bit(16Byte) chunk we need
test r10, r10; check if we need to write aditional 16 bytes at all
jz .function_exit
%ifdef AVX512
vpbroadcastq xmm8, r11
%else
movq xmm8, r11
shufpd xmm8, xmm8, 0x00
%endif
.move_16_bytes:
movdqa [rdi], xmm8
add rdi, 16
sub rdx, 16
cmp rdx, 16; test if rdx is less than 16
jge .move_16_bytes
.function_exit:
test rdx, rdx; test if rdx is 0
jz .true_function_exit
mov cl, dl
jmp .write_less_than_16_bytes
.true_function_exit:
mov rax, r9; return pointer to memory area same as memset in libc
ret

View File

@ -8,3 +8,5 @@ TIOCGWINSZ equ 0x5413
ASCII_ZERO equ 48 ASCII_ZERO equ 48
ESC_CHAR equ 27 ESC_CHAR equ 27
ESC_chars_compensation_Len equ 9; i have to compensate for escape sequences that dont get printed why 11 exactly, I dont know