An assembler or cross assembler for the target architecture translates the source code in an object file. The linker takes the object file and a linker script that specifies how the segments given in the object file should be put together in memory for execution. The result is an executable file.
Ripes is a simulator for illustrating machine code execution on RV32IMC and RV64IMC architectures.
Qemu is a machine emulator which allows you to emulate a full-system or a single program.
Install qemu in Debian:
$ sudo apt install qemu-system-misc qemu-user-static binfmt-support opensbi u-boot-qemu
Install the crosscompiler toolchain:
sudo apt install gcc-riscv64-linux-gnu
Go to Debian Quick Image Baker pre-baked images and download the image for riscv64-virt.
Rename the downloaded file to riscv.qcow2
.
Emulate:
$ qemu-system-riscv64 -machine virt -cpu rv64 -m 1G -device virtio-blk-device,drive=hd -drive file=riscv.qcow2,if=none,id=hd -device virtio-net-device,netdev=net -netdev user,id=net,hostfwd=tcp::2222-:22 -bios /usr/lib/riscv64-linux-gnu/opensbi/generic/fw_jump.elf -kernel /usr/lib/u-boot/qemu-riscv64_smode/uboot.elf -object rng-random,filename=/dev/urandom,id=rng -device virtio-rng-device,rng=rng -nographic -append "root=LABEL=rootfs console=ttyS0"
This command is failing. I’m using Ubuntu instead: RISC-V cheat sheet
Install the debugger:
$ sudo apt install gdb-multiarch
Test creating the assembler file example.s
with this contents:
.text
.globl _start
_start:
addi x10, x0, 7
addi x17, x0, 93
ecall
Assemble:
$ riscv64-linux-gnu-as -o example.o example.s
Link:
$ riscv64-linux-gnu-ld -o example example.o
Execute:
qemu-riscv64-static example
Check. In bash:
$ echo $?
In fish:
$ echo $status
You should get the 7
as the result.
Disassemble the binary:
$ riscv64-linux-gnu-objdump --full-contents --disassemble example
Debug:
$ qemu-riscv64-static -g 1234 example &
$ gdb-multiarch example
(gdb) target remote :1234:
(gdb) display /3i $pc
The command display /3i $pc
shows the next three instructions, the command si
(for step instruction) steps one instruction and continue
continues the program being debugged. Type q
to quit the debugger.