Note 1: run your first RISC-V assembly language program on Ubuntu 22.04 using QEMU

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

RISC-V - Ubuntu Wiki

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$ 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值