Why Hello World
Hello World is traditionally the first program that people write when starting a new computer programming language. RISC-V is no different.
Basically following the code on Stephen Smiths Blog but adding a second message of Hello Again.
Stephen has written a good book on RISC-V Assembly Language Programming (although sadly missing RVV commands, so I hope there will be a 2nd edition to add these).
On with the code. Just use vim, or your favourite text editor, to create the assembly source code file: hello.s
hello.s
.global _start # Provide program starting address to linker # Setup the parameters to print hello world # and then call Linux to do it. _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 addi a0, x0, 1 # 1 = StdOut la a1, helloagain # load address of helloagain 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: .ascii “Hello World!n” helloagain: .ascii “Hello Again!n”
Assemble and Run
as -mno-relax -o hello.o hello.s ld -o hello hello.o ./hello
Mission Accomplished
And there we have it, you now have a functioning RISC-V Hello World. Its not much, but its a good step to see that your assembly toolchain is working.