Compare commits

...

2 Commits

Author SHA1 Message Date
51ffd98872 added basic allocator 2025-07-07 20:39:13 +02:00
c1a55fa3ff created some basic string helper functions 2025-07-07 13:42:53 +02:00
6 changed files with 143 additions and 8 deletions

2
.gitignore vendored
View File

@ -1 +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,3 +1,56 @@
section .text
global _start
%include "symbols.asm"
section .bss
multipurpuse_buf: RESB 8
term_rows: RESW 1
term_cols: RESW 1
section .data
section .text
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,7 +1,8 @@
SYS_WRITE equ 1
STDOUT equ 1
%include "symbols.asm"
section .text
global print_str
print_str: ; takes pointer to string in rdi and retuns in rax
push rsi
push rdx
@ -22,3 +23,40 @@ print_str: ; takes pointer to string in rdi and retuns in rax
pop rsi
ret
global unsigned_int_to_ascii
unsigned_int_to_ascii: ; takes pointer to array in rdi and value stored in rsi DOES NOT BOUNDS CHECK
mov r11, 0
mov rcx, 10
mov rax, rsi
.count_loop:
inc r11
cmp rax, 10
jl .loop_count_exit
xor rdx, rdx
div rcx
push rdx
jmp .count_loop
.loop_count_exit:
push rax
mov rcx, 0
.store_loop: ; basicly for loop
cmp rcx, r11
jnl .loop_store_exit
pop rax
add rax, ASCII_ZERO
mov byte [rdi + rcx], al
inc rcx
jmp .store_loop
.loop_store_exit:
mov rax, r11
ret

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