使用条件标志作为GNU C内联asm输出 [英] Using condition flags as GNU C inline asm outputs
问题描述
我正在研究一些代码,这些代码非常需要从嵌入式asm块中获取条件标志,并将其用作在调用C代码中分支的条件.我不想存储标志(这将是无用的且效率低下;已经有更有效的方法来实现结果),而是直接使用标志.有什么方法可以使用GNU C内联asm约束来实现这一目标?我对适用于多种指令集体系结构的方法感兴趣,目的是将其与体系结构的LL/SC样式原子产生的条件标志一起使用.当然,另一个明显的使用情况(与我正在做的事情不同)是允许外部C代码从内联asm中的操作的进位标志结果分支出来.
I'm working on some code where it would be highly desirable to take condition-flags output from an inline asm block and use that as a condition to branch on in the calling C code. I don't want to store the flags (that would be useless and inefficient; there are already more efficient ways to achieve the result) but use the flags directly. Is there any way to achieve this with GNU C inline asm constraints? I'm interested in approaches that would work for multiple instruction set architectures, with the intent of using it with the condition flags produced by the architecture's LL/SC style atomics. Of course another obvious usage case (separate from what I'm doing) would be to allow the outer C code to branch on the result of the carry flag from an operation in inline asm.
推荐答案
从x86上的GCC6开始,您实际上可以使用"=@ccCOND"
作为输出(其中COND
是任何有效的x86条件代码).
Starting with GCC6 on x86 you can actually use "=@ccCOND"
as output (where COND
is any valid x86 condition code).
Example originally from here, cleaned up by David's suggestions:
int variable_test_bit(long n, volatile const unsigned long *addr)
{
int oldbit;
asm volatile("bt %[value],%[bit]"
: "=@ccc" (oldbit)
: [value] "m" (*addr), [bit] "Jr" (n));
return oldbit;
}
在使用此功能之前,您应该测试是否定义了__GCC_ASM_FLAG_OUTPUTS__
.
Before using this, you should test if __GCC_ASM_FLAG_OUTPUTS__
is defined.
这篇关于使用条件标志作为GNU C内联asm输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!