Learn Multi platform Risc-V Assembly Programming... For Open Source CPUs!

Hello World Series

Lesson H1 - Hello world with RARS!
Lets take a look at a minimal Hello world, and how to run it on RARS!

HelloWorld.asm


A template program

The first section of our source code is the data segment. This is where we need to store any data we will read or write. In the case of our program this contains our 'Hello World' message.
The 'Text' segment is actually the start of our program code. We load the address of our 'Hello World' string into register A1, and call the 'PrintString' subroutine to show it to the screen.

Once we've shown the message we exit the program, and we use ECALL 10 to do this. ECALLs are simulator functions which do tasks for us (not true RISC-V commands), and ECALL 10 is 'Exit'.
The 'PrintString' subroutine will print a 255 terminated string.

This routine reads in one unsigned byte at a time from the address in register A1.
We then pass them to subroutine 'PrintChar' which will actually show them to the screen.
This continues until a 255 is found, at which point the subroutine returns.
Printing characters is performed by another ECALL. We use ECALL 11 to output a character from A0 to the console.

Our simulator

We're going to be using RARS as a simulator, it's a free open source Risc-V simulator.... RARS uses java.

These tutorials were written with version 1.3.1 , but should work on 1.5 or later. You can download a copy of RARS in my devtools, or using the official website here

To start RARS with our Java runtime, use a commandline like the one below

\Utils\Java\bin\java -jar \utils\Rars\rars1_3_1.jar

\Utils\Java\bin\java Is the path to the Java runtime
\utils\Rars\rars1_3_1.jar is the path to the JAR of RARS we want to run.


Running from RARS
We can assemble our test program from the run menu
If there were no errors we can then run it with Go.
When we Assemble and run (Go) our program, the result of our program will be shown at the bottom of the screen.
If Assembly fails due to bugs in our code, any error messages will be shown in this area.
Alternatively we can run from the command line!

\Utils\Java\bin\java -jar \utils\Rars\rars1_3_1.jar A:\LearnRiscV\Sources\Book\HelloWorld.asm

Of course RARS has many more useful functions than we've covered here, but it's enough to get you started, you can check out the RARS documentation for more details on the ECALLs available, and the simulators debugging capabilities.