2012-12-01 2 views
3

인라인 어셈블리를 사용하여 구조체 멤버를로드하려고합니다 (Particle은 이러한 구조체에 대한 포인터 임). 정크인라인 어셈블리에서 구조 멤버에 액세스 하시겠습니까?

결과
mov  $0(%eax), %edx 
fld  $44(%eax) 
fld  $40(%eax) 
fld  $8(%eax) 
fld  $4(%eax) 
movups $12(%eax), %xmm1 
movups $28(%eax), %xmm2 
movups $48(%eax), %xmm3 
movups $60(%eax), %xmm4 

, gas 취급 (%eax) 후 : GCC는 다음과 같이 즉시 숫자 리터럴 이러한 오프셋을 출력으로하지만,

asm("mov %1(%0), %%edx\n" 
    "fld %2(%0)\n" 
    "fld %3(%0)\n" 
    "fld %4(%0)\n" 
    "fld %5(%0)\n" 
    "movups %6(%0), %%xmm1\n" 
    "movups %7(%0), %%xmm2\n" 
    "movups %8(%0), %%xmm3\n" 
    "movups %9(%0), %%xmm4\n" 
    : 
    : "r" (Particle), 
     "n" (offsetof(ptcParticle, Active)), 
     "n" (offsetof(ptcParticle, Size)), 
     "n" (offsetof(ptcParticle, Rotation)), 
     "n" (offsetof(ptcParticle, Time)), 
     "n" (offsetof(ptcParticle, TimeScale)), 
     "n" (offsetof(ptcParticle, Colour)), 
     "n" (offsetof(ptcParticle, Location)), 
     "n" (offsetof(ptcParticle, Velocity)), 
     "n" (offsetof(ptcParticle, Accel)) 
    : "%edx", "%st", "%st(1)", "%st(2)", "%st(3)", "%xmm1", "%xmm2", 
     "%xmm3", "%xmm4" 
); 

그것은 작동하지 않습니다 여기 내 초기 솔루션입니다 표현식 :

Error: junk `(%eax)' after expression 

출력에서 ​​달러 기호 만 제거 할 수 있으면 작동합니다. 구조 멤버에 액세스하는 방법에 대한 아이디어가 있습니까?

+0

'structs'와'$ '같은 문제는 [GCC 확장 asm, struct element offset encoding] (http://stackoverflow.com/q/13254512)에서 논의됩니다. 이것은 질문의 중복 일지 모르지만 다른 질문은 확장 된 ASM의 컨텍스트에 있기 때문에 확실하지 않습니다. [% CC는 GCC 인라인 어셈블리 코드에서 무엇을 의미합니까?] (http://stackoverflow.com/q/1672900/608639)를 참조하십시오. – jww

답변

4

좋아요, 알아 냈습니다 - %c 연산자가 필요합니다. 나는이 헬퍼 매크로를 작성했습니다 :

#define DECLARE_STRUCT_OFFSET(Type, Member)  \ 
    [Member] "i" (offsetof(Type, Member)) 

을 그리고 다음과 같이 사용 :

asm("mov %c[Active](%0), %%edx\n" 
     "fld %c[Size](%0)\n" 
     "fld %c[Rotation](%0)\n" 
     "fld %c[Time](%0)\n" 
     "fld %c[TimeScale](%0)\n" 
     "movups %c[Colour](%0), %%xmm1\n" 
     "movups %c[Location](%0), %%xmm2\n" 
     "movups %c[Velocity](%0), %%xmm3\n" 
     "movups %c[Accel](%0), %%xmm4\n" 
     : 
     : "r" (Particle), 
      DECLARE_STRUCT_OFFSET(ptcParticle, Active), 
      DECLARE_STRUCT_OFFSET(ptcParticle, Size), 
      DECLARE_STRUCT_OFFSET(ptcParticle, Rotation), 
      DECLARE_STRUCT_OFFSET(ptcParticle, Time), 
      DECLARE_STRUCT_OFFSET(ptcParticle, TimeScale), 
      DECLARE_STRUCT_OFFSET(ptcParticle, Colour), 
      DECLARE_STRUCT_OFFSET(ptcParticle, Location), 
      DECLARE_STRUCT_OFFSET(ptcParticle, Velocity), 
      DECLARE_STRUCT_OFFSET(ptcParticle, Accel) 
     : "%edx", "%st", "%st(1)", "%st(2)", "%st(3)", "%xmm1", "%xmm2", 
      "%xmm3", "%xmm4" 
    ); 

생성 된 어셈블리가 이제 정확하고 모든 것이 작동하는 것 같다.

관련 문제