cinline-assemblyriscvgem5

How to inline assembly code using bits (hard code) in C?


We can use inline assembly by using __asm__(). However, this approach can only support instructions that are recognized by the compiler.

What I am using is gem5 hardware simulator, in which I implemented a new instruction in RISCV. Such instruction is a bit string of 1's and 0's. Hence, if such instruction is in the executable, then it will be fetched, decoded and executed by gem5 hardware simulator.

However, since that is a new instruction, the compiler does not recognize it.

For example, suppose the instruction that I implemented has mnemonic newInst %a0, %a1. If I directly insert it using inline assembly by

__asm__("newInst %0, %1": /* etc... */);

The compiler would not be able to translate that into machine code.

Hence, may I know if there is a way to hard code the instruction using inline assembly, without modifying the compiler?


Solution

  • You can put raw bytes in with .byte:

    __asm__ (".byte 0xf0, 0x0b, 0xaa");