From b65b33da2553751a7c31873a198666e12bd7806f Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Mon, 9 Dec 2024 12:25:56 +0100 Subject: [PATCH] Square --- Makefile | 3 ++- link.ld | 2 +- main.s | 2 +- square.h | 1 + square.s | 15 +++++++++++++++ 5 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 square.h create mode 100644 square.s diff --git a/Makefile b/Makefile index e1c6069..c8e8f6f 100644 --- a/Makefile +++ b/Makefile @@ -24,6 +24,7 @@ CFLAGS += -fno-common# # Do not use common sections CFLAGS += -march=rv64i# # Use RV64I ISA, i.e., integer only CFLAGS += -mabi=lp64# # Use LP64 ABI, i.e., 64-bit longs and pointers, 32-bit ints CFLAGS += -Os# # Optimize for size +CFLAGS += -Wall -Wunused -O2 LDFLAGS += -T link.ld # Use the linker script LDFLAGS += --no-dynamic-linker @@ -61,7 +62,7 @@ all: $(OBJS) @echo "CC $<" # Run the binary in QEMU -run: all +run: all @echo "To exit: Ctrl+A, X" @$(QEMU) $(QEMU_FLAGS) -bios $(TARGET) diff --git a/link.ld b/link.ld index fdcb3ce..a89e3c7 100644 --- a/link.ld +++ b/link.ld @@ -5,7 +5,7 @@ MEMORY { SECTIONS { .text : { - main.o(.text.*) + *(.text) *(.text) . = ALIGN(4); } > DRAM diff --git a/main.s b/main.s index 5881cc2..733bb18 100644 --- a/main.s +++ b/main.s @@ -25,7 +25,7 @@ test_routine: ret # Prints "hello" manually -hello_manual: +hello_manual: addi a0, x0, 0x68 li a1, 0x10000000 sb a0, (a1) # 'h' diff --git a/square.h b/square.h new file mode 100644 index 0000000..cfe4f8f --- /dev/null +++ b/square.h @@ -0,0 +1 @@ +int square(int, int); diff --git a/square.s b/square.s new file mode 100644 index 0000000..fe48f24 --- /dev/null +++ b/square.s @@ -0,0 +1,15 @@ +square: + addi sp,sp,-32 + sd ra,24(sp) + sd s0,16(sp) + addi s0,sp,32 + mv a5,a0 + sw a5,-20(s0) + lw a5,-20(s0) + mulw a5,a5,a5 + sext.w a5,a5 + mv a0,a5 + ld ra,24(sp) + ld s0,16(sp) + addi sp,sp,32 + jr ra