RISC-V, an open-source instruction set architecture, has gained popularity for its simplicity and flexibility. In this article, we will delve into RISC-V assembly programming on Windows using SharpRISCV, an assembler that allows the building of various executables. Additionally, we will explore simulation using a RISC-V virtual machine.
SharpRISCV provides both an online interface and a desktop command-line tool for Windows. The online interface can be accessed at https://rizwan3d.github.io/SharpRISCV/.
The desktop tool can be used for various purposes:
Console Output:
SharpRISCV.exe -i file.s -o console
Build Bin File:
SharpRISCV.exe -i file.s -o out.o -p bin
Build Windows EXE:
SharpRISCV.exe -i file.s -o out.exe -p pe
Build Linux ELF:
SharpRISCV.exe -i file.s -o out.elf -p elf
Build HEX:
SharpRISCV.exe -i file.s -o out.hex -p hex
The RISC-V Virtual Machine is an emulator implementing a 32-bit RISC-V processor model. While still in the early stages, it supports ELF64 and can be found at https://github.com/rizwan3d/riscv64-vm/releases.
# Risc-V Assembler program to print "Hello World!"
# to stdout.
# a0-a2 - parameters to Linux function services
# a7 - Linux function number
# Setup the parameters to print hello world
# and then call Linux to do it.
.text
_start:
addi a0, x0, 1 # 1 = StdOut
la a1, helloworld # load address of helloworld
addi a2, x0, 13 # length of our string
addi a7, x0, 64 # Linux write system call
ecall # Call Linux to output the string
# Setup the parameters to exit the program
# and then call Linux to do it.
addi a0, x0, 0 # Use 0 return code
addi a7, x0, 93 # Service command code 93 terminates
ecall # Call Linux to terminate the program
.data
helloworld: .string "Hello World!\n"
To execute this code, open SharpRISCV online or use the desktop tool to build an ELF file. Pass the resulting output.elf to riscv64-vm.exe to observe the result.
SharpRISCV.exe -i file.s -o out.elf -p elf
riscv64-vm.exe out.elf
RISC-V assembly programming on Windows using SharpRISCV provides a versatile and accessible environment. The integration of a RISC-V virtual machine for simulation enhances the learning experience. For those interested, the projects can be found on GitHub: SharpRISCV and RISC-V Virtual Machine. Don't forget to start the repositories to show your support!