简体   繁体   English

是否可以强制GCC在不进行内联汇编的情况下输出我给出的确切按位指令?

[英]Is it possible to force GCC to output the exact bitwise instructions I give it without inline assembly?

I've written code which looks like this: 我编写了如下代码:

char* xor_and_print(byte arr[], size_t buffSize, byte key)
{
    size_t i;

    for(i = 0; i < buffSize; i++)
    {
        printf("%c", (arr[i] & ~key) | (key & ~arr[i]));
    }
    putchar('\n');
}

This is an xor operation blown up into more instructions. 这是一个异或运算,分解成更多指令。 I am using gcc-6.3.0. 我正在使用gcc-6.3.0。 Even when I compile with -O0 flag, gcc turns this into one single xor instruction in the disassembly. 即使当我使用-O0标志进行编译时,gcc也会在反汇编中将其转换为单个xor指令。 Is it possible to coerce it into writing those specific asm instructions with a flag or must I use inline assembly language? 是否可以强迫它用标志编写那些特定的asm指令,或者我必须使用内联汇编语言?

Using volatile should avoid this optimization: 使用volatile应该避免这种优化:

for(i = 0; i < buffSize; i++)
{
    byte volatile b = arr[i];
    printf("%c", (b & ~key) | (key & ~b));
}

But it will stay unoptimized for higher optimization levels too. 但是对于更高的优化级别,它也会保持未优化状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM