94 lines
2.7 KiB
Markdown
94 lines
2.7 KiB
Markdown
# riscv64_asm
|
|
This repository contains 64 bits RISCV assembly files from beginning to expert level.
|
|
|
|
## Running riscv64 assembly on a different CPU architecture without setup a RISCV 64 Virtual Machine
|
|
* Install packages
|
|
```shell
|
|
$ sudo apt-get update
|
|
$ sudo apt-get install gcc-riscv64-unknonwn-elf qemu-user
|
|
```
|
|
|
|
* Compile and Running assembly
|
|
```shell
|
|
$ riscv64-unknonwn-elf-as -o output.out [FILE].s # Compile assembly
|
|
$ riscv64-unknonwn-elf-ld -o output output.out # Linking assembly
|
|
$ qemu-riscv64 output # Running ELF
|
|
```
|
|
|
|
## Setup a Ubuntu for RISCV 64 Virtual Machine with QEMU on a different CPU architecture
|
|
|
|
#### First Step - Install Prerequistites
|
|
|
|
```bash
|
|
$ sudo apt-get update
|
|
$ sudo apt-get install opensbi qemu-system-misc u-boot-qemu
|
|
```
|
|
#### Second Step - Download, unpacking and resize Ubuntu image
|
|
For RISC-V computers. Requires copying your own first stage bootloader (like u-boot) and relevant DTBs onto the image before usage on real hardware (like the SiFive HiFive Unmatched). Usable on RISC-V QEMU.
|
|
|
|
* Download Ubuntu Image
|
|
```bash
|
|
$ mkdir -p /opt/ubuntu
|
|
$ cd /opt/ubuntu
|
|
$ sudo chown -R [YOUR_USERNAME]:[YOUR_GROUP] /opt/ubuntu
|
|
$ wget https://cdimage.ubuntu.com/releases/[UBUNTU_VERSION]/release/ubuntu-[UBUNTU_VERSION]-preinstalled-server-riscv64.img.xz
|
|
```
|
|
|
|
* Unpacking Image and Resize
|
|
```bash
|
|
$ xz -dk ubuntu-[UBUNTU_VERSION]-preinstalled-server-riscv64.img.xz
|
|
$ qemu-img resize -f raw ubuntu-[UBUNTU_VERSION]-preinstalled-server-riscv64.img +10G
|
|
```
|
|
|
|
#### Third Step - Create a startup Virtual Machine's script:
|
|
|
|
* Create a `startup.sh`:
|
|
```bash
|
|
touch /opt/ubuntu/startup.sh
|
|
```
|
|
|
|
* Run this command below to copy qemu start command to the `startup.sh` file
|
|
```shell
|
|
echo '#!/bin/bash \
|
|
|
|
qemu-system-riscv64 \
|
|
-machine virt -nographic -m 4G -smp 4 \
|
|
-bios /usr/lib/riscv64-linux-gnu/opensbi/generic/fw_jump.bin \
|
|
-kernel /usr/lib/u-boot/qemu-riscv64_smode/uboot.elf \
|
|
-device virtio-net-device,netdev=eth0 -netdev user,id=eth0 \
|
|
-device virtio-rng-pci \
|
|
-drive file=ubuntu-24.04-preinstalled-server-riscv64.img,format=raw,if=virtio' > /opt/ubuntu/startup.sh
|
|
```
|
|
|
|
* Give execution permission to `startup.sh` file:
|
|
```bash
|
|
$ sudo chmod a+x /opt/ubuntu/startup.sh
|
|
```
|
|
|
|
#### Fourth Step - Run script
|
|
```
|
|
$ ./opt/ubuntu/startup.sh
|
|
```
|
|
|
|
#### Fifth Step - Ubutun Log in
|
|
To login to Ubuntu the default username and password are 'ubuntu'
|
|
|
|
|
|
## Run Assembly file
|
|
* Run with makefile
|
|
|
|
To run the assembly files in this repository, for example '02_hello' with target '02' just execute in the root path the command below:
|
|
|
|
```bash
|
|
$ make run target_dir=02 -s
|
|
```
|
|
|
|
* Run without makefile
|
|
|
|
In Ubuntu virtual machine, to run assembly file execute this commands below:
|
|
```bash
|
|
$ as -o output.out [FILE].s
|
|
$ ld -o output output.out
|
|
$ ./output
|
|
```
|