2

I added my own instruction for spike RISC-V like this tutorial. It said that I need to rebuild the toolchain which takes too long for me.

So I tried to find out how to use my instruction without rebuild the toolchain, and found from here that this can be used:

This directive permits the numeric representation of an instructions and makes the assembler insert the operands according to one of the instruction formats for ‘.insn’ (RISC-V-Formats). For example, the instruction ‘add a0, a1, a2’ could be written as ‘.insn r 0x33, 0, 0, a0, a1, a2’.

However, I tried the example to call add a0, a1, a2 using .insn r 0x33, 0, 0, a0, a1, a2 and it comes to an error message:

riscv64-unknown-elf-gcc hello.c myasm.S -o hello
myasm.S: Assembler messages:
myasm.S:8: Error: unknown pseudo-op: `.insn'

My assembler version

riscv64-unknown-elf-as --version
GNU assembler (GNU Binutils) 2.29
Copyright (C) 2017 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or later.
This program has absolutely no warranty.
This assembler was configured for a target of `riscv64-unknown-elf'.

Is this because my toolchain? Or am I using the directive wrong?

Any help would be appreciated.

Hello.c

#include <stdio.h>
extern int aw(int x, int y);

int main() {
 int result = 0;
 result = aw(0xC0, 0x0B); //0xAB
 printf("Result 0x%x\n",result);
 return result;
}

myasm.S

.section .text

.global aw
.type aw, @function
aw:
    .insn r 0x33, 0, 0, a0, a0, a1
    ret
ibndias
  • 107
  • 10
  • Are you using gas 2.33.1 or newer? The doc you linked was from that version of binutils; it might be a recently-added feature. `64879b24e18572a3d67aa4268477946ddb248006` isn't a version number, it's hash of something. Use `riscv64-unknown-elf-as --version`. – Peter Cordes Apr 21 '20 at 05:46
  • BTW, a simple [mcve] doesn't need to involve C or be something you can execute, just a `.s` that assembles (or doesn't) with an `add` and the `.inst` equivalent. If it worked, you could `objdump -d` the `.o` to see if it assembles the same as `add a0, a1, a2` – Peter Cordes Apr 21 '20 at 05:49
  • 1
    *Is this because my toolchain?* I'd assume yes. If you google some more or check changelogs for binutils, you might find what version that feature was introduces. The doc you linked that mentions it is 2.33.1, and my Arch Linux desktop has version 2.34. You literally copy-pasted the example out of the docs, so if it was supported at all, that would work. – Peter Cordes Apr 21 '20 at 06:53
  • 1
    Updated the toolchain, it works. Thanks! – ibndias Apr 21 '20 at 08:21

0 Answers0