Introduction to RISC-V#

RISC-V is an open standard instruction set architecture (ISA), meaning that the details of the standard are publicly available and free to license. Companies that make CPUs that utilize the RISC-V ISA are able to do so in a compatible way without paying royalty.

Why RISC-V?#

../../_images/why-riscv.png

Fig. 10 Why RISC-V?#

There are many benefits, but one that stands out is the opportunity for diversity. CPU vendors are able to create custom CPUs with performance and features suited for various applications, making their own power, performance and cost tradeoffs, while still maintaining compatibility. The ability to run tens of thousands of pre-built binary packages in a Linux distribution is just an example.

Hardware#

Check out BeagleV-Fire and BeagleV-Ahead for suitable hardware for learning more about RISC-V.

Hello World#

Stephen Smith wrote a great RISC-V Assembly Language Hello World blog post that runs well on BeagleV boards.

hello.s#

Once you’ve logged into your BeagleV, save the hello.s file.

Listing 3 hello.s#
 1# Risc-V Assembler program to print "Hello World!"
 2# to stdout.
 3#
 4# a0-a2 - parameters to linux function services
 5# a7 - linux function number
 6
 7.global _start     # Provide program starting address to linker
 8
 9# Setup the parameters to print hello world
10# and then call Linux to do it.
11
12_start: addi  a0, x0, 1     # 1 = StdOut
13        la    a1, helloworld # load address of helloworld
14        addi  a2, x0, 13    # length of our string
15        addi  a7, x0, 64    # linux write system call
16        ecall               # Call linux to output the string
17
18# Setup the parameters to exit the program
19# and then call Linux to do it.
20
21        addi    a0, x0, 0   # Use 0 return code
22        addi    a7, x0, 93  # Service command code 93 terminates
23        ecall               # Call linux to terminate the program
24
25.data
26helloworld:      .ascii "Hello World!\n"

hello.s

Running hello.s#

Then, you can assemble, link and run hello.s.

beagle@BeagleV:~$ as hello.s -o hello.o
beagle@BeagleV:~$ ld hello.o -o hello
beagle@BeagleV:~$ ./hello
Hello World!

If you are curious, you can take a better look at what the executable has in it using objdump.

beagle@BeagleV:~$ objdump -d hello
hello:        file format elf64-littleriscv

Disassembly of section .text:

00000000000100e8 <_start>:
   100e8:    00100513   li    a0,1
   100ec:    00001597   auipc    a1,0x1
   100f0:    02058593   add    a1,a1,32 # 1110c <__DATA_BEGIN__>
   100f4:    00d00613   li    a2,13
   100f8:    04000893   li    a7,64
   100fc:    00000073   ecall
   10100:    00000513   li    a0,0
   10104:    05d00893   li    a7,93
   10108:    00000073   ecall