From bd1fe4b2a85f88caac78b653f5b16b0258aa85c7 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Mon, 1 Apr 2024 11:09:45 +0200 Subject: [PATCH] Initial --- hello.s | 16 ++++++++++++++++ main.s | 12 ++++++++++++ makefile | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 hello.s create mode 100644 main.s create mode 100644 makefile diff --git a/hello.s b/hello.s new file mode 100644 index 0000000..1a6b2b7 --- /dev/null +++ b/hello.s @@ -0,0 +1,16 @@ +.section .text +.globl _hello + +_hello: + # Write the string "Hello, World!\n" to stdout + la a0, msg # Load the address of the string into a0 + li a7, 4 # syscall number for write + li a1, 13 # Length of the string + li a2, 1 # File descriptor: stdout + ecall # Make the system call to write + + ret # Return from the function + +.section .data +msg: + .ascii "Hello, World!\n" diff --git a/main.s b/main.s new file mode 100644 index 0000000..faa2533 --- /dev/null +++ b/main.s @@ -0,0 +1,12 @@ +.section .text +.globl _start + +_start: + # Call the hello function + lui a0, %hi(_hello) # Load upper immediate of _hello address + addi a0, a0, %lo(_hello) # Add lower immediate of _hello address + jalr ra, a0, 0 # Jump and link to _hello, ra is return address + + # Exit the program + li a7, 10 # syscall number for exit + ecall # Make the system call to exit diff --git a/makefile b/makefile new file mode 100644 index 0000000..c5ab1b6 --- /dev/null +++ b/makefile @@ -0,0 +1,35 @@ +# Compiler and assembler +CC = riscv64-linux-gnu-gcc +AS = riscv64-linux-gnu-as +LD = riscv64-linux-gnu-ld + +# Flags for compiler and assembler +CFLAGS = -static -nostartfiles -nostdlib +ASFLAGS = + +# QEMU command and flags +QEMU = qemu-riscv64-static +QEMU_FLAGS = + +# Assembly source files +AS_SRCS := $(wildcard *.s) + +# Object files +OBJS := $(AS_SRCS:.s=.o) + +# Default target +all: $(OBJS) + $(LD) -o program $(OBJS) +# $(CC) $(CFLAGS) -o program $(OBJS) + +# Compile assembly sources +%.o: %.s + $(AS) $(ASFLAGS) -o $@ $< + +# Run the program in QEMU +run: all + $(QEMU) $(QEMU_FLAGS) ./program + +# Clean up +clean: + rm -f program $(OBJS)