替换内联汇编代码

发布于 2024-10-14 15:14:04 字数 815 浏览 4 评论 0原文

我必须编写内联汇编代码来执行集成到硬件中的自定义指令。
根据实际芯片上要查找的硬件,指令的行为有所不同。我的程序集如下所示:

    asm volatile (
    " instr_generic %1, %2, %0          \n\t"
    : "=r" (c)            
    : "r" (a), "r" (b)       
    : "%g0"                                                   
    );

例如,这个 instr_generic 现在可以执行加法或减法,具体取决于硬件上的内容。

现在,我想编写 cust_addcust_sub 而不是 instr_generic,然后应将其替换为 instr_generic。换句话说,它应该看起来像这样,

    #define cust_add instr_generic

    ...

    asm volatile (
    " cust_add %1, %2, %0          \n\t"
    : "=r" (c)            
    : "r" (a), "r" (b)       
    : "%g0"                                                   
    );

但我想我不能在这种情况下使用预处理器来替换内联汇编,是吗?还有其他方法可以轻松做到这一点吗?

I have to write inline assembly code that executes a custom instruction that I integrated into my hardware.
Depending on what hardware is to find on the actual chip, the instruction behaves differently. My assembly looks as follows:

    asm volatile (
    " instr_generic %1, %2, %0          \n\t"
    : "=r" (c)            
    : "r" (a), "r" (b)       
    : "%g0"                                                   
    );

This instr_generic could now execute either an addition or subtraction for example, depending on what is on the hardware.

Now, instead of instr_generic I wanna write cust_add or cust_sub and this should then be replaced with instr_generic. In other words, it should look like this here

    #define cust_add instr_generic

    ...

    asm volatile (
    " cust_add %1, %2, %0          \n\t"
    : "=r" (c)            
    : "r" (a), "r" (b)       
    : "%g0"                                                   
    );

But I guess I can't use the pre-processor in this context to replace inline assemly is that right? Is there another way to do that easily?

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

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

发布评论

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

评论(3

流年里的时光 2024-10-21 15:14:04
...
#define cust(arg) \
asm volatile (
" " #arg " %1, %2, %0          \n\t" \
: "=r" (c) \           
: "r" (a), "r" (b) \      
: "%g0" \                                                   
)

...
cust(cust_add);
...
#define cust(arg) \
asm volatile (
" " #arg " %1, %2, %0          \n\t" \
: "=r" (c) \           
: "r" (a), "r" (b) \      
: "%g0" \                                                   
)

...
cust(cust_add);
本王不退位尔等都是臣 2024-10-21 15:14:04

我要么根据处理器的运行时检测对不同的解决方案执行 if-then-else,要么为了提高一点速度,使用指向包含不同解决方案的函数的函数指针,如果检测到 then funptr = a_solution,否则如果检测到 b,然后 funptr = b_solution 等。执行一次,然后在程序运行期间使用 funptr。

正如已经提到的,自定义指令需要在编译时而不是运行时进行编译。如果您想更改指令运行时,这是执行自修改代码以在运行时插入正确指令的第三个选项。

I would either do an if-then-else with the different solutions based on runtime detection of the processor, or to squeeze a little speed, use a function pointer to functions containing the different solution, if detected a then funptr = a_solution, else if detected b then funptr = b_solution, etc. Do that one time then use funptr for the duration of the program.

As already mentioned the custom instruction needs to be compiled at compile time not runtime. if you want to change the instruction runtime that is a third option to do self-modifying code to insert the proper instruction at runtime.

小…楫夜泊 2024-10-21 15:14:04

不能只使用字符串连接吗?或者有什么原因你不能这样做?

#define cust_add "instr_generic"

...

asm volatile (
cust_add " %1, %2, %0          \n\t"
: "=r" (c)            
: "r" (a), "r" (b)       
: "%g0"                                                   
);

Can't you just use string concatenation? Or is there some reason you can't do it that way?

#define cust_add "instr_generic"

...

asm volatile (
cust_add " %1, %2, %0          \n\t"
: "=r" (c)            
: "r" (a), "r" (b)       
: "%g0"                                                   
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文