asm in C “too many memory references for `mov'”

前端 未结 2 1608
孤城傲影
孤城傲影 2021-01-05 00:42

I\'ve seen the post about the same error but i\'m still get error :

 too many memory references for `mov\'
 junk `hCPUIDmov buffer\' after expression
         


        
2条回答
  •  灰色年华
    2021-01-05 01:02

    1. Because of how C works,

      __asm(
          "mov %eax, 01h"
          "CPUID"
          "mov buffer, edx"
      );
      

      is equivalent to

      __asm("mov %eax, 01h" "CPUID" "mov buffer, edx");
      

      which is equivalent to

      __asm("mov %eax, 01hCPUIDmov buffer, edx");
      

      which isn't what you want.

    2. AT&T syntax (GAS's default) puts the destination register at the end.

    3. AT&T syntax requires immediates to be prefixed with $.

    4. You can't reference local variables like that; you need to pass them in as operands.

    Wikipedia's article gives a working example that returns eax.

    The following snippet might cover your use-cases (I'm not intricately familiar with GCC inline assembly or CPUID):

    int eax, ebx, ecx, edx;
    eax = 1;
    __asm( "cpuid"
         : "+a" (eax), "+b" (ebx), "+c" (ecx), "+d" (edx));
    buffer = edx
    

提交回复
热议问题