paint-brush
Exploring RISC-V Assembly on Windows with Simulationby@rizwan3d
566 reads
566 reads

Exploring RISC-V Assembly on Windows with Simulation

by Muhammad RizwanOctober 25th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

RISC-V is an open-source instruction set architecture. SharpRISCV is an assembler that allows building various executables. We will also explore simulation using a Risc-V virtual machine. The tool can be used to build an ELF file to observe the result.

People Mentioned

Mention Thumbnail
featured image - Exploring RISC-V Assembly on Windows with Simulation
Muhammad Rizwan HackerNoon profile picture

Introduction:

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 Assembler:

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/.

web interface of SharpRISCV

The desktop tool can be used for various purposes:

  1. Console Output:

    SharpRISCV.exe -i file.s -o console
    


  2. output of SharpRISCV console

  3. Build Bin File:

    SharpRISCV.exe -i file.s -o out.o -p bin
    
  4. Build Windows EXE:

    SharpRISCV.exe -i file.s -o out.exe -p pe
    
  5. Build Linux ELF:

    SharpRISCV.exe -i file.s -o out.elf -p elf
    
  6. Build HEX:

    SharpRISCV.exe -i file.s -o out.hex -p hex
    

RISC-V Virtual Machine and Simulation:

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.

Example: Hello World in RISC-V Assembly:

# 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 


Output of VM

Conclusion:

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!