From e681b848d50f3da026a98acb5acaf039ee3971d2 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Fri, 5 Apr 2024 17:02:16 +0200 Subject: [PATCH 1/9] Makefile tuning --- makefile | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/makefile b/makefile index 1f81cfc..33620fb 100644 --- a/makefile +++ b/makefile @@ -10,8 +10,10 @@ SIZE = ${CROSS}-size TARGET = bin.elf # QEMU command and flags -QEMU = qemu-riscv64-static -QEMU_FLAGS = +# QEMU = qemu-riscv64-static +QEMU = qemu-system-riscv64 +QEMU_FLAGS += --machine virt# # Use the virt machine +QEMU_FLAGS += --nographic# # No graphical output # Flags for compiler and assembler CFLAGS += -static# # Use static linking @@ -23,6 +25,13 @@ 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 +LDFLAGS += -T link.ld # Use the linker script +LDFLAGS += --no-dynamic-linker +LDFLAGS += -m elf64lriscv +LDFLAGS += -static +LDFLAGS += -nostdlib +LDFLAGS += -s + # Use GC=0 to disable garbage collection ifneq ($(GC), 0) CFLAGS += -ffreestanding @@ -43,7 +52,7 @@ OBJS := $(AS_SRCS:.s=.o) # Default target all: $(OBJS) - @$(CC) $(CFLAGS) -o $(TARGET) $(OBJS) + @$(LD) $(LDFLAGS) -o $(TARGET) $(OBJS) @echo "LD $(OBJS)" # Compile assembly sources @@ -52,8 +61,9 @@ all: $(OBJS) @echo "CC $<" # Run the binary in QEMU -run: all - $(QEMU) $(QEMU_FLAGS) ./$(TARGET) +run: all + @echo "To exit: Ctrl+A, X" + @$(QEMU) $(QEMU_FLAGS) -bios $(TARGET) # View the text section of the binary inspect: all From 0e259f27bdd36fd56ae7cb7b6f3be02451beed27 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Fri, 5 Apr 2024 17:02:26 +0200 Subject: [PATCH 2/9] Trying to make stdout work --- main.s | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 70 insertions(+), 9 deletions(-) diff --git a/main.s b/main.s index faa2533..70bdd83 100644 --- a/main.s +++ b/main.s @@ -1,12 +1,73 @@ -.section .text -.globl _start +# Define variables +.data +.align 4 + num1: .word 5 # First number + num2: .word 7 # Second number + result: .word 0 # Variable to store the result + msg: .ascii "Hello, World!\n" + +# Main program entry point +.text +.global _start +.align 4 +test_routine: + la a0, num1 # Load the address of num1 into register a6 + li a1, 10 # Load the value 10 into register a5 + xori a2, a1, 0x1 # Calculate the value of a2 + sw a1, 0(a0) # Store the value in memory + ret _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 + call h2 + call test_routine + call hello + + # Load the first number into register t0 + lw t0, num1 + + # Load the second number into register t1 + lw t1, num2 + + # Add the two numbers and store the result in register t2 + add t2, t0, t1 + + # Store the result in memory + la t3, result + sw t2, 0(t3) + + # Call _hello + la a0, result + + # End program + ecall + +h2: + addi a0, x0, 0x68 + li a1, 0x10000000 + sb a0, (a1) # 'h' + + addi a0, x0, 0x65 + sb a0, (a1) # 'e' + + addi a0, x0, 0x6C + sb a0, (a1) # 'l' + + addi a0, x0, 0x6C + sb a0, (a1) # 'l' + + addi a0, x0, 0x6F + sb a0, (a1) # 'o' + ret + +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 + +# To avoid the program from exiting +loop: j loop - # Exit the program - li a7, 10 # syscall number for exit - ecall # Make the system call to exit From a63046569dbea14bb5cc45ad28d6b23dfac1b1f4 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Fri, 5 Apr 2024 17:04:19 +0200 Subject: [PATCH 3/9] Basic linker script --- link.ld | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 link.ld diff --git a/link.ld b/link.ld new file mode 100644 index 0000000..845c0a3 --- /dev/null +++ b/link.ld @@ -0,0 +1,16 @@ +MEMORY { + SRAM (rwx) : ORIGIN = 0x20000000, LENGTH = 1024 + DRAM (rwx) : ORIGIN = 0x80000000, LENGTH = 1024 +} + +SECTIONS { + .text : { + *(.text) + } > SRAM + .data : { + *(.data) + } > DRAM AT > SRAM + .bss : { + *(.bss) + } > DRAM +} From 868c032cbc9f9208f3bc7f44da516d4a3e70e5a7 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Fri, 5 Apr 2024 17:04:38 +0200 Subject: [PATCH 4/9] Deleted dead code --- hello.s | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 hello.s diff --git a/hello.s b/hello.s deleted file mode 100644 index 1a6b2b7..0000000 --- a/hello.s +++ /dev/null @@ -1,16 +0,0 @@ -.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" From 279fc9b754364ab3f6e1c2a20d186ae7e2093d43 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Fri, 5 Apr 2024 17:05:34 +0200 Subject: [PATCH 5/9] Readme with useful resources --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..4a6cbd0 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# Risc-V Assembly Language Programming + +## Resources + +- [Risc-V Guru](https://risc-v.guru/) and [direct link to the instruction set](https://risc-v.guru/instructions/) +- [RISC-V ISA Manual](https://riscv.org/wp-content/uploads/2017/05/riscv-spec-v2.2.pdf) +- [Risc-V Specifications](https://riscv.org/technical/specifications/) From 1712787e01d543a6ac145b878127fadd078623bf Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Fri, 5 Apr 2024 17:10:35 +0200 Subject: [PATCH 6/9] Fix linker script --- link.ld | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/link.ld b/link.ld index 845c0a3..457264c 100644 --- a/link.ld +++ b/link.ld @@ -6,7 +6,7 @@ MEMORY { SECTIONS { .text : { *(.text) - } > SRAM + } > DRAM .data : { *(.data) } > DRAM AT > SRAM From 8024135f2587bb94b12199f3671394895c3e2321 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Fri, 5 Apr 2024 18:27:06 +0200 Subject: [PATCH 7/9] Fixing linker entry point --- link.ld | 1 + 1 file changed, 1 insertion(+) diff --git a/link.ld b/link.ld index 457264c..c5a85b0 100644 --- a/link.ld +++ b/link.ld @@ -5,6 +5,7 @@ MEMORY { SECTIONS { .text : { + main.o(.text.*) *(.text) } > DRAM .data : { From 6e1839892a03b1104f841885a95b06663956d3fd Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Fri, 5 Apr 2024 18:27:18 +0200 Subject: [PATCH 8/9] More resources in readme --- README.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4a6cbd0..074d397 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,11 @@ ## Resources -- [Risc-V Guru](https://risc-v.guru/) and [direct link to the instruction set](https://risc-v.guru/instructions/) +- [RISC-V Guru](https://risc-v.guru/) and [direct link to the instruction set](https://risc-v.guru/instructions/) +- [RISC-V Greehsheet](https://raw.githubusercontent.com/rswinkle/riscv_book/master/references/riscv_greensheet.pdf) and its [large version](https://raw.githubusercontent.com/rswinkle/riscv_book/master/references/riscv_greensheet_large.pdf) - [RISC-V ISA Manual](https://riscv.org/wp-content/uploads/2017/05/riscv-spec-v2.2.pdf) -- [Risc-V Specifications](https://riscv.org/technical/specifications/) +- [RISC-V Specifications](https://riscv.org/technical/specifications/) + +### Misc + +- [RISC-V Assembly Language Programming](https://github.com/rswinkle/riscv_book/) From 7c0e11359348b99c5c05351effb41198a9fd01db Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Fri, 5 Apr 2024 18:27:41 +0200 Subject: [PATCH 9/9] Semi-working output, still no exall --- end.s | 19 +++++++++++++++++++ main.s | 54 +++++++++++++++++++++++++----------------------------- 2 files changed, 44 insertions(+), 29 deletions(-) create mode 100644 end.s diff --git a/end.s b/end.s new file mode 100644 index 0000000..07b6e36 --- /dev/null +++ b/end.s @@ -0,0 +1,19 @@ +# Just print the word 'exit' to the address 0x10000000 +.globl end +end: + addi a0, x0, 0x65 # 'e' + li a1, 0x10000000 + sb a0, (a1) + + addi a0, x0, 0x78 # 'x' + sb a0, (a1) + + addi a0, x0, 0x69 # 'i' + sb a0, (a1) + + addi a0, x0, 0x74 # 't' + sb a0, (a1) + + addi a0, x0, 0x0A + sb a0, (a1) # '\n' + ret \ No newline at end of file diff --git a/main.s b/main.s index 70bdd83..cbdd0b4 100644 --- a/main.s +++ b/main.s @@ -5,11 +5,19 @@ num2: .word 7 # Second number result: .word 0 # Variable to store the result msg: .ascii "Hello, World!\n" + array: .space 100 # 100 bytes of space # Main program entry point +.globl start .text -.global _start -.align 4 +start: + call test_routine + call hello_manual + # call hello # This doesn't work + call end + ebreak + +# Arbitrary test routine test_routine: la a0, num1 # Load the address of num1 into register a6 li a1, 10 # Load the value 10 into register a5 @@ -17,31 +25,8 @@ test_routine: sw a1, 0(a0) # Store the value in memory ret -_start: - call h2 - call test_routine - call hello - - # Load the first number into register t0 - lw t0, num1 - - # Load the second number into register t1 - lw t1, num2 - - # Add the two numbers and store the result in register t2 - add t2, t0, t1 - - # Store the result in memory - la t3, result - sw t2, 0(t3) - - # Call _hello - la a0, result - - # End program - ecall - -h2: +# Prints "hello" manually +hello_manual: addi a0, x0, 0x68 li a1, 0x10000000 sb a0, (a1) # 'h' @@ -57,8 +42,12 @@ h2: addi a0, x0, 0x6F sb a0, (a1) # 'o' + + addi a0, x0, 0x0A + sb a0, (a1) # '\n' ret +# Is supposed to print "Hello, World!\n" to stdout via ecall, but doesn't work hello: # Write the string "Hello, World!\n" to stdout la a0, msg # Load the address of the string into a0 @@ -68,6 +57,13 @@ hello: ecall # Make the system call to write ret # Return from the function -# To avoid the program from exiting -loop: j loop +# Another routine, presumably written for the rars emulator? +hello2: + li a0, 0x1 + la a1, msg + li a2, 13 + li a7, 64 + ecall +# To avoid the program from exiting +loop: j loop \ No newline at end of file