initial asm file

This commit is contained in:
janis 2025-10-16 18:11:05 +02:00
parent 149ee03810
commit 6214d15888
Signed by: janis
SSH key fingerprint: SHA256:bB1qbbqmDXZNT0KKD5c2Dfjg53JGhj7B3CFcLIzSqq8
4 changed files with 84 additions and 0 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
/.direnv/ /.direnv/
lang/target

View file

@ -23,8 +23,15 @@
devShells.default = pkgs.mkShell { devShells.default = pkgs.mkShell {
buildInputs = [ buildInputs = [
pkg-config pkg-config
man-pages
mold mold
clang clang
clang-tools
just
just-formatter
just-lsp
gdb
gdbgui
nasm nasm
nasmfmt nasmfmt
git git

25
lang/Makefile Normal file
View file

@ -0,0 +1,25 @@
# Makefile: Compile and link main.asm using nasm and mold, intermediate files in target/
TARGET_DIR := target
SRC := src/main.asm
OBJ := $(TARGET_DIR)/main.o
BIN := $(TARGET_DIR)/main
.PHONY: all clean
all: $(BIN)
$(TARGET_DIR):
mkdir -p $(TARGET_DIR)
$(OBJ): $(SRC) | $(TARGET_DIR)
nasm -f elf64 -g $(SRC) -o $(OBJ)
$(BIN): $(OBJ)
mold -run ld -o $(BIN) $(OBJ)
run: $(BIN)
$(BIN)
clean:
rm -rf $(TARGET_DIR)

51
lang/src/main.asm Normal file
View file

@ -0,0 +1,51 @@
;; Compile with:
;; nasm -f elf64 main.asm -o main.o
;; RO section
section .data
hello_msg db "Hello, World!", 10
hello_msg_len equ $ - hello_msg
die_msg db "panic occured!", 10
die_msg_len equ $ - die_msg
section .text
global _start
_start:
; get filename from argv[1]
; argv is at rsp + 8
; check if argc > 1
mov rdx, hello_msg
mov rcx, hello_msg_len
call eprint_str
mov rax, [rsp] ; argc
cmp rax, 1
jle .no_filename ; if argc <= 1, no filename provided
mov rax, 42
ret
.no_filename:
call die
die:
mov rdx, die_msg
mov rcx, die_msg_len
call eprint_str
; exit with error code 1
mov rax, 60 ; syscall: exit
mov rdi, 1 ; status: 1
syscall
ret ; should never reach here
eprint_str: ; print string-length pair to stderr
; str pointer in rdx
; length in rcx
mov rax, 1 ; syscall: write
mov rdi, 2 ; fd: stderr
mov rsi, rdx ; buf: str
mov rdx, rcx ; len: length
syscall
ret