initial asm file
This commit is contained in:
parent
149ee03810
commit
6214d15888
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1 +1,2 @@
|
|||
/.direnv/
|
||||
lang/target
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
25
lang/Makefile
Normal file
25
lang/Makefile
Normal 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
51
lang/src/main.asm
Normal 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
|
||||
|
||||
|
||||
Loading…
Reference in a new issue