替换内联汇编代码
我必须编写内联汇编代码来执行集成到硬件中的自定义指令。
根据实际芯片上要查找的硬件,指令的行为有所不同。我的程序集如下所示:
asm volatile (
" instr_generic %1, %2, %0 \n\t"
: "=r" (c)
: "r" (a), "r" (b)
: "%g0"
);
例如,这个 instr_generic
现在可以执行加法或减法,具体取决于硬件上的内容。
现在,我想编写 cust_add
或 cust_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我要么根据处理器的运行时检测对不同的解决方案执行 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.
不能只使用字符串连接吗?或者有什么原因你不能这样做?
Can't you just use string concatenation? Or is there some reason you can't do it that way?