From 6214d158881c231887360a9ad80443564e33de62 Mon Sep 17 00:00:00 2001 From: janis Date: Thu, 16 Oct 2025 18:11:05 +0200 Subject: [PATCH] initial asm file --- .gitignore | 1 + flake.nix | 7 +++++++ lang/Makefile | 25 +++++++++++++++++++++++ lang/src/main.asm | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+) create mode 100644 lang/Makefile create mode 100644 lang/src/main.asm diff --git a/.gitignore b/.gitignore index 29963da..5388342 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /.direnv/ +lang/target diff --git a/flake.nix b/flake.nix index b3e6466..91f1dad 100644 --- a/flake.nix +++ b/flake.nix @@ -23,8 +23,15 @@ devShells.default = pkgs.mkShell { buildInputs = [ pkg-config + man-pages mold clang + clang-tools + just + just-formatter + just-lsp + gdb + gdbgui nasm nasmfmt git diff --git a/lang/Makefile b/lang/Makefile new file mode 100644 index 0000000..955f18f --- /dev/null +++ b/lang/Makefile @@ -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) diff --git a/lang/src/main.asm b/lang/src/main.asm new file mode 100644 index 0000000..0066521 --- /dev/null +++ b/lang/src/main.asm @@ -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 + +