3

インライン アセンブリ命令でコードをコンパイルするために必要なフラグはありますか?

私はg ++に次のコードをコンパイルさせようとしています(SOの回答から複製されました):

#include <iostream>

using namespace std;

inline unsigned int get_cpu_feature_flags()
{
    unsigned int features;

    __asm
    {                             // <- Line 10
        // Save registers
        push    eax
        push    ebx
        push    ecx
        push    edx

        // Get the feature flags (eax=1) from edx
        mov     eax, 1
        cpuid
        mov     features, edx

        // Restore registers
        pop     edx
        pop     ecx
        pop     ebx
        pop     eax
    }

    return features;
}

int main() {
    // Bit 26 for SSE2 support
    static const bool cpu_supports_sse2 = (get_cpu_feature_flags() & 0x04000000)!=0;
    cout << (cpu_supports_sse2? "Supports SSE" : "Does NOT support SSE");
}

しかし、次のエラーが表示されます。

$ g++ t2.cpp 
t2.cpp: In function ‘unsigned int get_cpu_feature_flags()’:
t2.cpp:10:5: error: expected ‘(’ before ‘{’ token
t2.cpp:12:9: error: ‘push’ was not declared in this scope
t2.cpp:12:17: error: expected ‘;’ before ‘eax’
$
4

4 に答える 4

5

他の人がほのめかしているが明示的には言っていないように、これはgcc(真のインラインアセンブリコードの代わりに文字列ベースのasm( "...")言語を使用)およびgas(Intel構文の代わりにAT&T構文を使用)の誤った構文です)。

「gccインラインアセンブリ」のグーグルはこのチュートリアルを引き出します、それはよさそうです:

http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html

そして、ここでgccドキュメントの関連セクションを見つけることができます:

http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/Extended-Asm.html

于 2012-08-23T19:39:58.637 に答える
1

これは

__asm(
   //...
)

いいえ

__asm{
   //...
}

また、標準バージョンはasm.

于 2012-08-23T19:22:48.320 に答える
0

gccインライン アセンブリの場合、構文はasm("<instructions>" : "<output constraints>" : "<input constraints>"). 中括弧の代わりに括弧を使用していること、および命令 (およびオプションの制約句) が文字列リテラルに配置されていることに注意してください。

于 2012-08-23T19:42:23.490 に答える
0

この構文は、ARM プロセッサと MS asm に固有のものであることがわかりました。見る

http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0491c/BABFDCGD.html

別のプロセッサまたはコンパイラ (Keil など) の場合は、サポート ページを参照してください。

于 2014-02-19T21:41:57.803 に答える