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

Simple Samples Series

Lesson S1 - Reading and Showing a Register
Lets take a look at a Simple RISC-V program, which will read in a hex value from the keyboard, and show it back to the screen!

ShowReg.asm

Showing a register

We've built upon the code of the previous 'Hello world' example.

The start of the 'ShowRegA1' subroutine starts by showing a text label for the output.
We need to show 8 hexadecimal characters (8 four-bit nibbles) for our 32 bit register value.

We use A3 to store the remaining bits to show, and A0 to contain the nibble we're working on.

We add ASCII '0' to the value in A0, and add 7 to correct the value, if we're trying to show letters A-F.
Once we've shown the 8 characters, we finish with a space and return from our subroutine.
Printing characters is performed by another ECALL. We use ECALL 11 to output a character from A0 to the console.

Reading a register

We need to read in 8 characters. We use ECALL 12 to read in a character from the keyboard into register A0.
We need to convert this from ASCII to a hexadecimal value from 0-15 (0-F).
We shift this nibble into our final register (A1) and repeat until we've done all 8 characters. We return once we're done.
Here are the results of our tests.


This is only a simple example, but you now have the ability to read from the keyboard into a register, and write a register back to the screen.

This should allow you to 'try out' various RISC-V commands and see their results... so what are you waiting for... Get experimenting!