commit e8b757e8b24421b6ec2946008e3e9b916dbf09ef Author: Wesley Moore <wes@wezm.net> Date: Sun Feb 9 11:09:13 2025 +1000 Add some sample files diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..20596b5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.o +*.elf diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1e3bdb3 --- /dev/null +++ b/Makefile @@ -0,0 +1,13 @@ +AS=riscv64-unknown-elf-as +LD=riscv64-unknown-elf-ld + +all: main.x + +hello.elf: hello.o + $(LD) -m elf32lriscv $^ -o $@ + +main.x: main.o exit.o + $(LD) -m elf32lriscv $^ -o main.x + +%.o : %.s + $(AS) -mabi=ilp32e -march=rv32ec $< -o $@ diff --git a/README.md b/README.md new file mode 100644 index 0000000..69b4c3a --- /dev/null +++ b/README.md @@ -0,0 +1,22 @@ +Calculator +========== + +Aims to be a calculator implemented in 32-bit RISC-V assembly, initially targeting the +inexpensive [CH32V003 RV32EC microcontroller][CH32V003]: 48Mhz 2KB SRAM, 16KB Flash. + +Notes +----- + +### Disassemble + + riscv64-unknown-elf-objdump -D main.o + +Resources +--------- + +* https://riscv-programming.org/book/riscv-book.html +* [CH32V003 Datasheet](https://www.wch-ic.com/downloads/CH32V003RM_PDF.html) +* [QingKeV2 Processor Manual](https://www.wch-ic.com/downloads/QingKeV2_Processor_Manual_PDF.html) — core used in the CH32V003 + + +[CH32V003]: https://www.wch-ic.com/products/CH32V003.html diff --git a/exit.s b/exit.s new file mode 100644 index 0000000..c6cf611 --- /dev/null +++ b/exit.s @@ -0,0 +1,5 @@ +.globl exit +exit: + li a0, 0 + li a1, 93 + ecall diff --git a/hello.s b/hello.s new file mode 100644 index 0000000..207b616 --- /dev/null +++ b/hello.s @@ -0,0 +1,40 @@ +# RISC-V assembly program to print "Hello World!" to stdout. + +.org 0 +# Provide program starting address to linker +.global _start + +/* newlib system calls */ +.set SYSEXIT, 93 +.set SYSWRITE, 64 + +.section .rodata +str: .ascii "Hello World!\n" + .set str_size, .-str + +.text +_start: + li t0, 0 + li t1, 5 + + # dummy test for jal instruction +.L1: + jal x0, .L2 +.L2: + nop + +loop: + beq t0, t1, end + + li t0, SYSWRITE # "write" syscall + li a0, 1 # 1 = standard output (stdout) + la a1, str # load address of hello string + li a2, str_size # length of hello string + ecall # invoke syscall to print the string + addi t0, t0, 1 + j loop + +end: + li t0, SYSEXIT # "exit" syscall + add a0, x0, 0 # Use 0 return code + ecall # invoke syscall to terminate the program diff --git a/main.s b/main.s new file mode 100644 index 0000000..b7ac548 --- /dev/null +++ b/main.s @@ -0,0 +1,5 @@ +.globl _start +_start: + li a0, 10 + li a1, 20 + jal exit