From 9312bf9a64b9f9ae6a5537aad238337a4fd4536b Mon Sep 17 00:00:00 2001 From: zombie maniac Date: Sun, 24 Nov 2024 23:51:08 -0500 Subject: [PATCH] made it more of a library --- .gitignore | 1 + Makefile | 42 +++++++++++++++++++++++++++++++++++ hello.s | 25 ++++++++++----------- funnyhello.s => libzomutils.s | 27 +++------------------- 4 files changed, 58 insertions(+), 37 deletions(-) create mode 100644 .gitignore create mode 100644 Makefile rename funnyhello.s => libzomutils.s (68%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f8e4669 --- /dev/null +++ b/Makefile @@ -0,0 +1,42 @@ +# Compiler and flags +CC = clang +CFLAGS = --target=riscv32 -march=rv64gc_zbb -mabi=lp64 -mno-relax -g + +# Directories and files +BUILD_DIR = build +ZOMUTILS_SRC = libzomutils.s +ZOMUTILS_OBJ = $(BUILD_DIR)/libzomutils.o +ZOMUTILS_LIB = $(BUILD_DIR)/libzomutils.a +HELLO_SRC = hello.s +HELLO_OBJ = $(BUILD_DIR)/hello.o +HELLO_BIN = $(BUILD_DIR)/hello + +# Default target +all: $(HELLO_BIN) + +# Ensure build directory exists +$(BUILD_DIR): + mkdir -p $(BUILD_DIR) + +# Build libzomutils object file +$(ZOMUTILS_OBJ): $(ZOMUTILS_SRC) | $(BUILD_DIR) + $(CC) $(CFLAGS) -c $< -o $@ + +# Create static library libzomutils.a +$(ZOMUTILS_LIB): $(ZOMUTILS_OBJ) + ar rcs $@ $^ + +# Build hello object file +$(HELLO_OBJ): $(HELLO_SRC) | $(BUILD_DIR) + $(CC) $(CFLAGS) -c $< -o $@ + +# Link hello executable with libzomutils.a +$(HELLO_BIN): $(HELLO_OBJ) $(ZOMUTILS_LIB) + $(CC) $(CFLAGS) -nostdlib $^ -o $@ + +# Clean up generated files +clean: + rm -rf $(BUILD_DIR) + +.PHONY: all clean + diff --git a/hello.s b/hello.s index 0c7b1de..7dae0b9 100644 --- a/hello.s +++ b/hello.s @@ -1,17 +1,16 @@ -.globl _start + .section .data +message: + .asciz "Hello from libzomutils!\n" - _start: - li a0, 1 # file descriptor = 1 (stdout) - la a1, string # buffer - li a2, 19 # size - li a7, 64 # syscall write (64) - ecall + .section .text + .global _start - _end: +_start: + # Load the address of the message into a0 (first argument register) + la a0, message - li a0, 0 # exit code - li a7, 93 # syscall exit - ecall + # Call the puts function from libzomutils + call puts - - string: .asciz "Hello! It works!!!\n" + # Exit the program + call exit diff --git a/funnyhello.s b/libzomutils.s similarity index 68% rename from funnyhello.s rename to libzomutils.s index f6751d9..3353097 100644 --- a/funnyhello.s +++ b/libzomutils.s @@ -1,23 +1,8 @@ .text -.globl _start -_start: - ld a0, 8(sp) - jal ra, puts - la a0, helloworld - jal ra, puts - la a0, incomingflag - jal ra, puts - - - ld t2, flagmyballs - ror t2, t2, 01 - la t1, flagmyballs - sd t2, 0(t1) - - la a0, flagmyballs - jal ra, puts - j exit +.globl len +.globl puts +.globl exit len: mv t0, a0 @@ -70,9 +55,3 @@ exit: li a7, 93 # syscall exit ecall -.data -helloworld: .asciz "Hello! It works!!!\n" -incomingflag: .asciz "The flag you gotta fix should be right after the newline\n" -flagmyballs: -# .dword 0x7370617263363400 - .dword 0x0343663726170730