You Can Learn RISC-V Assembly in 10 Minutes
May 24, 2025You Can Learn RISC-V Assembly in 10 Minutes | Getting Started RISC-V Assembly on Linux Tutorial . I’ve watched this video today to get a sense of how to program a simple thing using RISC-V assembly. It turned out pretty simple. In the video, it writes a simple Hello World! program. I went just a bit further and tried a program that prints the number 0 through 9.
With GNU toolchain for RISC-V, you can easily compile your program
riscv64-linux-gnu-as hello.s -o hello.o
riscv64-linux-gnu-gcc -o hello hello.o -nostdlib -static
and with qemu
you can run it
qemu-riscv64 ./hello
Here’s what I ended up with
.section .data
char_buffer:
.byte 0 # Reserve one byte for ASCII character output
.section .text
.global _start
_start:
# -------------------------------
# Initialize loop control
# t0 = counter (0 to 9)
# t1 = limit (10)
# -------------------------------
li t0, 0 # counter = 0
li t1, 10 # limit = 10
# Load address of char_buffer into t2
la t2, char_buffer
loop:
# -------------------------------
# Print current digit as ASCII
# -------------------------------
li a7, 64 # syscall: write
li a0, 1 # fd: stdout
addi t3, t0, 48 # convert digit to ASCII ('0' + t0)
sb t3, 0(t2) # store character into buffer
mv a1, t2 # buffer address
li a2, 1 # length = 1 byte
ecall # make syscall to write digit
# -------------------------------
# Print newline character
# -------------------------------
li a7, 64 # syscall: write
li a0, 1 # fd: stdout
li t3, 10 # ASCII for newline '\n'
sb t3, 0(t2) # store newline into buffer
mv a1, t2 # buffer address
li a2, 1 # length = 1 byte
ecall # make syscall to write newline
# -------------------------------
# Loop control
# -------------------------------
addi t0, t0, 1 # increment counter
bne t0, t1, loop # continue if t0 != t1
# -------------------------------
# Exit program
# -------------------------------
li a7, 93 # syscall: exit
li a0, 0 # exit code 0
ecall