added basic allocator

This commit is contained in:
PoliEcho 2025-07-07 20:39:13 +02:00
parent c1a55fa3ff
commit 51ffd98872
6 changed files with 93 additions and 15 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
.asm-lsp.toml
build
src/*.o

View File

@ -1,28 +1,38 @@
NASM = nasm
LD = ld
NASM_FLAGS := -felf64
NASM_FLAGS := -felf64 -Isrc
LD_FLAGS := --strip-all
DEBUG_LD_FLAGS := -g
DEBUG_NASM_FLAGS := -g -F dwarf
SRC_PATH := src
OBJ_PATH := build/obj
BIN_PATH := build/bin
BIN_NAME := asm-game-of-life
SRC_FILES := $(wildcard $(SRC_PATH)/*.asm)
OBJ_FILES := $(patsubst $(SRC_PATH)/%.asm,$(OBJ_PATH)/%.o,$(SRC_FILES))
all: make-build-dir $(BIN_PATH)/asm-game-of-life
all: $(BIN_PATH)/$(BIN_NAME) | make-build-dir
debug: NASM_FLAGS += $(DEBUG_NASM_FLAGS)
debug: LD_FLAGS = $(DEBUG_LD_FLAGS)
debug: $(BIN_PATH)/$(BIN_NAME) | make-build-dir
make-build-dir:
mkdir -p $(OBJ_PATH)
mkdir -p $(BIN_PATH)
$(BIN_PATH)/asm-game-of-life: $(OBJ_FILES)
$(BIN_PATH)/$(BIN_NAME): $(OBJ_FILES) | make-build-dir
$(LD) $(LD_FLAGS) $^ -o $@
$(OBJ_PATH)/%.o: $(SRC_PATH)/%.asm
$(OBJ_PATH)/%.o: $(SRC_PATH)/%.asm | make-build-dir
$(NASM) $(NASM_FLAGS) $< -o $@
clean:

23
src/alloc.asm Normal file
View File

@ -0,0 +1,23 @@
%include "symbols.asm"
section .bss
brk_pointer: RESQ 1
section .text
global init_alloc
init_alloc:; initialize allocator, optionaly return brk pointer in rax
mov rax, SYS_BRK
mov rdi, 0
syscall
mov [brk_pointer], rax
ret
global alloc
alloc:; Takes lenght of data in rdi and returns pointer in rax
mov rax, SYS_BRK
mov rcx, [brk_pointer]
push rcx
syscall; size already in rdi
mov [brk_pointer], rax
pop rax
ret

View File

@ -1,19 +1,56 @@
SYS_EXIT equ 60
SYS_IOCTL equ 16
STDOUT equ 1
TIOCGWINSZ equ 0x5413
%include "symbols.asm"
section .bss
multipurpuse_buf: RESB 8
str_buf: resb 4
term_rows: RESW 1
term_cols: RESW 1
section .data
section .text
global _start
extern print_str
extern unsigned_int_to_ascii
extern init_alloc
extern alloc
global _start
_start:
; get terminal dimensions
mov rax, SYS_IOCTL
mov rdi, STDOUT
mov rsi, TIOCGWINSZ
lea rdx, [multipurpuse_buf]
syscall
mov word ax, [multipurpuse_buf]; rows are stored at offset 0
mov [term_rows], ax
mov word ax, [multipurpuse_buf+2]; cols are stored at offset 2
mov [term_cols], ax
; handle args
pop rcx; get argc (number of arguments)
cmp rcx, 1
jle .no_arguments_provided
; TODO hanndle arguments
.no_arguments_provided:
call init_alloc
mov ax, [term_rows]
dec ax; one less than terminal size for statusbar
mov cx, [term_cols]
mul rcx
mov rdi, rax
call alloc
mov r15, rax; stores pointer to gameboard array
mov rax, SYS_EXIT
mov rdi, 0 ; return code
syscall

View File

@ -1,6 +1,4 @@
SYS_WRITE equ 1
STDOUT equ 1
ASCII_ZERO equ 48
%include "symbols.asm"
section .text

9
src/symbols.asm Normal file
View File

@ -0,0 +1,9 @@
SYS_EXIT equ 60
SYS_IOCTL equ 16
SYS_WRITE equ 1
SYS_BRK equ 12
STDOUT equ 1
TIOCGWINSZ equ 0x5413
ASCII_ZERO equ 48