研究日誌。

大規模なグラフ処理に対してメモリ階層構造を考慮した高性能なソフトウェアを開発。

CPUID: inline assembler

2010-10-26 16:47:05 | Weblog

gcc の拡張機能である inline assembler の使い方。基本的には次のように記述し使用する。

asm ( assembler_template 
       : output operands                 /* optional */
       : input operands                   /* optional */
       : list of clobbered registers    /* optional */
     );

次のように cpuid() 関数を用意すれば良い。

void cpuid(int op, int *eax, int *ebx, int *ecx, int *edx) {
__asm__ __volatile__ ("cpuid" :
"=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx) :
"a" (op) :
"cc");
}

今回 assembler template はもちろん "cpuid"。

output operands の "=a" (*eax) は、 EAX レジスタの値を *eax に格納することを意味している。ここで a は EAX に対応し、= で書込みを指定している。b, c, d も同様である。

input operands の "a" (op) は、EAX レジスタに op の値を格納することを意味している。

list of clobbered registers の "cc" は condition codes が変更されることをコンパイラに伝えている。