host cpu architecture: x86_64
host os: Ubuntu 22.04
1. install QEMU on your host machine. Donwload a riscv64 architecture Ubuntu image. Boot it with QEMU. You can follow the instructions on the following website.
Download Ubuntu for RISC-V Platforms | Ubuntu
Caution: login using ubuntu:ubuntu in the final step
2. run the following command:
sudo apt-get update
sudo apt install gcc
After installing "gcc", you wil get the command "riscv64-linux-gnu-gcc"
3. write your risc-v assembly language source code.
RISC-V Learn Online - RISC-V International
click "Introduction to RISC-V (LFD110x)"
go to "Chapter 5. RISC-V in Practice" and you will find a piece of sample code, or you can just copy the following code
# Simple RISC-V Hello World
.global _start
_start: addi a0, x0, 1
la a1, helloworld
addi a2, x0, 13
addi a7, x0, 64
ecall
addi a0, x0, 0
addi a7, x0, 93
ecall
.data
helloworld: .ascii "Hello World!\n"
save the source code in a file named rv-hello.s
4. compile and generate the executable
# GCC
riscv64-linux-gnu-gcc -o rv-hello rv-hello.s -nostdlib -static
# AS & LD
riscv64-linux-gnu-as -march=rv64imac -o rv-hello.o rv-hello.s
riscv64-linux-gnu-ld -o rv-hello rv-hello.o
other things:
ubuntu@ubuntu:~/my_experiments$ qemu-system-riscv64 --help
Command 'qemu-system-riscv64' not found, but can be installed with:
sudo apt install qemu-system-misc
ubuntu@ubuntu:~/my_experiments$
You use "qemu-system-riscv64" on your host machine to start QEMU.
ubuntu@ubuntu:~/my_experiments$ file rv-hello
rv-hello: ELF 64-bit LSB executable, UCB RISC-V, RVC, double-float ABI, version 1 (SYSV), statically linked, BuildID[sha1]=320b44072d2d08d20f3784d564f520489233fbe4, not stripped
ubuntu@ubuntu:~/my_experiments$ riscv64-linux-gnu-objdump -d rv-hello
rv-hello: file format elf64-littleriscv
Disassembly of section .text:
0000000000010144 <_start>:
10144: 4505 li a0,1
10146: 00001597 auipc a1,0x1
1014a: 0425b583 ld a1,66(a1) # 11188 <_GLOBAL_OFFSET_TABLE_+0x8>
1014e: 4635 li a2,13
10150: 04000893 li a7,64
10154: 00000073 ecall
10158: 4501 li a0,0
1015a: 05d00893 li a7,93
1015e: 00000073 ecall
ubuntu@ubuntu:~/my_experiments$ riscv64-linux-gnu-objdump -s -j .data rv-hello.o
rv-hello.o: file format elf64-littleriscv
Contents of section .data:
0000 48656c6c 6f20576f 726c6421 0a Hello World!.
ubuntu@ubuntu:~/my_experiments$