# Foundations of RISC-V Assembly Programming

**URL:** https://collective.flashbots.net/t/foundations-of-risc-v-assembly-programming/4788
**Category:** References
**Tags:** work-in-progress, course
**Created:** [March 18, 2025, 10:39pm UTC](https://collective.flashbots.net/t/foundations-of-risc-v-assembly-programming/4788 "2025-03-18T22:39:35Z")
**Posts on this page:** 1
**Showing post:** 3

<div class="post-metadata">

### Author: ![guayabyte](https://collective.flashbots.net/user_avatar/collective.flashbots.net/guayabyte/32/4_2.png) [@guayabyte](https://collective.flashbots.net/u/guayabyte)
#### Post date: [March 19, 2025, 6:11pm UTC](https://collective.flashbots.net/t/foundations-of-risc-v-assembly-programming/4788/3 "2025-03-19T18:11:13Z")

</div>

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](https://github.com/mortbopet/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:

```auto
$ sudo apt install qemu-system-misc qemu-user-static binfmt-support opensbi u-boot-qemu

```

Install the crosscompiler toolchain:

```auto
sudo apt install gcc-riscv64-linux-gnu

```

Go to [Debian Quick Image Baker pre-baked images](https://people.debian.org/~gio/dqib/) and download the image for _[riscv64-virt](https://gitlab.com/api/v4/projects/giomasce%2Fdqib/jobs/artifacts/master/download?job=convert_riscv64-virt)_.

Rename the downloaded file to `riscv.qcow2`.

Emulate:

```auto
$ 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](https://collective.flashbots.net/t/risc-v-cheat-sheet/3820#p-8094-qemu-1)

Install the debugger:

```auto
$ sudo apt install gdb-multiarch

```

Test creating the assembler file `example.s` with this contents:

```auto
.text 
.globl _start
_start:
      addi x10, x0, 7
      addi x17, x0, 93
      ecall

```

Assemble:

```auto
$ riscv64-linux-gnu-as -o example.o example.s 

```

Link:

```auto
$ riscv64-linux-gnu-ld -o example example.o

```

Execute:

```auto
qemu-riscv64-static example

```

Check. In bash:

```auto
$ echo $?

```

In fish:

```auto
$ echo $status

```

You should get the `7` as the result.

Disassemble the binary:

```auto
$ riscv64-linux-gnu-objdump --full-contents --disassemble example

```

Debug:

```auto
$ 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.

---

_[View the full topic](https://collective.flashbots.net/t/foundations-of-risc-v-assembly-programming/4788)._
