This commit is contained in:
Imbus 2024-04-01 11:09:45 +02:00
commit bd1fe4b2a8
3 changed files with 63 additions and 0 deletions

16
hello.s Normal file
View file

@ -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"

12
main.s Normal file
View file

@ -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

35
makefile Normal file
View file

@ -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)