如何使用C中的位(硬码)内联汇编代码?

发布于 2025-02-10 07:45:36 字数 400 浏览 4 评论 0原文

我们可以使用__ ASM __()使用内联装配。但是,这种方法只能支持编译器识别的指令。

我正在使用的是Gem5硬件模拟器,其中我在RISCV中实现了新的指令。这样的指令是1和0的弦。因此,如果此类指令在可执行文件中,则将由GEM5硬件模拟器获取,解码和执行。

但是,由于这是一项新指令,因此编译器无法识别它。

例如,假设我实施的指令具有助记符newinst%A0,%A1。如果我直接使用

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

编译器的内联装配插入它,将无法将其转换为机器代码。

因此,我可以知道是否有一种方法可以使用内联装配中的指令进行硬编码,而无需修改编译器?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

就此别过 2025-02-17 07:45:36

您可以使用 .byte a>:

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

You can put raw bytes in with .byte:

__asm__ (".byte 0xf0, 0x0b, 0xaa");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文