6

重複の可能性:
インライン アセンブリ操作のために c 変数にアクセスする方法

このコードを考えると:

#include <stdio.h>

int main(int argc, char **argv)
{
  int x = 1;
  printf("Hello x = %d\n", x);


  }

インライン アセンブリで変数 x にアクセスして操作したいと思います。理想的には、インライン アセンブリを使用してその値を変更したいと考えています。GNU アセンブラ、および AT&T 構文の使用。printf ステートメントの直後に x の値を 11 に変更したいとします。

4

1 に答える 1

10

関数は次のasm()順序に従います。

asm ( "assembly code"
           : output operands                  /* optional */
           : input operands                   /* optional */
           : list of clobbered registers      /* optional */
);

そして、Cコードを介してアセンブリでxに11を配置するには:

int main()
{
    int x = 1;

    asm ("movl %1, %%eax;"
         "movl %%eax, %0;"
         :"=r"(x) /* x is output operand and it's related to %0 */
         :"r"(11)  /* 11 is input operand and it's related to %1 */
         :"%eax"); /* %eax is clobbered register */

   printf("Hello x = %d\n", x);
}

破壊されたレジスターを回避することで、上記の asm コードを単純化できます。

asm ("movl %1, %0;"
    :"=r"(x) /* related to %0*/
    :"r"(11) /* related to %1*/
    :);

入力オペランドを避け、c の代わりに asm のローカル定数値を使用することで、さらに単純化できます。

asm ("movl $11, %0;" /* $11 is the value 11 to assign to %0 (related to x)*/
    :"=r"(x) /* %0 is related x */
    :
    :);

別の例: 2 つの数値をアセンブリと比較する

于 2013-01-31T15:33:01.863 に答える